移动光标
gg:文件第一行
G:文件最后一行
ctrl+f:下一屏
ctrl+b:上一屏
$:本行尾字符
0:本行首字符
n+Enter:向下移动n行
搜索与替换
/word:向下搜索
?word:向上搜索
n:下一个
N:上一个
:[addr]s/原字符串/目的字符串/[option]
说明:
[addr] - 表示检索范围,省略时表示当前行
s - substitute(替换)
g - gobal
基本搜索与替换
The :substitute
:%s/foo/bar/g
Find each occurrence of 'foo', and replace it with 'bar'.
:%s/foo/bar/gc
Change each 'foo' to 'bar', but ask for confirmation first.
:%s/\<foo\>/bar/gc
Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation.
:%s/foo/bar/gci
Change each 'foo' (case insensitive) to 'bar'; ask for confirmation.
This may be wanted after using :set noignorecase to make searches case sensitive (the default).
:%s/foo/bar/gcI
Change each 'foo' (case sensitive) to 'bar'; ask for confirmation.
This may be wanted after using
:set ignorecase to make searches case insensitive.
The g flag means global – each occurrence in the line is changed, rather than just the first. This tip assumes the default setting for the 'gdefault' option (off), which requires that the g flag be included in%s///g to perform a global substitute. Using:set gdefaultcreates confusion because then%s/// is global, whereas%s///g is not (that is,g reverses its meaning).
参考资料
官方文档,http://vimcdoc.sourceforge.net/doc/help.html
Search and replace, http://vim.wikia.com/wiki/Search_and_replace
vim 全局替换命令,http://andyss.blog.51cto.com/315552/131652
剪切、复制、粘贴、撤销、重复
x:剪切当前字符
X:剪切前一个字符
dd:剪切当前行
ndd:剪切光标所在向下n行
dG:剪切当前至最后一行
d1G:剪切当前至第一行
复制(同上)
yy:
nyy:
yG:
y1G:
p:先后或下粘贴
P:先前或上粘贴
u:撤销
ctrl+r:重复上一个操作
补充非常用:
d0:剪切当前至本行第一个字符
d$:剪切当前至本行最后一个字符
复制(同上)
y0:
y$:
保存、退出
:w
:wq
ZZ
:q
:w!——对只读文档强制写入
:q!——不保存更改退出
:w <filename>——另存为
:n1,n2 w <filename>——将n1-n2行另存为
:r <filename>——读入<filename>文档中的内容
执行命令
:!CMD
set
:set nu/nonu - 显示行号
:set wrap/nowrap - 自动换行
:set hlsearch/nohlsearch - 高亮查找字符
区块选择(Visual Block)
v:字符选择,会将光标经过的地方反白选择
V:行选择,会将光标经过的行反白选择
ctrl+v:区块选择,可以用长方形的方式选择区块
y:将反白的地方复制起来
d:将反白的地方剪切
多文档编辑
vim <file1> <file2> <file3> ...
:files——列出所有文档
:n——下一个文档
:N——上一个文档
多窗口
:sp <file>——再开启一个窗口
[ctrl]+w+j或[ctrl]+w+↓:转到下方窗口。按键的按法是:先按下[ctrl] 不放, 再按下w 后放开所有的按键,然后再按下j (或向下方向键)
[ctrl]+w+ k或[ctrl]+w+↑:转到上方窗口
[ctrl]+w+ q:离开