Useful Ex commands to use with the global command
Printing with the :print/:p command is the default action for the global command. If you don’t specify an Ex command, it’s assumed that you mean :print.
We can do a lot more than just print, though. Here are some useful Ex commands that you can use in conjunction with :global:
:delete/:d— used to delete a line — for example,:g/foo/dwill delete all lines matchingfoo:move/:m— can be used to move a line to a particular address — for example,:g/foo/m 0will move all lines matchingfooto the start of the file:copy/:t— can be used to copy a line to a particular address — for example,:g/foo/t 0will move all lines matchingfooto the start of the file:norm ...— can be used to execute some normal mode commands on a line — for example,:g/foo/norm Abarwill appendbarto all lines matchingfoo
… plus many more.
Any Ex command can be used in conjunction with the global command, so feel free to use your imagination. In my experience, though, the ones listed above are the most useful.
Let’s try this in the editor. Use the global command to delete all of the lines matching foo with :g/foo/d.