Modes: The Mental Model That Makes Vim Make Sense
This chapter teaches you the modal model: normal, insert, visual, command-line, operator-pending, and replace. Get this right and everything else in Vim stops feeling arbitrary.
Why Modes
Most editors are single-mode. Every key you press either types a character or, with a modifier, runs a command. Vim splits these jobs into separate modes. In normal mode, d deletes. In insert mode, d types the letter d.
The benefit is that commands are short. In a single-mode editor, "delete the next word" is Ctrl+Shift+Right, Delete. In Vim, it is dw. Two keys. No modifier. This matters because you change text far more often than you type new text, and your fingers stay on the home row.
Normal Mode
Normal mode is home. You start here. You come back here after almost every action.
In normal mode:
- Letters are commands
- You move around with
h,j,k,l, and friends - You delete, change, yank, and paste
- You enter other modes intentionally
If you are unsure what mode you are in, press <Esc>. You are now in normal mode. You can press <Esc> as many times as you like. Vim just beeps the second time and ignores the rest. No harm done.
Some terminals treat <Esc> as flaky. Two alternatives that always work:
<C-[> same as Esc, never flaky
<C-c> also exits most states, with minor differences
I use <C-[> by habit. Your call.
Insert Mode
Insert mode is where typing puts characters into the buffer. You enter it from normal mode with one of:
i insert before the cursor
a insert after the cursor (append)
I insert at the first non-blank character of the line
A insert at the end of the line
o open a new line below and enter insert mode
O open a new line above and enter insert mode
gi resume insert mode at the last place you left it
All six of the first group are worth memorising. A and o are the two you'll reach for most.
The status line shows -- INSERT -- while you're in insert mode. That is your visible reminder.
Leave insert mode with <Esc> or <C-[>. Back to normal mode.
Editing inside insert mode
You mostly want to leave insert mode for anything beyond typing. A few shortcuts are worth knowing:
<C-w> delete the previous word
<C-u> delete back to the start of the line
<C-h> backspace (same as <BS>)
<C-o> run one normal-mode command, then come back
<C-o> is the interesting one. Say you're in insert mode and want to jump to the top of the file, paste something, then keep typing:
<C-o>gg in insert mode, jump to the top, stay in insert mode
Use it sparingly. If you're reaching for <C-o> often, just leave insert mode.
Visual Mode
Visual mode selects a region so you can operate on it. Three flavours:
v charwise visual. Select characters
V linewise visual. Select whole lines
<C-v> blockwise visual. Select a rectangle
Once you have a selection, apply an operator:
d delete
c change
y yank
> indent
< outdent
~ swap case
So Vjjd selects three lines and deletes them. vi(d is a shortcut for "delete inside the parentheses you're in". More on that in chapter 5.
Visual mode is a teaching crutch. Once you've internalised operators and motions, you'll use it less. dap deletes a paragraph without selecting first. But selecting first is fine while you build the habits.
gv re-selects your last visual region. Handy when you applied the wrong operator.
Command-Line Mode
Command-line mode runs ex commands, the longer-form commands typed at the bottom of the screen:
: start a command
/ search forward
? search backward
Everything you typed : before counts: :w, :q, :help, :substitute, :global, and so on.
You enter command-line mode from normal mode, not insert mode. If : appears as a character in your file, you are in insert mode. Press <Esc> first.
Search is a mode too. After /pattern<Enter>, you are back in normal mode, with the cursor on the first match. Press n for the next match, N for the previous.
Command-line history
Pressing q: opens the command-line window, a special buffer showing every command you have typed this session. Press <Enter> on any line to run it again. Press :q to close the window. Treat it as a shell history for Vim.
Operator-Pending Mode
You will never see this mode advertised. It is real and important.
When you press d in normal mode, Vim enters operator-pending mode and waits for a motion. If you press w, it deletes to the next word. If you press $, it deletes to the end of the line. If you press i(, it deletes inside the enclosing parentheses.
d operator-pending: waiting for a motion
dw deletes a word
d$ deletes to end of line
dG deletes to end of file
Understanding that d puts Vim into a second mode, waiting for more, is the single mental shift that makes the operator-motion grammar obvious.
Replace Mode
Replace mode overwrites characters as you type. Enter with R:
R enter replace mode
<Esc> leave
The status line shows -- REPLACE --. Every key you type overwrites the character under the cursor and advances.
A lighter version is r, which replaces a single character and returns to normal mode:
ra replace the character under the cursor with 'a'
Replace mode is rare in practice. r is common for fixing single-character typos.
Switching Modes: The Map
Here is every transition you need:
Normal → Insert i, a, I, A, o, O, gi
Normal → Visual v, V, <C-v>
Normal → Command :, /, ?
Normal → Replace R, r
Insert → Normal <Esc>, <C-[>
Visual → Normal <Esc>, <C-[>
Visual → Operator d, c, y, >, <, =, ~, u, U
Command → Normal <Esc> or run the command
Replace → Normal <Esc>
You can also go mode-to-mode indirectly. Press <Esc> first if in doubt.
Knowing What Mode You're In
Four clues:
- The status line shows
-- INSERT --,-- VISUAL --,-- REPLACE --, or nothing (normal) - The cursor shape changes in most terminals (block in normal, bar in insert)
- In command-line mode, a
:,/, or?appears at the bottom - If confused, press
<Esc>. You are now in normal mode
Turn on the showmode option (it's default in most setups) and Vim will always tell you:
:set showmode
Common Pitfalls
"I typed a command but got letters in my file." You were in insert mode. Press <Esc>, undo with u until the file is clean, then try again from normal mode.
"Escape feels slow or doesn't register." Some terminals add a delay. Use <C-[> instead. You can also set ttimeoutlen=10 in your .vimrc to speed it up.
"I pressed capital letters and don't know what I did." Capital letters are real commands. U undoes a whole line's worth of changes. D deletes to the end of the line. X deletes backwards. If you made a mess, u and keep pressing until you're back to safety.
"I selected text in visual mode and can't figure out how to leave." <Esc> leaves. Any operator also leaves (after applying).
Next Steps
Continue to 03-navigation.md to learn the motions that let you stop touching the arrow keys.