目录

1. 文本搜索

2. 文本替换

基本语法

range的指定方式

替换命令历史查询和使用

例1:文件中全局替换 

例2:当前行中替换,忽略大小写

例3:在指定的行范围内进行替换

例4:在选中的行中进行替换

例5:针对文件的前若干行进行替换

例6:全词匹配替换

例7:提醒确认的替换(交互确认式替换)

例8:给每一行的行首添加行号

例9:给每一行行尾添加某一字符串

例10:给每一行行首添加某一字符串 

例11:删除或保留所有不包含某字符串的行

例12:删除某字符串

例13:忽略大小写的替换

例14:基于正则表达式的替换


1. 文本搜索

        To be added

2. 文本替换

基本语法

gvim编辑文本时可以用以下命令进行文本替换。

可以把old_string看作是一个用于搜索匹配的pattern(后文pattern与old_string互换使用)。 

其中,

        "s" 指的是substitute(替换)

        如果没有指定[range] 和 [count]的话,仅对当前行进行处理。当前行是指光标所处行。

        “/”表示定界符(delimiter),但是...Instead of the slash character (/), you can use any other non-alphanumeric single-byte character except as a delimiter. This option is useful when you have the ‘/’ character in the search pattern or the replacement string.比如说:

range的指定方式

        百分号 % 用于表示从第一行到最后一行,即以整个文件作为处理范围。

        用逗号分隔的两个数字表示搜索范围,两个数字分别表示起始行号和终止行号(inclusive,即包含这两个数字所指的行):

        “.”表示当前行,“$”表示最后一行。

        还可以用+和-来表示基于前一个行号指示,指定第2个行号指示相对于前一个的偏移值。比如说从当前行到从当前行后数第4行为止的范围内进行替换:

flags有以下几种:

  1. [c] Confirm each substitution.即每次替换都提请确认。如果不加c则自动替换掉。
  2. [g] Replace all occurrences in the specified range. 指定范围中所有找到的old_string都替换成new_string。如果不指定g,则仅替换指定范围中找到的第一个old_string
  3. [i] Ignore case for the pattern. 忽视大小写。

 

替换命令历史查询和使用

vim会跟踪记录当前编辑会话期间所有命令。可以键入“:s”以查询当前编辑会话期间所有替换命令,用up/down来选定,然后回车即可重复执行。当然也可以在选定后进行必要的修改之后再执行。

 

例1:文件中全局替换 

在以上例中,"%"表示指定整个文件作为操作范围,即%与g结合表示对整个文件进行全局替换

例2:当前行中替换,忽略大小写

例3:在指定的行范围内进行替换

将从第1行到第10行范围内的‘helo’全部替换为‘hello’。

例4:在选中的行中进行替换

首先,选定所要操作的行范围。可用用鼠标操作,也可以按Ctrl+V,然后用导航键(navigation key,即四个箭头键)进行范围选中。然后点击“:”会自动生成“:'<,’>”,然后可以用如下命令进行选定范围内的替换:

例5:针对文件的前若干行进行替换

最后的参数“4”表示对文件的前4行进行替换操作。

例6:全词匹配替换

要点是用\<, \>将old_string圈起来。

\<, \> – word boundary

例7:提醒确认的替换(交互确认式替换)

“c”表示要求提示确认。

执行以上命令,在每次找到old_string时会给出以下提示:

  • y – Will replace the current highlighted word. After replacing it will automatically highlight the next word that matched the search pattern
  • n – Will not replace the current highlighted word. But it will automatically highlight the next word that matched the search pattern
  • a – Will substitute all the highlighted words that matched the search criteria automatically.
  • l – This will replace only the current highlighted word and terminate the find and replace effort.

例8:给每一行的行首添加行号

When the string starts with ‘\=’, it should be evaluated as an expression. Using the ‘line’ function we can get the current line number. By combining both the functionality the substitution does the line numbering of all lines.

Note: This is different from the “:set number” where it will not write the line numbers into the file. But when you use this substitution you are making these line number available inside the file permanently.

例9:给每一行行尾添加某一字符串

 

例10:给每一行行首添加某一字符串 

例11:删除或保留所有不包含某字符串的行

在比如说芯片仿真时会碰到大量打印Warning或者Error信息的情况,这是想过滤一下仅保留包含Error的行的话,可以使用以下命令:        

反之,如果要删除所有包含指定字符串的行保留所有其它行命令如下:

例12:删除某字符串

删除某字符串可以理解为用空字符串(不是空格!)来替换该字符串!

例13:忽略大小写的替换

缺省动作是大小写敏感(case-sensitive)的,如果需要忽略对pattern(oldstring)的大小写,可以用i选项来实现:

另外一种方法是在pattern后面加上“\c”,也表示忽略大小写。

反之,在pattern后面加上“\C”,ze表示要求区分大小写(有时候将缺省配置修改为忽略大小写时这个就可以用于使能区分大小写)。

例14:基于正则表达式的替换

太难。。。新手不宜。。。

 

ref1:Vi and Vim Editor: 12 Powerful Find and Replace Examples (thegeekstuff.com)

ref2: Find and Replace in Vim / Vi | Linuxize 

07-08 09:18