在vim字符串替换新行中自动缩进?

rxi*_*xin 5 regex vim

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

%s/my_pattern/\0, \r some_other_text_i_want_to_insert/
Run Code Online (Sandbox Code Playgroud)

问题是,与\rsome_other_text_i_want_to_insert获取新行之后插入:

mycode(
  some_random_text my_pattern
)
Run Code Online (Sandbox Code Playgroud)

会成为

mycode(
   some_random_text my_pattern
some_other_text_i_want_to_insert   <--- this line is NOT indented
)
Run Code Online (Sandbox Code Playgroud)

代替

mycode(
   some_random_text my_pattern
   some_other_text_i_want_to_insert  <--- this line is now indented
)
Run Code Online (Sandbox Code Playgroud)

即新插入的行没有缩进。

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

谢谢。

mea*_*gar 0

这是实现相同目标的一种迂回方式:您可以记录一个宏,该宏查找下一次出现的my_pattern并在其后插入换行符和替换字符串。如果打开自动缩进,则无论在何处找到 ,都将保持缩进级别my_pattern

像这样的按键序列:

q 1                  # begin recording
/my_pattern/e        # find my_pattern, set cursor to end of match
a                    # append
\nsome_other_text... # the text to append
<esc>                # exit insert mode
q                    # stop recording
Run Code Online (Sandbox Code Playgroud)

重复按@1