Global command in VIM

Posted on Sat 03 January 2009 in Technology • 2 min read

Well you guessed it right, the “global” command (or :g in command mode) will let you select lines that match a particular pattern globally in the file and lets you operate on the selected text. The :v operates similar to grep -v which selects all texts which do not match the pattern.

Syntax: :g/pattern/command where pattern is any regular expression, command is any vim command

Here’s the scenario which taught me the global command:

I have a file with \~29K lines. All lines must begin with “". However somelines are broken into two, in which case the lines which do not start with “" need to be appended to the previous


Sample text:






Should be converted to:




And we solved it this way:

:v/^\\/exe "normal i\<C-H>\<ESC>"

All we did was to execute a backspace(C-H) for all occurences of pattern in normal mode

Let’s end the post with a theorem:

Statement:VI is perfect

Proof: VI in roman numerals is 6. The natural numbers less than 6 which

divide 6 are 1, 2, and 3. 1 + 2 + 3 = 6. So 6 is a perfect number. Therefore, vi

is perfect.

— Arthur Tateishi

And a corollary

VIM in roman numerals might be: (1000 - (5 + 1)) = 994, which happens to be

equal to 2*496+2. 496 is divisible by 1, 2, 4, 8, 16, 31, 62, 124, and 248 and

1+2+4+8+16+31+62+124+248 = 496. So, 496 is a perfect number. Therefore, vim is

twice as perfect as vi, plus a couple extra bits of goodies. :-)

That is, vim is better than perfect.

— Nathan T. Oelger

Have an awesome 2009!