Vim - 仅在某些行上执行替换 [英] Vim - Performing substitution on certain lines only
问题描述
我在寻求仅对偶数行执行替换的过程中发现以下内容:
I have found the following in my quest to perform a substitution on even numbered lines only:
:g/^/if !(line('.')%2)|s/foo/bar/g|endif
效果很好.但是有人可以解释一下对 | 的需要吗?:g 调用的命令部分中的字符?
Works great. But can someone please explain the need for the | characters in the command section of the :g call?
推荐答案
|
字符是命令分隔符;使用它,您可以在一行中连接多个 Ex 命令,而无需添加换行符.请参阅 :help :bar
.
The |
character is the command separator; with it, you can concatenate multiple Ex commands in a single line, without adding newlines. See :help :bar
.
因此,您的条件等效于以下内容:
So, your conditional is equivalent to the following:
if !(line('.')%2)
s/foo/bar/g
endif
请注意,一些 Ex 命令占用了命令行的全部剩余部分,因此不能直接与 |
连接.但是您可以将这些命令包装在 :execute "{cmd}"
中.
Note that some Ex commands consume the entire remainder of the command-line and therefore cannot be directly concatenated with |
. But you can wrap those commands in :execute "{cmd}"
.
这篇关于Vim - 仅在某些行上执行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!