如何在 Vim 中的单个命令调用中全局搜索和替换,从光标位置开始并环绕文件末尾? [英] How to search and replace globally, starting from the cursor position and wrapping around the end of file, in a single command invocation in Vim?
问题描述
当我使用 /
普通模式命令搜索时:
When I search with the /
Normal-mode command:
/\vSEARCHTERM
Vim 从光标位置开始搜索并继续向下,环绕到顶部.但是,当我使用 :substitute
命令进行搜索和替换时:
Vim starts the search from the cursor position and continues downwards, wrapping around to the top. However, when I search and replace using the :substitute
command:
:%s/\vBEFORE/AFTER/gc
Vim 从文件顶部开始.
Vim starts at the top of the file, instead.
有没有办法让 Vim 从光标位置开始执行搜索和替换并一旦到达末尾就环绕到顶部?
Is there a way to make Vim perform search and replace starting from the cursor position and wrapping around to the top once it reaches the end?
推荐答案
1. 使用两步替换:
:,$s/BEFORE/AFTER/gc|1,''-&&
首先,对每一行开始执行替换命令直到文件末尾的当前文件:
First, the substitution command is run for each line starting from the current one until the end of file:
,$s/BEFORE/AFTER/gc
然后,该 :substitute
命令以相同的搜索重复模式、替换字符串和标志,使用 :&
命令(见 :help :&
):
Then, that :substitute
command is repeated with the same search
pattern, replacement string, and flags, using the :&
command
(see :help :&
):
1,''-&&
然而,后者在行的范围内执行替换从文件的第一行到前一个上下文所在的行标记已设置,减一.从第一个 :substitute
命令开始在开始实际替换之前存储光标位置,''
寻址的行是之前的当前行该替换命令已运行.(''
地址指的是'
伪标记;有关详细信息,请参阅 :help :range
和 :help ''
.)
The latter, however, performs the substitution on the range of lines
from the first line of the file to the line where the previous context
mark has been set, minus one. Since the first :substitute
command
stores the cursor position before starting actual replacements, the
line addressed by ''
is the line that was the current one before
that substitution command was run. (The ''
address refers to the
'
pseudo-mark; see :help :range
and :help ''
for details.)
注意第二个命令(在 |
命令分隔符之后——见:help :bar
) 当模式或标志在第一个中进行了更改.
Note that the second command (after the |
command separator—see
:help :bar
) does not require any change when the pattern or flags
are altered in the first one.
2. 为了节省一些打字,为了调出命令行中的上述替换命令,可以定义正常模式映射,如下所示:
2. To save some typing, in order to bring up the skeleton of the above substitution command in the command line, one can define a Normal-mode mapping, like so:
:noremap <leader>cs :,$s///gc\|1,''-&&<c-b><right><right><right><right>
尾随
部分是必要的将光标移动到命令行的开头 (
) 和然后向右四个字符(
× 4),因此把它放在前两个斜线符号之间,为用户准备开始输入搜索模式.一旦所需的模式和替换准备就绪,可以通过按运行生成的命令输入.
The trailing <c-b><right><right><right><right>
part is necessary
to move the cursor to the beginning of the command line (<c-b>
) and
then four characters to the right (<right>
× 4),
thus putting it between the first two slash signs, ready for the user
to start typing the search pattern. Once the desired pattern and the
replacement are ready, the resulting command can be run by pressing
Enter.
(可以考虑在上面的映射中使用 //
而不是 ///
,如果您更喜欢输入模式,则输入分隔斜线自己,后跟替换字符串,而不是使用向右箭头将光标移动到已经存在的分隔符上斜线开始替换部分.)
(One might consider having //
instead of ///
in the mapping above,
if one prefers to type the pattern, then type the separating slash
oneself, followed by the replacement string, instead of using the
right arrow to move the cursor over an already present separating
slash starting the replacement part.)
这篇关于如何在 Vim 中的单个命令调用中全局搜索和替换,从光标位置开始并环绕文件末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!