vimtricks.wiki Concise Vim tricks, one at a time.

How do I insert text at the beginning of a line?

Answer

I

Explanation

The I (uppercase) command moves the cursor to the first non-blank character of the current line and enters insert mode. It is the counterpart to A, which appends at the end of the line.

How it works

  • I is equivalent to ^i — it moves to the first non-blank character, then enters insert mode
  • i enters insert mode at the current cursor position
  • I always jumps to the beginning of the meaningful content on the line

Example

Given the text with the cursor somewhere in the middle:

    console.log("hello");

Pressing I moves the cursor to the c of console (the first non-blank character) and enters insert mode. Type // to get:

    // console.log("hello");

The leading whitespace is preserved.

Tips

  • Use A to enter insert mode at the end of the line
  • Use gI to insert at column 1 (the absolute beginning of the line, before any whitespace)
  • In visual block mode (<C-v>), select a column of lines and press I to insert the same text at the beginning of every selected line
  • Use 0i if you specifically want to insert before any leading whitespace instead of after it

Next

How do I edit multiple lines at once using multiple cursors in Vim?