Skip to content

Instantly share code, notes, and snippets.

@SimonLeeGit
Created May 12, 2021 03:42
Show Gist options
  • Save SimonLeeGit/c705aa21763432dc23990aee9fc2871b to your computer and use it in GitHub Desktop.
Save SimonLeeGit/c705aa21763432dc23990aee9fc2871b to your computer and use it in GitHub Desktop.
VIM 常用命令 —— 替换
语法为
:[addr]s/源字符串/目的字符串/[option]
全局替换命令为:
:%s/源字符串/目的字符串/g
[addr] 表示检索范围,省略时表示当前行。
“1,20” :表示从第1行到20行;
“%” :表示整个文件,同“1,$”;
“. ,$” :从当前行到文件尾;
s : 表示替换操作
[option] : 表示操作类型
g 表示全局替换;
c 表示进行确认
p 表示替代结果逐行显示(Ctrl + L恢复屏幕);
省略option时仅对每行第一个匹配串进行替换;
如果在源字符串和目的字符串中出现特殊字符,需要用”\”转义 如 \t
下面是一些例子:
==================================================
#将That or this 换成 This or that
:%s/\(That\) or \(this\)/\u\2 or \l\1/
=======================================================
#将句尾的child换成children
:%s/child\([ ,.;!:?]\)/children\1/g
=======================================================
#将mgi/r/abox换成mgi/r/asquare
:g/mg\([ira]\)box/s//mg//my\1square/g <=> :g/mg[ira]box/s/box/square/g
=======================================================
#将多个空格换成一个空格
:%s/ */ /g
=======================================================
#使用空格替换句号或者冒号后面的一个或者多个空格
:%s/\([:.]\) */\1 /g
=======================================================
#删除所有空行
:g/^$/d
=======================================================
#删除所有的空白行和空行
:g/^[ ][ ]*$/d
=======================================================
#在每行的开始插入两个空白
:%s/^/> /
=======================================================
#在接下来的6行末尾加入.
:.,5/$/./
=======================================================
#颠倒文件的行序
:g/.*/m0O <=> :g/^/m0O
=======================================================
#寻找不是数字的开始行,并将其移到文件尾部
:g!/^[0-9]/m$ <=> g/^[^0-9]/m$
=======================================================
#将文件的第12到17行内容复制10词放到当前文件的尾部
:1,10g/^/12,17t$
~~~~重复次数的作用
=======================================================
#将chapter开始行下面的第二行的内容写道begin文件中
:g/^chapter/.+2w>>begin
=======================================================
:/^part2/,/^part3/g/^chapter/.+2w>>begin
=======================================================
:/^part2/,/^part3/g/^chapter/.+2w>>begin|+t$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment