grep is named after an ed command: g/re/p. Globally, match a regular expression, print. The command never left. It lives in every vi you will ever sit in front of, and it is the most underused thing in the editor, mostly because people learned dd and stopped.
Everything below works in the vi that ships with FreeBSD (nvi) and in traditional vi. I am not using anything that needs vim, because I am not installing vim to edit a file that is 40 lines long.
The shape of it
:[range]g/pattern/command
For every line in the range (default: the whole file) matching the pattern, run the ex command. With no command it defaults to p, which is why :g/pattern/ alone behaves like grep. :v/pattern/command, or :g!/pattern/command, runs it on the lines that do not match.
It makes two passes. First it marks every matching line, then it runs the command on each mark. That is why deleting lines inside a global does not skip neighbours.
The five I actually use
:g/^#/d delete every comment line
:g/^$/d delete blank lines
:v/ERROR/d keep only lines containing ERROR
:g/foo/s//bar/g substitute on matching lines only
:g/^/m0 reverse the file
Some notes on those.
:v/ERROR/d is the one that earns its keep when you have pasted a log into a buffer and want only the interesting part. It is grep ERROR done in place.
:g/foo/s//bar/g works because an empty pattern in :s reuses the last regular expression, which the global just set. You type the pattern once.
:g/^/m0 moves each line, one at a time, to just after line zero, i.e. to the top. Each move puts the next line above the previous one, so the file ends up reversed. It is a lovely trick and you will use it twice in your life, but both times it will save you a script.
Joining and duplicating
:g/\\$/j join lines ending in a backslash with the next one
:g/pattern/t. duplicate each matching line below itself
:g/pattern/t$ copy each matching line to the end of the file
t is copy (“transfer”), m is move, . is the current line and $ is the last. :g/^server /t$ followed by editing the copies is a faster way to build a second stanza than anything involving the mouse.
Filtering a range through the shell
The other half of the trick is !. A range followed by !command sends those lines to the command’s stdin and replaces them with its stdout.
:%!sort -u sort and dedupe the whole buffer
:.,+9!fmt -w 72 reflow ten lines starting here
:/^\[mysqld\]/,/^$/!sort
The last one sorts one config stanza, from the [mysqld] header to the next blank line, and leaves the rest of the file alone. In normal mode !! filters the current line and !} filters to the end of the paragraph. :r !date reads a command’s output into the buffer below the cursor, which is how I stamp changelog entries.
Combine the two when you need to. :g/^server /.,/^}/!sort runs a filter per block, though think before you do that on a file you care about; a filter that sorts the wrong range is faster than your reflexes.
Undo is one keystroke
A global command is undone as a single unit. Run :g/^#/d, look at the result, notice that your pattern also ate the shebang line, and press u. The whole thing comes back. This is the reason I am comfortable running destructive globals in a file I have not backed up, which is also a bad habit, so keep the backup. For files that matter, cp file file.orig costs one second and the rsync post covers the rest of the paranoia.
Where it bites
- Patterns are basic regular expressions by default.
+,?and|mean literal characters unless your vi hasextendedset, and this is the bug in nine of ten “my global did nothing” reports. :g/pattern/normal ...is a vim feature. nvi does not have:normal. If a command you copied from the internet uses it, it will fail on FreeBSD’s vi with an error, which is at least honest.- A filter that fails mid-way can replace your range with an error message. Check what came back before you write the file. This is the same lesson as pipeline exit status: the shell will happily hand you the output of a command that died.
- It is regex on lines. Anything multi-line is the point where you should stop and reach for
awk, which I covered in associative arrays.
I will admit that I was suspicious of every editing trick that was not in the first chapter of the manual. Then I spent a week cleaning other people’s configs with :v and :g and never opened a second tool. Annoying. It was right there the whole time, in a program older than most of the people reading this.
More of this sort of thing sits in the Unix shell and tooling hub.
