Vim:替换和查找

Linux VI提供了文件中字符串的查找和全局替换的方法。在命令模式下输入/或?可进入查找模式/输入“/searchstring”,然后回车,VI光标从光标位置开始出现第一次出现的地方。输入n跳到该串的下一个出现处,输入N跳到该串上一次出现的位置。

在替换时,可指定替换的范围(1,n),当n为$时指定为最后一行。s是替换命令,g代表全部替换。

例如:

:1,$ s/pattern1/pattern2/g

将行1至结尾的文字中匹配模式pattern1的字符串替换为pattern2字符串。如无g,则仅替换每一行所匹配的第一个字符串;如有g,则将每一个字符串均作替换。

现有一段文件的内容如下:

1
2
3
4
5
6
7
8
9
10
11
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hand</groupId>
<artifactId>hap-db</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

将所有的dependency字符串替换为test字符串的命令如下:

:1,$ s/dependency/test/g

替换后的内容如下:

1
2
3
4
5
6
7
8
9
10
11
<test>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</test>
<test>
<groupId>com.hand</groupId>
<artifactId>hap-db</artifactId>
<version>1.0-SNAPSHOT</version>
</test>

如果你输入的是

:1,10 s/dependency/test/g

那么你将会替换前面10行

1
2
3
4
5
6
7
8
9
10
11
<test>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</test>
<test>
<groupId>com.hand</groupId>
<artifactId>hap-db</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
0%