【发布时间】:2017-09-12 16:30:06
【问题描述】:
我看到how to search and replace in specific lines,按行号指定,还有how to search and replace using the current line as reference to a number of lines down。
如何仅在当前行中搜索和替换?我正在寻找一个简单的解决方案,它不需要像链接的解决方案那样指定行号。
【问题讨论】:
我看到how to search and replace in specific lines,按行号指定,还有how to search and replace using the current line as reference to a number of lines down。
如何仅在当前行中搜索和替换?我正在寻找一个简单的解决方案,它不需要像链接的解决方案那样指定行号。
【问题讨论】:
用str2 替换某行中所有出现的str1:
:s/str1/str2/g
如果您只想替换第一个匹配项,请删除 g 选项。
【讨论】:
当前行可以使用.,比如:
:.s/old/new/
这只会在当前行中将old 更改为new。
【讨论】:
g 标志即可更改所有事件。问题是how to search and replace in current line only?,我已经回答了点是考虑当前行的方式。
g 更新您的答案,因此评论为提醒
s// 命令中的默认值。使用点和不使用点有什么区别吗?
.s/old/new/g
如果您想搜索并替换当前行中所有匹配的单词,您可以在命令模式下轻松使用简单的substitute (s) 和g 修饰符。
:s/search/replace/g
如果您只想搜索和替换当前行中的第一个匹配词,只需从命令中移开 g 修饰符即可。
:s/search/replace/
参考::help substitute
【讨论】:
确认 (c) 并替换:
:s/foo/bar/gc
查找 'foo' 的出现,并在将其替换为 'bar' 之前请求确认。
Vim 为您提供了这些选项:
replace with bar (y/n/a/q/l/^E/^Y)?
y - 是的n - 没有a - 替换所有匹配项q - 退出l - 替换当前并停止(最后)^E (CTRL + e) - 向下滚动^Y (CTRL + y) - 向上滚动现在,在您的情况下,您可以使用y、n 和l 的组合来实现您的目的。
【讨论】: