Vim: finding patterns and adding text to the end of each line where this happens

I would like to find a template in vim, and in each line where this happens add text to the end of the line. For example, if the search template print(, and the text to add - ):

from __future__ import print_function
print('Pausing 30 seconds...'
print("That not a valid year!"

should become

from __future import print_function
print('Pausing 30 seconds...')
print("That not a valid year!")
+4
source share
2 answers

this command should do it for you:

:g/print(/norm! A)

what does he do:

:g/print(/   "find all lines matching the regex
norm! A)     "do norm command, append a ")" at the end of the matched line.

you can check

:h :g

for details.

+11
source

To add text to the end of a line starting with a specific line, try:

:g/^print(/s/$/)

See: Strength g - Examples for further explanation.

+1
source

Source: https://habr.com/ru/post/1502915/


All Articles