Reputation: 430
I have the following paragraph:
1 sometexthere
2 indented text
3 indented text
4 indented text
I would like to use vim's search and replace command to add some text right before indented text. For example:
1 sometexthere
2 test: indented text
3 test: indented text
4 test: indented text
Is there a way to use vim's search and replace syntax to achieve the following results? I have tried commands like
2,4s/^/test: /
but still can't figure out a way to strip off the whitespace.
Upvotes: 0
Views: 229
Reputation: 15081
Matching leading whitespace and replacing it by itself:
%s/^\s\+/&test: /
Or matching lines by :global
and editing them by :normal
g/^\s/normal! Itest:
Upvotes: 3
Reputation: 2236
Unless I am misunderstanding you question you can achieve this fairly simply like:
%s/ /\ test:/g
This gives the output you desire from the given input. For this though, i prefer to use a macro.
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
Then to run this macro on the next indent go @q
. Just repeat @q
or @@
to keep running the macro until everything is indented.
Another alternative route, which is fairly readable, is to use the normal command.
/____ # Search for the indent.
:'<,'> normal 0nitest: # Inserts "test:" 4 characters right of the search result. you can replace the <,> with a range of course.
Upvotes: 2