Modes and the grammar
vim has modes. Normal mode is where keys move the cursor and run commands, insert mode is where typed text goes into the file, and command mode is where you type a line that starts with a colon. Press Escape to return to normal mode from the other two. The editing grammar works in normal mode: a count, then an operator, then a motion or a text object. The count 3 with the operator d and the motion w means delete three words.
The operators act on a region. d deletes, c deletes and enters insert mode, y yanks into a register, and the greater-than and less-than signs indent. The region comes from a motion, which moves the cursor, or from a text object, which names a unit such as a word, a sentence, or the contents of quotes.
Motions
w moves to the start of the next word, b moves back to the start of the previous word, and e moves to the end of the word. A capital W does the same with words separated by spaces only. The zero key moves to column zero, the dollar sign to the end of the line, and f followed by a character moves to the next occurrence of that character on the same line. The t motion stops just before the character.
Because a motion is a region, you can see what an operator will do before you press it. dw deletes from the cursor up to the start of the next word, and dt followed by a comma deletes up to a comma. The motion is the noun, and the operator is the verb.
Text objects
Text objects are regions with a name. iw is the inner word, aw is the word with trailing space, and i followed by a quote is the text inside a pair of quotes. The difference between i and a is whether the delimiters are included. ci followed by a quote changes the text inside the quotes and leaves the quotes in place, which is often exactly what you want when you edit a string.
Text objects work with any operator. di followed by a parenthesis deletes what is inside the parentheses, and yi followed by a brace yanks the inside of a block. They make edits independent of where the cursor is inside the object.
Batch mode for experiments
vim can run without a terminal, which is useful for scripts and for teaching. The -e and -s options put vim in silent Ex mode, where it reads commands from standard input or from -c options. The -N option turns off compatibility with vi, and -u NONE skips your vimrc, so the result does not depend on your setup. Each -c option runs one command, and wq at the end writes the file and quits.
vim -Nes -u NONE -c "normal! wdw" -c "wq" words.txtThe normal command runs normal-mode keys from an Ex command, and the exclamation mark stops the keys from using any mapping. In batch mode the cursor starts on the last line of the file, which is a surprise the first time. Use gg to jump to the first line before you rely on the cursor position.
In batch mode, normal with insert text ends insert mode for you. In an interactive session, you must press Escape. Forgetting that difference is the usual reason a script hangs or a change is lost.
Practice with a purpose
Pick one motion or object per day and use it on real text until it is automatic. Use the dot command to repeat the last change, which makes an operator plus motion very cheap to repeat. The vim documentation at the project site lists every motion, operator, and object, and the vimtutor program that ships with vim teaches the basics interactively on a real terminal.
Registers and repeating changes
Every delete and yank goes into a register. The unnamed register holds the most recent change, and p puts its contents after the cursor. Named registers, written with a quote followed by a letter, keep several values at once. The dot command repeats the last change, so a dw followed by a few dots deletes several words in a row, and the count before the dot changes how many times the change repeats.
Undo and the file on disk
The u key undoes the last change, and Ctrl-r redoes it. Undo history lives in the session, so it is lost when you quit, unless you enable persistent undo in your configuration. Writing the file with a colon followed by w saves the changes to disk, and a colon followed by q quits. A colon followed by q with an exclamation mark quits without saving, which is the way out of an unwanted change.
Searching and substituting
A slash followed by a pattern searches forward through the file, and n repeats the search. The substitute command in command mode changes text across lines. A form such as substitute with the pattern, the replacement, and the g flag changes every match on each line, and a range before the command limits it to certain lines. Batch mode accepts the same substitute command with the -c option, so an edit can be applied by a script and checked afterward.
Visual selection
The v key starts a character selection and V starts a line selection. Motions extend the selection, and an operator then acts on the whole selected region. Visual mode is the easiest way to see the region an operator will use, and it is worth using while you learn the grammar, because it shows the boundaries before anything changes.