下面用一个cheat sheet记录一些vim的快捷指令,方便自己需要的时候查询。
search
查找模式中加\c为大小写不敏感查找,\C为大小写敏感查找
e.g.
# ignore uppercase
/foo\c or
:
# do not ignore uppercase
/foo\C
replace
:s(substitute)命令用来查找和替换字符串。格式为
# the partition character can be '/' or '#' or '@' (maybe other special characters)
:{作用范围}s/{目标}/{替换}/{替换标志} or
:{作用范围}s#{目标}#{替换}#{替换标志} or
:{作用范围}s@{目标}@{替换}@{替换标志}
其中,替换标志有下面几种:
替换标志 | 含义 | 备注 |
---|---|---|
i/I | 大小写敏感查询 | i为敏感 |
c | 替换前需要确认 | |
g | 全局替换 | 一行中的多个匹配项都会被替换 |
e.g.
# replace foo with bar in current line for all matches
:s/foo/bar/g
# replace foo with bar in the whole context only for the first match of each line
:%s/foo/bar/
# replace foo with bar searching from line 5 to line 12
:5,12s/foo/bar/
# replace foo with bar searching from current line to the 2 plus lines(3 lines in all) in uppercase ignore case
:.,+2s/foo/bar/I