Two editors, one buffer
◈ 10 cardsThe file-versus-buffer distinction, the vi command grammar that makes vi learnable rather than memorisable, and the same five edits translated into Emacs.
A file is on disk; a buffer is in memory
This is the idea both editors are built on, and almost every confusion about editing dissolves once you hold it.
A file is a sequence of bytes on disk. When you open it, the editor reads a copy into memory — the buffer — and every keystroke you make edits the copy. The file on disk is untouched until you explicitly save, at which point the editor writes a new byte sequence over the old one. Undo, crash recovery, quit without saving, and why did my change vanish are all consequences of that one fact.
Emacs makes the point unmissable by having buffers that were never files at all: *Messages*, *Help*, the output of a compilation. A buffer is simply a workspace with a name; being connected to a file is optional.
Modal versus modeless
vi starts in Command mode, where every key is a command: j moves down, d starts a delete, x removes a character. You cannot type text until you enter Insert mode, and you return to Command mode with Esc. Typing :, / or ? puts you in Last-Line mode (also called ex mode), where you type a whole line — a substitution, a write, a quit — and press Enter.
Emacs is modeless: printable keys always insert themselves, and commands are issued with modifier keys — Ctrl and Meta (which on most keyboards is Alt). Emacs does have modes, but they are a different thing: a major mode (C, Python, Lisp, Fundamental) changes indentation and syntax behaviour for that buffer, and minor modes layer options on top. They change how the editor behaves; they do not change what your printable keys mean.
The vi grammar — the one rule worth the whole chapter
A vi editing command has the shape
[count1] operation [count2] target
The operation is the verb (d delete, c change, y yank/copy). The target is a motion (w a word, $ to end of line, G to end of file, } to the next paragraph). The counts multiply.
So dw deletes a word, 3dw deletes three, d$ deletes to the end of the line, and dG deletes to the end of the file. And there is one special case that saves an enormous amount of memorising: doubling the operator makes the current line the target — dd deletes this line, yy copies it, cc changes it, and 3dd deletes three lines. Learn the verbs and the motions separately and you can compose commands you were never taught.
Worked example: the same five edits, twice
You have a three-line script and four things to fix.
In vi. Open it; you land in Command mode. i to insert a missing #!/bin/sh at the top, then Esc to return. The cursor is on a line that should not exist at all: dd removes it, and the next two are wrong too, so 3dd would have taken all three at once. A line needs duplicating: yy copies it into the general-purpose buffer, then p puts it back below the cursor (P would put it above). Finally every occurrence of old should read new, throughout the file:
:1,$s/old/new/g
Read that as from line 1 to the last line ($), substitute old with new, globally within each line. Without the trailing g you would change only the first match on each line; without the range you would change only the current line. In vim, % is a shorthand for 1,$. Then :wq writes and quits.
In Emacs, to the identical end state. Open it and just type — no mode to enter. To delete a line, Ctrl-a to its start then Ctrl-k, which kills to the end of the line, and a second Ctrl-k to take the newline itself; killed text goes onto the kill ring. To duplicate a line, kill it and then Ctrl-y (yank) twice — once to put it back and once to place the copy. For the substitution, Meta-% runs query-replace: it prompts for the two strings and then walks the file stopping at each match, where y replaces, n skips, and ! accepts all the rest without asking. Save with Ctrl-x Ctrl-s; leave with Ctrl-x Ctrl-c.
Along the way Emacs uses two more terms you need: the point is the cursor position (strictly, the gap just before the highlighted character), the mark is a second position you set with Ctrl-Space, and the region is everything between them. Ctrl-w kills the region; Meta-w copies it without deleting.
Making it stick
Options set inside a session last only for that session. To keep them, write them to a file the editor reads at start-up: ~/.exrc for vi, ~/.vimrc for vim (an empty ~/.vimrc is already meaningful — it takes vim out of vi-compatible mode), and ~/.emacs for Emacs, whose contents are Emacs Lisp expressions of the form (function argument ...).