我正在使用以下命令自动替换某些代码(在现有段之后添加新的代码段)

%s/my_pattern/\0, \r some_other_text_i_want_to_insert/


问题在于,对于\rsome_other_text_i_want_to_insert会在新行之后插入:

mycode(
  some_random_text my_pattern
)


会成为

mycode(
   some_random_text my_pattern
some_other_text_i_want_to_insert   <--- this line is NOT indented
)


代替

mycode(
   some_random_text my_pattern
   some_other_text_i_want_to_insert  <--- this line is now indented
)


也就是说,新插入的行未缩进。

vim或技巧中是否可以使用任何选项来缩进新插入的行?

谢谢。

最佳答案

试试这个:

:let @x="some_other_text_i_want_to_insert\n"
:g/my_pattern/normal "x]p


在这里,逐步介绍:

首先,将要插入的文本放入寄存器中...

:let @x="some_other_text_i_want_to_insert\n"


(请注意字符串末尾的换行符-这很重要。)

接下来,使用:global命令将文本放在每行匹配的行之后...

:g/my_pattern/normal "x]p


]p普通模式命令的工作原理与常规p命令相同(即,将寄存器的内容放在当前行之后),但也会调整缩进以使其匹配。

更多信息:

:help ]p
:help :global
:help :normal

09-04 05:37
查看更多