Vim - Performing substitution on certain lines only

好久不见. 提交于 2020-01-14 06:22:13

问题


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 

Works great. But can someone please explain the need for the | characters in the command section of the :g call?


回答1:


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

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}".



来源:https://stackoverflow.com/questions/23679200/vim-performing-substitution-on-certain-lines-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!