简体   繁体   English

Vim - 在不以'character'开头的行上用'bar'替换'foo'

[英]Vim — replace ‘foo’ by ‘bar’ on lines NOT starting with ‘character’

In Vim regular expression, I know it is possible to replace foo by bar on all lines starting with % using 在Vim正则表达式中,我知道可以在以% using开头的所有行上用bar替换foo

:g/^%/s/foo/bar/g

but I want to replace foo by bar on all lines NOT starting with % . 但是我希望在不以%开头的所有行上用bar替换foo Is there a way to easily do so? 有没有办法轻松这样做?

试试:vglobal

:v/^%/s/foo/bar/g

You can just negate the % character using character class : - 您可以使用character class来否定%字符: -

:g/^[^%]/s/foo/bar/g

[^%] match any character except % , at the start of the string. [^%]匹配字符串开头处除%之外的任何字符。

The inverse of :g is :g! 反过来:g:g! , so your example could be expressed: ,所以你的例子可以表达:

:g!/^%/s/foo/bar/g

Note that :g! 注意:g! is just another way of writing :v (cf. Jim Davis' answer) 只是另一种写作方式:v (参见Jim Davis的回答)

尝试:g/^[^%]/s/foo/bar/g匹配所有不以%开头的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM