Reputation: 289
I'm trying to edit a config file and want to add a specific line of code (on a newline) whenever a specific string is matched...everything I've searched for does search and replace, I'm looking for search and append...
Upvotes: 16
Views: 18944
Reputation: 195139
personally, i think using ":g" command is probably better than ":s" for this problem.
:g/key/norm owhat ever you want
will make text:
foo
bar
key
foo2
bar2
key2
blah
to:
foo
bar
key
what ever you want
foo2
bar2
key2
what ever you want
blah
if you want to add the new line above the line containing the pattern, just change the small "o" to "O".
Upvotes: 19
Reputation: 3298
This is what I would do:
:g/specific string/s/.*/&specific line of code
The g
command fetches all lines containing specific string
and then apply the s
substitute command to those lines. The s
command substitutes the whole line with its contents plus the specific line of code
HTH
Upvotes: 0
Reputation: 77115
In vim
\r
is the new-line. So you can do something like this
%s/search string/&\rnew code/
Upvotes: 15