editing
i / a / o / O
The four ways to enter insert mode: i inserts BEFORE the cursor, a inserts AFTER it, o opens a new line BELOW and enters insert mode, O opens a new line ABOVE. Choosing the right one saves a motion or two before you start typing.
i a o O (normal mode → insert mode) Variations
| Keystroke | Effect |
|---|---|
| I | Insert at first non-blank character of line |
| A | Append at end of line (very common) |
| gi | Re-enter insert mode at the last place you left it |
| 5i-<Esc> | Prefix count: insert '-' five times then leave insert mode → ----- |
Examples
A;<Esc> Jump to end of line, append a semicolon, return to normal mode
o Create a new line below current and start typing
O Create a new line ABOVE and start typing
Gotcha
a puts the cursor AFTER the current character — at end of line, use A instead of a (a on the last char is fine, but A is clearer).
Related
Escape
Leaves the current mode and returns to normal mode. Vim is modal — most of the time you want to be in normal mode; if you're not sure what mode you're in, mash Escape.
r / R
r replaces the single character under the cursor with the next key you press — no need to enter insert mode. Capital R enters REPLACE mode, where every character you type overwrites the one under the cursor until Escape.
dd
Deletes the current line (and stores it in the unnamed register, so you can paste it back with p). The bread-and-butter 'get rid of this line' command.
yy
'Yank' (copy) the current line into the unnamed register — Vim's word for copy. Combine with p/P to duplicate lines without touching your system clipboard.
p / P
Paste the contents of the unnamed register: p pastes AFTER the cursor (or below the line, for a linewise yank), P pastes BEFORE the cursor (or above the line). Works with anything you dd'd, yy'd, or x'd.