Normal and the global command
Sometimes, Ex commands are not quite enough. Like, what if for each of the matches the global command finds, you want to perform some kind of complex editing task?
Vim has the answer: the :normal/:norm command. This is an Ex command that accepts a sequence of characters, and pretends they were typed in normal mode. I mentioned this briefly in an earlier section — let’s take a proper look now.
As a simple example, let’s say you want to append a string to each line that matches with your :global command. You can do something like this
:g/foo/norm Ahello world!
Meaning for each line containing ‘foo’, send the keys Ahello world! to normal mode. This change could be done with a substitution without too much trouble (hint: s/$/hello world!), but for some edits, it’s far easier to just use :norm.
If you want more visibility into what you’re doing, you can record a macro and replay it on each matching line with:
:g/foo/norm @q
… that is, run the q macro on each line that matches.
Let’s try it out. The code in the editor is missing some of its semicolons. The regex ;$ matches the lines that already end with one, so the inverse global command selects the lines that don’t — and norm A; appends a ; to each of them. Work through the checklist below to fix every line in a single command.