Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Modes and Navigation

Vim Modes: Normal, Insert, Replace and Command-line

Master Vim's modes: Normal, Insert, Replace, Command-line and Operator-pending. See how to switch, how Ctrl-o and Ctrl-c help, and practice with a simulator.

Vim has several modes, and fluency starts with knowing which one you are in and how to move between them with the fewest keys. You met the idea in the first chapter. Here we go deeper: every mode, every entry key, and the shortcuts experts use so they hardly ever reach for Esc unnecessarily.

The modes at a glance

flowchart LR
  N["Normal"] -- "i a o I A O" --> I["Insert"]
  I -- "Esc" --> N
  N -- "v V Ctrl-v" --> V["Visual"]
  V -- "Esc" --> N
  N -- ": / ?" --> C["Command-line"]
  C -- "Enter / Esc" --> N
  N -- "R" --> R["Replace"]
  R -- "Esc" --> N
  N -- "d c y ..." --> O["Operator-pending"]
  O -- "motion" --> N

Normal mode is the hub: everything starts and ends there.

Normal mode

The default. Keys move, edit, and start other modes. When you are not typing new text, you should be here. A common beginner mistake is to stay in Insert mode while reading or moving around; try to leave Insert as soon as you finish typing.

Insert mode

Keys insert text. Enter it with i a I A o O, or with s (delete a character and insert), S or cc (replace the line), C (change to end of line). Some useful Insert-mode keys:

Key Effect
Ctrl-w delete the word before the cursor
Ctrl-u delete back to the start of the line
Ctrl-h backspace
Ctrl-r {reg} paste a register (e.g. Ctrl-r 0)
Ctrl-o run one Normal-mode command, then return to Insert
Ctrl-[ same as Esc

Ctrl-[ is worth adopting: it is a physical key combination close to your fingers, and it works in every terminal. Many people also remap Caps Lock to Esc or Ctrl.

Visual mode

Selects text. v selects characters, V whole lines, and Ctrl-v a rectangular block. The next lessons and visual mode cover it in depth.

Command-line mode

Pressing : opens a prompt at the bottom for an ex command; / and ? open it for searching forward and backward. Tab completes names, the up arrow or Ctrl-p recalls history, and Ctrl-f opens the command-line window where you can edit history like a normal buffer. Esc or Ctrl-c cancels.

Replace mode

R enters Replace mode, where typed characters overwrite existing ones. Backspace restores what you overwrote. For a single character use r{char} instead, which does not enter a mode at all.

Operator-pending mode

After you type an operator like d or y, Vim waits for a motion. This is a real (brief) mode; pressing Esc cancels it. That is why d followed by w works: Vim is in Operator-pending mode in between.

Switching with fewer keys

The best Vim users cut down on mode round-trips:

  • A instead of $ then a (append at line end).
  • I instead of ^ then i.
  • o instead of A then Enter.
  • Ctrl-o from Insert mode to run one command without leaving the Insert session. For example Ctrl-o $ jumps to the end of the line and returns to typing.
  • gi goes back to where you last left Insert mode and starts typing.

Ctrl-o is also special for undo: a single Insert session is one undo step, but moving the cursor with arrow keys splits it. Using Ctrl-o to move keeps history unbroken in some cases, but if you want long Insert sessions to be undone together, leave Insert to move.

A mode simulator with Insert shortcuts (stand-in)

The block below extends the earlier idea with Replace, Operator-pending, and Ctrl-o. It is a stand-in model using named keys, not real Vim.

JS Mode transition model (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the output as the path through the diagram. The C-o step shows Vim waiting for one command and then returning to Insert.

The mode indicator and cursor shape

Check the bottom-left for -- INSERT --, -- VISUAL --, -- REPLACE --. You can also change the cursor so each mode looks different. In the terminal that is done with escape sequences and in Neovim with the guicursor option; chapter 6 shows how. A thin bar for Insert and a block for Normal is common.

Pressing keys in order: a worked example

Say you want to turn price = 10 into price = 12 and add a comment. Here is a sequence that stays efficient.

  1. f1 jumps to the 1 (see basic motions).
  2. cw changes the word 10, now in Insert mode; type 12.
  3. A appends at the end; type // updated.
  4. Esc returns to Normal.

Four mode changes happen in a few keystrokes, and each key had one job. That is the target pace.

Here is a final stand-in to count mode switches in a key sequence.

JS Count mode switches (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Insert-mode key runs one Normal command and returns to Insert?

    Ctrl-o executes a single Normal-mode command and then you are back in Insert mode.
  2. 2. What is Replace mode used for?

    R makes typed characters overwrite existing ones; for one character use r.
  3. 3. Which key is a handy alternative to Esc that sits near the home row?

    Ctrl-[ is equivalent to Esc.
  4. 4. What is Operator-pending mode?

    After you type an operator, Vim waits for the motion or text object that completes the command.

Summary

  • Modes: Normal, Insert, Visual, Command-line, Replace and the brief Operator-pending.
  • Return to Normal with Esc or Ctrl-[; do not linger in Insert.
  • A I o O gi save mode round-trips; Ctrl-o runs one command from Insert.
  • Insert-mode Ctrl-w, Ctrl-u and Ctrl-r edit and paste without leaving the mode.
  • Use the mode indicator to know where you are.

Next: Basic Motions: Characters, Words and Characters on a Line