文章目录
lecture
当程序员显然很大一部分时间都在写代码,熟练掌握editor工具对提升效率帮助挺大的
Tips太长不看版:可以直接
vimtutor去跟着教程实践一遍
如何掌握新的editor
- 看教程
- 即使最开始有点慢也要坚持用这个editor进行各种编辑
- 查找改进用法,如果有可能有更好用的方式,那很可能真的有
一般1-2小时就可以学会基本用法,20小时后就和之前的editor一样熟练了,之后就可以显示出新editor的强大功能来节省时间了
会有新的有意思的编辑器出现,学的越多越快!
可以看Stack Overflow来看当前比价流行的editor
Vim特点
由于编程中很多时间是在读、来回移动和少量的编辑,而不是大段大段连续的写,因而vim分为不同的模式,插入文本和操作文本是不一样的模式。
vim不用鼠标,因为鼠标太慢了,也不用上下左右箭头,因为需要比较大的移动,因此熟练使用vim的话就会非常快
vim model
- normal 浏览文件,做一些编辑处理
- insert 插入文本
- replace 替换文本
- visual(plain line block) 选择文本段
- command line 运行命令
同样的key在不同的模式下有不同的作用,一般在左下角显示model,通常情况下大部分是在Normal(initial/default)和Insert下
所有模式按ESC可以回到Normal模式下

因为会频繁的涌到ESC,所以可以把大写锁定key换成esc macOS solution 、windows solution
基本用法
插入文本:
按i进入insert mode
buffers,tabs,windows
vim可以打开很多文件,称为buffers,一个vim session有多个tabs,每个tabs有多个windows,每个windows展示一个Buffer。不像网页浏览器那样tbuffer和window之间是1对1的关系,vim中windows只负责展示数据。
一个Buffer可以在多个window中打开,一个 Window 在同一时间内只能展示一个 Buffer,一个 Buffer 可以同时被多个 Window 展示。这就意味着可以同时打开一个文件的不同部分。一个 Tab 上可以有多个 Window,不同 Tab 之间的 Window 互不影响。
官方解释这三者是:
A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.
可以看下blog
打开多个window的时候,:q只会退出顶部的那个window, :qa全关
command-line
在normal node下按冒号可以进入命令模式。
:qquit (close window) 关闭top的tab(:qa会关掉各个打开的windows):wsave (“write”) 保存:wqsave and quit 保存并关闭:e{name of file} open file for editing 打开文件:lsshow open buffers 展示打开的文件:sp拆成两个视图:help{topic} 后面跟特定的key或者命令来查看具体的帮助:help :wopens help for the :w command:help wopens help for the w movement- 按F1也可以直接打开帮助界面,
:q离开
移动选择等操作
Vim接口本身就可以看作是编程语言,按键本身就是命令,还可以组合。当这些变成肌肉记忆的时候就会格外的快
移动
在normal mode里
- Basic movement:
hjkl(left, down, up, right)左下上右移动(4j下移4次) - Words:
w(next word移动到下一个单词),b(beginning of word移动到单词开头),e(end of word移动到单词末尾) - Lines按行移动:
0(beginning of line),^(first non-blank character),$(end of line) - Screen按屏移动:
H(top of screen),M(middle of screen),L(bottom of screen) - Scroll滚动:
Ctrl-u(up),Ctrl-d(down) - File:
gg(beginning of file),G(end of file) Ctrl+g会显示当前在第几行; 数字+G会回到第n行- Line numbers:
:{number}<CR>or{number}G比如3G (line {number}) - Misc:
%(corresponding item) 按%可以让光标跳到对应的当前括号对应的括号上{([ - Find:
f{character},t{character},F{character},T{character}
find/to forward/backward {character} on the current line
,/;for navigating matches 比如fo向下找到第一个字母o - Search:
/{regex},n/Nfor navigating matches /- 比如/range回车,会到range第一个字母处,按
n到下一个匹配位置 ,N反向 ?代替/的话可以反向查找
- 比如/range回车,会到range第一个字母处,按
.可以重复之前的编辑命令,比如第一个位置添加“end=’’”后,jj.会向下两行之后也加上“end="”。这样就不用重复打字了Ctrl+o回到之前的位置,Ctrl+i向前移动

选择
在normal mode下:
Visual: v
Visual Line: V
Visual Block: Ctrl-v
进入选择模式后可以移动进行选择
选择完了可以y进行复制,又回到normal mode下
edit
所有需要用到鼠标的地方都可以用组合key来进行
i插入,a光标后面append,A行末尾追加插入oO在这行的下面或者上面插入d{modtion}进行各种删除dw删除单词直到下一个单词开始,de删到这个单词结束,d$删到这行结束,d0删到这行开头,dd删除整行,d命令总是需要和移动动作结合在一起用,dd完了在p可以直接复制刚刚删除的内容c{motion}修改,相当于d{motion}+i,比如ce之后就可以直接输入修改后的单词,基本用法和d一致x删除字符s替换,相当于xir替换,到想要替换的字母前按r然后按想要替换的那个目标字母,可以替换掉当前光标后的字母;大写R可以替换多个字符,按ECS退出替换- 在visual mode下,移动来选择,
d删除,c修改 uundo ,Ctrl+rredo,U撤回整行的操作ycopy 也是需要结合movement,比如yw复制当前行,yy复制当前行ppaste or 复制刚刚删除的内容
还有非常多
计数
给定操作执行多次,数字+动作,比如
3wmove 3 words forward5jmove 5 lines down7dwdelete 7 words,7dw也可以,2dd可以连续删除两行
modifiers
可以用来改变操作的意思
比如i意味着内部,a意味着周围
ci(change the contents inside the current pair of parentheses改变小括号内的内容ci[change the contents inside the current pair of square brackets改变中括号内的内容da'delete a single-quoted string, including the surrounding single quotes 删除单引号之间的内容,包括单引号- 这些都是可以组合改变的,像编程一样
配置vim
通过编辑~/.vimrc来配置
可以参考课程给出的配置,粘贴到文件里:link
可以多看看别人在github上分享出来的配置,明白到底配置了什么东西
创建文件夹~/.vim/pack/vendor/start/然后通过git clone来下载plugins可以拓展vim
推荐的几个vim插件:
- ctrlp.vim: fuzzy file finder
- ack.vim: code search
- nerdtree: file explorer
- vim-easymotion: magic motions
所有的shell里都可以配置export EDITOR=vim环境变量
甚至Web也可以配置成vim的一些用法,比如chrome的Vimium,也可以在jupyter notebook中配置
高级用法
搜索和替换
-
:s替换 documentation -
%s/foo/bar/g-->:%s/old/new/g- replace foo with bar globally in file
:%s/foo/bar/gc每次替换前都问一下:s/foo/bar/g只换当前行,不加/g只换出现的第一个
-
可以加行号
:#,#s/old/new/g -
%s/\[.*\](\(.*\))/\1/g- replace named Markdown links with plain URLs
多窗口
:sp/:vspto split windows- 一个Buffer可以有多个views
Ctrl w Ctrl w在window之间跳
宏
q{character}to start recording a macro in register{character}qto stop recording@{character}replays the macro- Macro execution stops on error
{number}@{character}executes a macro {number} times- Macros can be recursive
- Vim commands / macros
Gdd, ggdddelete first and last lines- Macro to format a single element (register
e)- Go to line with
<name> qe^r"f>s": "<ESC>f<C"<ESC>q
- Go to line with
- Macro to format a person
- Go to line with
- qpS{j@eA,j@ejS},q
- Macro to format a person and go to the next person
- Go to line with
- qq@pjq
- Execute macro until end of file
- 999@q
- Manually remove last
,and add[and]delimiters
其他
:!后可以加其他命令,比如:!ls回车,就像在shell中一样:w xxx可以把当前文件另存为xxx文件- 部分保存,按
v进入选择模式进行选择,然后:会看到
,接着在后面输入w xxx就可以部分保存到文件xxx中 - 插入文件中的内容:
:r FILENAME,还可以插入命令执行的结果,比如:r !ls - 配置搜索选项,一次搜索忽略大小写可以
/something\c回车,设置一直忽略大小写可以:set ic,关闭则set noic,想要高亮历史搜索结果可以set hls is,关闭可以:nohlsearch,一边搜索一边高亮可以:set is。一般来说就是:set设置,:set no xxx关掉 - 命令补全,
Ctrl D+TAB
参考资料
vimtutor装了vim之后可以直接在shell运行vimtutor查看官方教程- Vim Adventures is a game to learn Vim
- Vim Tips Wiki
- Vim Advent Calendar has various Vim tips
- Vim Golf is code golf, but where the programming language is Vim’s UI
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim (book)
练习
1.完成vimtutor. Note: it looks best in a 80x24 (80 columns by 24 lines) terminal window. √
2.Download our basic vimrc and save it to ~/.vimrc. Read through the well-commented file (using Vim!), and observe how Vim looks and behaves slightly differently with the new config. √
3.Install and configure a plugin: ctrlp.vim.
本文详细介绍了Vim编辑器的特点、基本用法和高级技巧,包括移动、选择、编辑等操作,并提供了配置和插件建议。通过学习和实践,提升编程效率。

被折叠的 条评论
为什么被折叠?



