← All lessons

19. The Global Command

Step 6 of 10

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. Try adding a semicolon to the end of all of the lines that don’t currently end with a semicolon.

Loading editor…