在vim中将C语句替换为替换语句

在vim中将C语句替换为替换语句,vim,vi,Vim,Vi,我想使用vim的替换函数(:%s)来搜索和替换特定的代码模式。例如,如果我有类似于以下内容的代码: if(!foo) %s/if(!\(.\{-}\))/if(\1 == NULL)/gc qa .............. starts macro 'a' f! .............. jumps to next '!' x ............... erase that e ............... jump to the end of word a .........

我想使用vim的替换函数(:%s)来搜索和替换特定的代码模式。例如,如果我有类似于以下内容的代码:

if(!foo)
%s/if(!\(.\{-}\))/if(\1 == NULL)/gc
qa .............. starts macro 'a'
f! .............. jumps to next '!'
x ............... erase that
e ............... jump to the end of word
a ............... starts append mode (insert)
== NULL ........ literal == NULL
<ESC> ........... stop insert mode
q ............... stops macro 'a'

:%norm @a ........ apply marco 'a' in the whole file
:g/^if(!/ norm @a  apply macro 'a' in the lines starting with if...
我想用以下内容代替:

if(foo == NULL)
然而,foo只是一个例子。变量名可以是任何内容

这是我为vim命令想到的:

:%s/if(!.*)/if(.* == NULL)/gc
它正确地搜索语句,但它尝试用“.*”替换它,而不是那里的变量(即“foo”)。有没有一种方法可以满足我对vim的要求

如果没有,是否有其他编辑器/工具可用于帮助我进行此类修改

提前谢谢

您可以使用

:%s/if(!\(.*)/if(\1==NULL)/gc

通过在\(\)中放入.*可以创建编号的捕获组,这意味着正则表达式将捕获其中的内容* 当替换开始时,您将使用\1打印捕获的组

您可以使用

:%s/if(!\(.*)/if(\1==NULL)/gc

通过在\(\)中放入.*可以创建编号的捕获组,这意味着正则表达式将捕获其中的内容*
当替换开始时,您将使用\1打印捕获的组

您需要使用捕获分组反向引用来实现这一点:

      Pattern     String sub. flags
    |---------| |------------| |-|
:%s/if(!\(.*)/if(\1==NULL)/gc
|--||--|
|        ^
|________|
模式中匹配的字符串将在字符串替换中完全重复


您需要使用捕获分组反向引用来实现这一点:

      Pattern     String sub. flags
    |---------| |------------| |-|
:%s/if(!\(.*)/if(\1==NULL)/gc
|--||--|
|        ^
|________|
模式中匹配的字符串将在字符串替换中完全重复

请尝试以下操作:

if(!foo)
%s/if(!\(.\{-}\))/if(\1 == NULL)/gc
qa .............. starts macro 'a'
f! .............. jumps to next '!'
x ............... erase that
e ............... jump to the end of word
a ............... starts append mode (insert)
== NULL ........ literal == NULL
<ESC> ........... stop insert mode
q ............... stops macro 'a'

:%norm @a ........ apply marco 'a' in the whole file
:g/^if(!/ norm @a  apply macro 'a' in the lines starting with if...
量词
\{-}
尽可能少地匹配非空单词(比
*
更严格)

paranetsis
\(
\)
用于将搜索的表达式划分为子表达式,以便您可以在替换字符串中使用这些子组

最后,
\1
允许用户使用第一个匹配的子表达式,在我们的例子中,它是捕获到的任何子表达式

我希望这是更清楚,更多的信息可以找到。感谢您提出的改进答案的建议。

尝试以下方法:

if(!foo)
%s/if(!\(.\{-}\))/if(\1 == NULL)/gc
qa .............. starts macro 'a'
f! .............. jumps to next '!'
x ............... erase that
e ............... jump to the end of word
a ............... starts append mode (insert)
== NULL ........ literal == NULL
<ESC> ........... stop insert mode
q ............... stops macro 'a'

:%norm @a ........ apply marco 'a' in the whole file
:g/^if(!/ norm @a  apply macro 'a' in the lines starting with if...
量词
\{-}
尽可能少地匹配非空单词(比
*
更严格)

paranetsis
\(
\)
用于将搜索的表达式划分为子表达式,以便您可以在替换字符串中使用这些子组

最后,
\1
允许用户使用第一个匹配的子表达式,在我们的例子中,它是捕获到的任何子表达式


我希望这是更清楚,更多的信息可以找到。感谢您提出的改进答案的意见。

在这种情况下,宏很简单,只需执行以下操作:

if(!foo)
%s/if(!\(.\{-}\))/if(\1 == NULL)/gc
qa .............. starts macro 'a'
f! .............. jumps to next '!'
x ............... erase that
e ............... jump to the end of word
a ............... starts append mode (insert)
== NULL ........ literal == NULL
<ESC> ........... stop insert mode
q ............... stops macro 'a'

:%norm @a ........ apply marco 'a' in the whole file
:g/^if(!/ norm @a  apply macro 'a' in the lines starting with if...
qa。。。。。。。。。。。。。。启动宏“a”
F跳到下一个
x。。。。。。。。。。。。。。。抹掉
E跳到单词的末尾
A.启动附加模式(插入)
==空。。。。。。。。文本==NULL
........... 停止插入模式
Q停止宏“a”
:%norm@a。。。。。。。。在整个文件中应用马可“a”
:g/^if(!/norm@a在以if开头的行中应用宏“a”。。。

在这种情况下,宏很简单,只需执行以下操作:

if(!foo)
%s/if(!\(.\{-}\))/if(\1 == NULL)/gc
qa .............. starts macro 'a'
f! .............. jumps to next '!'
x ............... erase that
e ............... jump to the end of word
a ............... starts append mode (insert)
== NULL ........ literal == NULL
<ESC> ........... stop insert mode
q ............... stops macro 'a'

:%norm @a ........ apply marco 'a' in the whole file
:g/^if(!/ norm @a  apply macro 'a' in the lines starting with if...
qa………..启动宏“a”
跳转到下一个“!”
x………..将其删除
跳到单词的末尾
a………..启动附加模式(插入)
==NULL…….文本==NULL
……停止插入模式
q…………停止宏“a”
:%norm@a…….在整个文件中应用马可'a'
:g/^if(!/norm@a在以if开头的行中应用宏“a”。。。

你能给我们解释一下这里发生了什么吗?否则我们在类似情况下会一直依赖你的帮助,不是吗?谢谢你的帮助!你能给我们解释一下这里发生了什么吗?否则我们在类似情况下会一直依赖你的帮助,不是吗?谢谢你的帮助!