vim 将文本添加到多行的开头,不包括白色 space 并进行替换
vim add text to beginning of multiple lines excluding white space with substitution
我有以下段落:
1 sometexthere
2 indented text
3 indented text
4 indented text
我想使用 vim 的搜索和替换命令在缩进文本之前添加一些文本。例如:
1 sometexthere
2 test: indented text
3 test: indented text
4 test: indented text
有没有办法使用vim的搜索和替换语法来实现以下结果?我试过像这样的命令
2,4s/^/test: /
但还是想不出去除空格的方法。
除非我误解了你的问题,否则你可以相当简单地实现这一目标:
%s/ /\ test:/g
这会根据给定的输入给出您想要的输出。
不过,为此,我更喜欢使用宏。
qq # Begin recording the macro
/____ # Search for four spaces
4li # Insert four characters to the right
test: # Type the desired text
<ESC>q # Exit insert mode and save the macro to the q register
然后到 运行 这个宏的下一个缩进去 @q
。只需重复 @q
或 @@
以保持 运行 宏直到所有内容都缩进。
另一种可读性很强的替代方法是使用普通命令。
/____ # Search for the indent.
:'<,'> normal 0nitest: # Inserts "test:" 4 characters right of the search result. you can replace the <,> with a range of course.
匹配前导空格并自行替换它:
%s/^\s\+/&test: /
或按 :global
匹配行并按 :normal
编辑它们
g/^\s/normal! Itest:
我有以下段落:
1 sometexthere
2 indented text
3 indented text
4 indented text
我想使用 vim 的搜索和替换命令在缩进文本之前添加一些文本。例如:
1 sometexthere
2 test: indented text
3 test: indented text
4 test: indented text
有没有办法使用vim的搜索和替换语法来实现以下结果?我试过像这样的命令
2,4s/^/test: /
但还是想不出去除空格的方法。
除非我误解了你的问题,否则你可以相当简单地实现这一目标:
%s/ /\ test:/g
这会根据给定的输入给出您想要的输出。 不过,为此,我更喜欢使用宏。
qq # Begin recording the macro
/____ # Search for four spaces
4li # Insert four characters to the right
test: # Type the desired text
<ESC>q # Exit insert mode and save the macro to the q register
然后到 运行 这个宏的下一个缩进去 @q
。只需重复 @q
或 @@
以保持 运行 宏直到所有内容都缩进。
另一种可读性很强的替代方法是使用普通命令。
/____ # Search for the indent.
:'<,'> normal 0nitest: # Inserts "test:" 4 characters right of the search result. you can replace the <,> with a range of course.
匹配前导空格并自行替换它:
%s/^\s\+/&test: /
或按 :global
匹配行并按 :normal
g/^\s/normal! Itest: