Entering insert mode the right way

Vim · Essentials · 6 min

Picking the right insert command removes a lot of small cursor movements.

There are several ways into Insert mode, each starting at a different place:

  • i inserts before the cursor, a appends after it.
  • I inserts at the first non-blank character of the line, A appends at the end of the line.
  • o opens a new line below and enters Insert mode, O opens one above.
  • s deletes the character under the cursor and inserts; S (same as cc) clears the whole line and inserts, keeping indentation.

So to add a semicolon at the end of a line, type A;<Esc> from anywhere on the line, rather than $a;<Esc>. That matters more than it looks: as a single command, A; can be repeated on other lines with . (covered in the undo lesson).

While in Insert mode a few keys are handy:

  • Ctrl-w deletes the word before the cursor, Ctrl-u deletes back to the start of the insert.
  • Ctrl-n and Ctrl-p complete words that appear in open buffers.
  • Ctrl-o runs one Normal-mode command and comes straight back to Insert mode.

A count also works: 3ihey <Esc> inserts hey hey hey . Leave Insert mode as soon as you are done typing; Normal mode is the home base.

Example

A;<Esc>         " append ';' at end of line
I// <Esc>       " comment out: insert '// ' at first non-blank
o<Esc>          " add an empty line below
Oimport os<Esc> " add a line above
3i-<Esc>        " insert '---'
S               " wipe line contents (keeps indent) and insert

Remember

  • i/a = before/after cursor, I/A = start/end of line, o/O = new line below/above
  • A single insert command can be repeated with .
  • Ctrl-w deletes the previous word while typing

Check yourself

  1. You want to add `)` at the end of the current line, wherever the cursor is. Which is best?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/insert.txt.html