I would like to search for a pattern in vim, and on each line where it occurs, add text to the end of the line. For example, if the search pattern is print(
and the text to add is )
:
from __future__ import print_function
print('Pausing 30 seconds...'
print("That's not a valid year!"
should become
from __future import print_function
print('Pausing 30 seconds...')
print("That's not a valid year!")
this command should do it for you:
:g/print(/norm! A)
what it does:
:g/print(/ "find all lines matching the regex
norm! A) "do norm command, append a ")" at the end of the matched line.
you may want to check
:h :g
for details.
To add text to the end of a line that begins with a certain string, try:
:g/^print(/s/$/)
See: Power of g - Examples for further explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With