目录

1. 文本搜索

1.1 基本命令

1.2 全词搜索

1.3 搜索当前字符串

1.4 pattern中包含特殊字符

1.5 忽略大小写的搜索

1.6 高亮(Highlight)现实搜索结果

2. 文本替换

基本语法

range的指定方式

替换命令历史查询和使用

例1:文件中全局替换 

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

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

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

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

例6:全词匹配替换

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

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

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

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

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

例12:删除某字符串

例13:忽略大小写的替换

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


1. 文本搜索

1.1 基本命令

在vim/gvim的normal模式(再任意其它模式按ESC键回到normal mode),按“/”或者“?”后面跟所要搜索的pattern,然后按回车开启对pattern的搜索:

/pattern

?pattern

进入搜索模式后,按“n”表示寻找下一个,按“N”(shift + n)表示寻找上一个。

1.2 全词搜索

以上基本命令只是将pattern当作字符串进行匹配,比如说搜索“/world”的话,文本中的“hello_world”也会匹配上。如果要进行全词(whole word)匹配的话,需要用以"/<pattern\>"的格式进行搜索。

/\<world\>

1.3 搜索当前字符串

将光标放在文本中待搜索字符串pattern上,然后按“*”表示向前搜索该pattern,按“#”表示向后搜索该pattern。持续按“*”或者“#”的话会持续向前或者向后搜索。

1.4 pattern中包含特殊字符

pattern中包含特殊字符时,需要用“\”将其进行转义处理。

比如说,要搜索文本文件中的scoreboard[4], 这里"["和“]”是特殊字符。

错误的搜索命令:

/scoreboard[4]

正确的搜索命令:

 /scoreboard\[4\]

1.5 忽略大小写的搜索

缺省条件下vim的搜索是case-sensitive(大小敏感的) ,可以用属性“\c”来告诉vim在搜索时忽略大小写,如下所示:

/\<pattern\>\c

另一个解决办法是用以下命令告诉vim在本次编辑过程中都忽视大小写。

:set ignorecase 

当然,也可以在~/.vimrc文件中追加以上这条命令,这样,vim就会把缺省行为修改为搜索处理中忽略大小写。

1.6 高亮(Highlight)现实搜索结果

Vi(m) has a useful feature that highlights search results in the file. To enable highlighting, type the following command in the text editor:

:set hlsearch

To disable this feature run:

:set !hlsearch

当然,同样可以在~/.vimrc中追加这条命令使其成为缺省行为。

2. 文本替换

基本语法

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

:[range]s[ubstitute]/{old_string}/{new_string}/[flags] [count]

可以把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.比如说:

:s|foo|bar|

range的指定方式

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

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

:3,10s/foo/bar/g

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

:.,$s/foo/bar/

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

:.,+4s/foo/bar/g 

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:文件中全局替换 

:%s/old_string/new_string

:%s/old_string/new_string/g

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

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

:s/I/We/gi

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

:1,10s/helo/hello/g

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

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

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

:'<,'>s/helo/hello/g

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

:s/helo/hello/g 4

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

例6:全词匹配替换

:s/\<his\>/her/

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

\<, \> – word boundary

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

:%s/old_string/new_string/gc

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

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

replace with wonderful (y/n/a/q/l/^E/^Y)?
  • 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:给每一行的行首添加行号

:%s/^/\=line(".") . ". "/g

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:给每一行行尾添加某一字符串

:%s/$/StringToBeAppend>/g

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

:%s/^/StringToBeAppend/g

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

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

:g!/Error/d

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

:g/ERROR/d 

例12:删除某字符串

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

:s/foo//g

例13:忽略大小写的替换

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

:s/Foo/bar/gi

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

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

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

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

ref1: How to Search a Word in Vim / Vi {Multiple Find Options} (phoenixnap.com)

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

ref3: Find and Replace in Vim / Vi | Linuxize 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐