My recent favorite vim trick is g + norm. Look in
:h g and
:h norm to see what these do. Basically
g will search all lines in a file for some pattern, and then do something on those lines.
norm will take a string of text as an argument and process that string as though you were pressing the keys in vim yourself, starting in Normal mode.
Combined, you can do some neat things. For example to search for all lines that start with a number, and then add a semi-colon to the end of just those lines:
Note that you need to use
o, or some other way of getting into Insert mode if you want to add text.
One of my favorites (I think I read this on some other site one day) is to find every line containing some text and move those lines to the end of the text file. Sort of an ad-hoc partial way of sorting a file:
:g/some text/norm ddGp
All the vim movement commands work too, so to find every line that has a
", move to the end of the line, move 3 characters back to the left and add a
You can move between lines or add lines to the file too, which makes things interesting. What if you start with this text file:
1 2 3 4 5
And you do this:
Happily this does not blow up into infinite recursion, because vim always makes two passes through the file (one to mark the lines to operate on; a second to actually operate on them); you end up with
1 999 2 999 3 999 4 999 5 999