Advanced Insertion Commands and Shortcuts
Here is a quick reference for advanced insertion, formatting, and filtering commands/shortcuts.
|g + ? + m||Perform rot13 encoding, on movement m|
|n + ctrl + a||+n to number under cursor|
|n + ctrl + x||-n to number under cursor|
|g + q+ m||Format lines of movement m to fixed width|
|:rce w||Center lines in range r to width w|
|:rle i||Left align lines in range r with indent i|
|:rri w||Right align lines in range r to width w|
|!mc||Filter lines of movement m through command c|
|n!!c||Filter n lines through command c|
|:r!c||Filter range r lines through command c|
Different ways to get into insert mode
|Append text following current cursor position|
|Append text at the end of current line|
|Insert text before the current cursor position|
|Insert text before first non-blank character of current line|
|Insert text in first column of cursor line|
|Insert text at same position where it was left last time in Insert mode|
|Open up a new line above the current line and add text there (CAPITAL |
|Open up a new line below the current line and add text there (lowercase |
|Delete character under the cursor and switch to insert mode|
|Delete entire line and switch to Insert mode|
|Delete from the cursor position to the end of the line and start insert mode|
Disable auto-indent to paste code
When pasting text through a terminal emulator, the auto-indent feature may destroy the indentation of the pasted text.
will be pasted as:
In these cases, use the
nopaste option to disable / enable the auto-indent feature:
Adding to this, there is a simpler approach to the problem: Add the following line in your .vimrc:
And if you want to paste as is from the clipboard. Just press
insert mode, and paste. Press
F3 again to exit from the
Insert mode shortcuts
|Delete word before cursor|
|Indent current line with by one |
|Unindent current line with by one |
|reindent the line, (move cursor to auto indent position)|
|Insert previously inserted text|
|Insert the character below|
|Delete one character backward|
|Insert the character above|
|Insert the content of |
|Next autocomplete option for the current word|
|Previous autocomplete option for the current word|
|Insert a character by its ASCII value in decimal|
|Insert a character by its ASCII value in hexadecimal|
|Insert a character by its unicode value in hexadecimal|
|Enter a digraph|
Insert text into multiple lines at once
Press Ctrl + v to enter into visual block mode.
Use ↑ / ↓ / j / k to select multiple lines.
Press Shift + i and start typing what you want.
After you press Esc, the text will be inserted into all the lines you selected.
Remember that Ctrl+c is not 100% equivalent to Esc and will not work in this situation!
There are slight variations of Shift + i that you can press while in visual block mode:
|Delete selected block and enter insert mode|
|C||Delete selected lines (from cursor until end) and enter insert mode|
|R||Delete selected lines and enter insert mode|
|A||Append to selected lines, with the column at the end of the first line|
Also note that pressing . after a visual block operation will repeat that operation where the cursor is!
Leaving insert mode
|Leaves insert mode, triggers autocommands and abbreviations|
|Exact synonymous of |
|Leaves insert mode, doesn't trigger autocommands|
Some people like to use a relatively uncommon pair of characters like
jk as shortcut for
<C-[> which can be hard to reach on some keyboards:
Paste text using terminal "paste" command
If you use the paste command from your terminal emulator program, Vim will interpret the stream of characters as if they were typed. That will cause all kind of undesirable effects, particularly bad indendation.
To fix that, from command mode:
Then move on to insert mode, with i, for example. Notice the mode is now
-- INSERT (paste) --. Now paste with your terminal emulator command, or with the mouse. When finished go to command mode, with Esc and run:
There is a simpler way, when one wants to paste just once. Put this in your .vimrc (or use the plugin unimpaired.vim):
Now one can simply press
yo to paste code under the cursor, and then
<Esc> to go back to normal/nopaste mode.
Pasting from a register while in insert mode
While in insert mode, you can use
<C-r> to paste from a register, which is specified by the next keystroke.
<C-r>" for example pastes from the unnamed (
Running normal commands from insert mode
While in insert mode, press
<C-o> to temporarily leave insert mode and execute a one-off normal command.
<C-o>2w jumps to the second word to the left and returns to insert mode.
Note: Repeating with
. will only repeat the actions from returning to insert mode
This allows for some useful mappings, e.g.
Now ctrl+a will put the cursor to the beginning of the line and ctrl+e - to the end of line. These mappings are used by default in
readline, so might be useful for people who want consistency.