Vim commands worth knowing

These notes grew out of the Editors (Vim) lecture from MIT Missing Semester. Changing a short piece of text makes it easier to see how movement and editing fit together than memorizing commands one at a time. The examples start with one two, then move through navigating and editing it.

Entering text and saving a file

Open a practice file from your terminal:

Open a practice file

vim practice.txt

Vim opens files in Normal mode, where keys are commands for moving and editing rather than text to insert. Press i to enter Insert mode before typing.

  1. Press i and type one two.
  2. Press Esc to return to Normal mode.
  3. Type :w and press Enter to save.

Think of i as starting text entry and Esc as ending it. In the examples below, <Esc> and <Enter> mean those keys; do not type the angle brackets.

To save and quit, use :wq<Enter>. Use :q<Enter> to quit when there are no unsaved changes, or :q!<Enter> to discard unsaved changes and quit. Leave the file open for now if you want to follow the next examples.

Moving to the next word

Press Esc, then 0 to move to the start of the line. In one two, the cursor will be on the first o. Press w to move to the t at the start of two. The brackets below mark the cursor position; they are not part of the file.

Move with w

[o]ne two  →  one [t]wo

One w takes you to the next word instead of several presses of an arrow key. Use e to move to the end of the current word, or b to move back toward the start of a word.

Deleting the range you would move across

Press 0 to return to the first o, then press d followed by w. These are separate keystrokes, not a shortcut you hold down together. dw deletes from the cursor to just before the next word, removing one and the space after it.

Delete with dw

one two  →  two

On its own, w moves the cursor. With the delete operator d in front, it deletes that range instead. A movement can double as an editing range. The same applies to $, which moves to the end of the line: d$ deletes from the cursor to the end. The lecture explains this under Vim’s interface is a programming language.

Replacing what you deleted

Press u to undo the deletion and get one two back. Use 0 to put the cursor on the first o, then press c followed by w. This deletes one and switches to Insert mode. Type new, then press Esc:

Change with cw

one two  →  new two

Written as a sequence, that is cwnew<Esc>. When used on a word, cw changes through the end of that word, so it leaves the following space in place, unlike dw above. d deletes and stays in Normal mode; c lets you type a replacement immediately.

To change text inside parentheses, use ci(. With the cursor somewhere in old in call(old), pressing ci( removes old but keeps the parentheses. Type new and press Esc to get call(new). Here, c means change, and i( specifies the text inside the parentheses.

A reference for later

As in the examples above, start movement and editing commands in Normal mode. In these tables, {motion} is a placeholder for a motion such as w or $, not text to type literally. Replace {char} with the character you want to find.

Modes

Mode Key to enter it What it does
Normal <Esc> Move the cursor and run editing commands
Insert i Enter text
Visual v / V (whole lines) Select a range to edit
Command-line : Enter save, quit, and substitution commands

Movement

Command Destination Example
h / j / k / l Left / down / up / right 3j moves down three lines
w / b / e Start of the next word / start of the previous word / end of a word From the first o in one two, w moves to the t in two
0 / ^ / $ Start of the line / first nonblank character / end of the line On an indented line, 0 reaches the first space and ^ reaches the first nonblank character
gg / G Start / end of the file G moves to the last line
Ctrl-u / Ctrl-d Half a screen up / down Ctrl-d scrolls down half a screen
f{char} Next occurrence of {char} on the current line From the c in call(arg), f( moves to the opening parenthesis
/pattern Search for a pattern; n repeats in the same direction, N in the opposite direction Type /TODO, press <Enter>, then use n for the next match

Editing

Command Action Example
i / a Start inserting before / after the cursor On the b in abc, iX<Esc> produces aXbc
o / O Open a line below / above the current line and enter Insert mode ohello<Esc> adds hello on a new line below
x Delete one character On the b in abc, x produces ac
d{motion} Delete a range; dd deletes the current line On the first o in one two, dw leaves two
c{motion} Delete a range and enter Insert mode On the first o in one two, cwnew<Esc> produces new two
y{motion} Copy a range; use p to paste yy followed by p duplicates the current line below it
u / Ctrl-r Undo / redo Restore a character deleted with x using u, then delete it again with Ctrl-r

Replacing repeated text

When a word appears in several places, use the substitution command :s instead of moving to each occurrence. For example, on a line containing old old, entering :s/old/new/ and pressing Enter produces new old. By default, it replaces only the first match on the current line.

Search and replace

:s/old/new/
:s/old/new/g
:%s/old/new/g

Adding g replaces every occurrence of old on the current line. Adding % at the beginning applies it throughout the file. The second command turns the example line into new new; the third also replaces old on other lines.

Settings and practice

If you are new to Vim, run vimtutor in a terminal to practice the basics. Put your settings in ~/.vimrc. For example, set incsearch shows search matches while you type the pattern. The settings I tried while following the lecture are in my vimrc, with comments.