Vim is "VI, iMproved," and vi was originally developed as an extension to a line editor called "ex." ex itself was an improvement on ed, which itself came from the even older qed line editor. Line editors were meant for the days of teletypes, where you would edit one line at a time. ex still exists in Vim - more than you know, in fact. Every time you type
: and start typing a command, you're using the ex subsystem of Vim.
It's not strictly necessary to know this, but I think it's good to understand the history involved. And to better understand the tool you're using, and what those arcane commands you're going to be using all the time in Vim actually mean. So today we're going to take a detour into ex.
Open a test file (one you don't care if you mangle) in Vim. In Normal mode, press
Q (capital "q"). Vim will warn you: Entering Ex mode. Type "visual" to go to Normal mode. Remember that.
At your colon prompt, type
$. This will take you to and print the last line of the file. Now go to the first line with
1. If you want more context, print a chunk of the file with
1,5. If you're unsure what line you're on at any time, print the current line with the
p command. You can do a substitution on the current line with
s/i/a/g (which means "wherever you find an 'i', replace it with an 'a'" - the trailing 'g' means "do this globally for all occurrences of the character 'i' on the line"). If you decide that that didn't work out the way you planned, use the
u command to undo the last changes. (Vim has multi-level undo, but ex only remembers one previous operation to undo.) Now delete lines 2 and 3 with the command
2,3d. Write your changes with
w and then exit the file with
This is how files were edited in the 1970s on teletype machines (in the 1980s and 1990s, this form of editing was still to be found in MUDs and MOOs, but that's another story entirely ...). In its original form, this isn't a good way to edit a file: and yet all those commands live on in ex-mode in Vim, and you'll find yourself using them a lot when you start doing regex search-and-replace commands on a file open in Vim.