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
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
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 thewhichwrap
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 the 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
- if you have typed some characters in an existing line, this will delete characters till the starting point of the modification
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
If more than one word matches, they are displayed using a popup menu. You can take further action using the following options:
- ↑ and ↓ move up and down the list, but doesn't change the autocompleted text
- Ctrl+p and Ctrl+n move up and down the list as well as change the autocompleted text to that particular selection
- Ctrl+y confirm the current selection (the popup menu disappears)
- you can also use the Enter key for confirmation if you have used the arrow keys to move through the popup list
Typing any character will make the popup menu disappear and insert whatever character you had typed.
Autocomplete line
- Ctrl+x followed by Ctrl+l autocomplete line based on matching lines in the backward direction
If more than one line matches, they are displayed using a popup menu. In addition to the options seen in the previous section, 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
See :h ins-completion for more details and other autocomplete features. See :h 'complete' setting for customizing autocomplete commands.
Execute a 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
- Ctrl+o followed by ce clear till the end of the word
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
Cursor can be anywhere in the line for the above features. 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
- Ctrl+r followed by =12+1012 and then Enter key inserts
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 commandCTRL-R CTRL-R {register}
.
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+v followed by Esc gives
- Ctrl+q alias for Ctrl+v, helps if it is mapped to do something else
You'll see a practical usage of this command in the 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
¥
- Ctrl+k followed by Ye gives
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.