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
Iis equivalent to^i— it moves to the first non-blank character, then enters insert modeienters insert mode at the current cursor positionIalways 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
Ato enter insert mode at the end of the line - Use
gIto 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 pressIto insert the same text at the beginning of every selected line - Use
0iif you specifically want to insert before any leading whitespace instead of after it