Insert mode

This is the mode where the required text is typed. There are also commands available for moving around, deleting, autocompletion, etc.

Documentation links:

  • :h usr_24.txt — overview of the most often used Insert mode commands
  • :h insert.txt — reference manual for Insert and Replace mode

info Recall that you need to add i_ prefix for built-in help on Insert mode commands, for example :h i_CTRL-P.

Motion keys and commands

  • move left by one character within the current line
  • move right by one character within the current line
  • move down by one line
  • move up by one line
  • Ctrl+ and Ctrl+ move to the start of the current/previous and next word respectively
    • From :h word "A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space"
    • you can also use Shift key instead of Ctrl
  • Home move to the start of the line
  • End move to the end of the line
  • PageUp move up by one screen
  • PageDown move down by one screen
  • Ctrl+Home move to the start of the file
  • Ctrl+End move to the end of the file

info You can use the whichwrap setting (ww for short) to allow and arrow keys to cross lines. For example, :set ww+=[,] tells Vim to allow left and right arrow keys to move across lines in Insert mode (+= is used here to preserve existing options for the whichwrap setting).

Deleting

  • Delete delete the character after the cursor
  • Backspace delete the character before the cursor
    • Ctrl+h also deletes the character before the cursor
  • Ctrl+w delete characters before the cursor until start of a word
    • From :h word "A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space"
  • Ctrl+u delete all the characters before the cursor in the current line, preserves indentation if any

Autocomplete word

  • Ctrl+p autocomplete word based on matching words in the backward direction
  • Ctrl+n autocomplete word based on matching words in the forward direction

info If more than one word matches, they are displayed using a popup menu. You can use / arrow keys or Ctrl+p/Ctrl+n to move through this list.

info With multiple matches, you'll notice that the first match is automatically inserted and moving through the list doesn't change the text that was inserted. You'll have to press Ctrl+y or Enter key to choose a different completion text. If you were satisfied with the first match, typing any character will make the popup menu disappear and insert whatever character you had typed. Or press Esc to select the first match and go to Normal mode.

Autocomplete line

  • Ctrl+x followed by Ctrl+l autocomplete line based on matching lines in the backward direction

info If more than one line matches, they are displayed using a popup menu. You can use / arrow keys or Ctrl+p/Ctrl+n to move through this list. You can also use Ctrl+l to move up the list.

Autocomplete assist

  • Ctrl+e cancels autocomplete
    • you'll retain the text you had typed before invoking autocomplete
  • Ctrl+y or Enter change the autocompletion text to the currently selected item from the popup menu

info See :h ins-completion for more details and other autocomplete features. See :h 'complete' setting for customizing autocomplete commands.

Execute single Normal mode command

  • Ctrl+o execute a Normal mode command and return to Insert mode
    • Ctrl+o followed by A moves the cursor to the end of the current line
    • Ctrl+o followed by 3j moves the cursor three lines below

Indenting

  • Ctrl+t indent the current line
  • Ctrl+d unindent the current line
  • 0 followed by Ctrl+d deletes all indentation in the current line

info Indentation depends on the shiftwidth setting. See :h 'shiftwidth' for more details.

Insert register contents

  • Ctrl+r helps to insert the contents of a register
    • Ctrl+r followed by % inserts the current file name
    • Ctrl+r followed by a inserts the content of "a register
  • Ctrl+r followed by = allows you to insert the result of an expression
    • Ctrl+r followed by =12+1012 and then Enter key inserts 1024
    • Ctrl+r followed by =strftime("%Y/%m/%d") and then Enter key inserts the current date, for example 2022/02/02

From :h 24.6:

If the register contains characters such as <BS> or other special characters, they are interpreted as if they had been typed from the keyboard. If you do not want this to happen (you really want the <BS> to be inserted in the text), use the command CTRL-R CTRL-R {register}.

info Registers will be discussed in more details in the Normal mode chapter. See :h usr_41.txt to get started with Vim script.

Insert special characters

  • Ctrl+v helps to insert special keys literally
    • Ctrl+v followed by Esc gives ^[
    • Ctrl+v followed by Enter gives ^M
  • Ctrl+q alias for Ctrl+v, helps if it is mapped to do something else

info You'll see a practical usage of this command in Macro chapter. You can also specify the character using decimal, octal or hexadecimal formats. See :h 24.8 for more details.

Insert digraphs

  • Ctrl+k helps to insert digraphs (two character combinations used to represent a single character, such characters are usually not available on the keyboard)
    • Ctrl+k followed by Ye gives ¥

info You can use :digraphs to get a list of combinations and their respective characters. You can also define your own combinations using the :digraph command. See :h 24.9 for more details.