Follow @learnvim for more Vim tips and tricks!
Insert mode is an important mode in vim. I've put together a cheatsheet with 8 tips and tricks to use insert mode more efficiently.
Table of Contents
- Different ways to get into insert mode
- Different ways exiting insert mode
- Repeating insert mode
- Deleting chunks in insert mode
- Inserting from register
- Scrolling
- Autocompletion
- Executing normal mode command
Different ways to get into insert mode
There are different ways to get into insert mode besides i
. Here are some of them:
i " insert text before cursor
I " insert text before the first non-blank character of the line.
a " append text after cursor
A " append text at the end of line
o " starts a new line below cursor and insert text
O " starts a new line above cursor and insert text
gi " insert text in same position where last insert mode was stopped in current buffer
gI " insert text at the start of line (column 1)
Notice the lowercase/ uppercase pattern.
Different ways to exit insert mode
There are several ways to exit insert mode back to normal mode.
<esc> " exits insert mode and go to normal mode
Ctrl-[ " exits insert mode and go to normal mode
Ctrl-c " like Ctrl-[ and <esc>, but doesn't check for abbreviation
Another common technique is to remap your <caps lock>
key to behave like <esc>
.
Repeating Insert Mode
You can give a count before you enter insert mode. For example:
10i
Then if you type "hello world!" and exit insert mode, it will repeat the text "hello world!" 10 times. This works with any methods you use to enter insert mode (10I, 11a, 12o
)
Deleting chunks in insert mode
There are different ways to delete while in insert mode besides <backspace>
:
Ctrl-h " delete one character
Ctrl-w " delete one word
Ctrl-u " delete entire line
By the way, these shortcuts also work in command-line mode and Ex mode too, not just vim.
Inserting from register
Registers are like spaces in memory to store texts. To learn more about registers, check out :h registers
.
Vim can insert contents from any register with Ctrl-r
plus register symbol. You can use any alphabetical character a-z for named register. To use it, if you want to yank a word to register a, you can do:
"ayiw
To insert content from register a:
Ctrl-r a
Vim also has 10 numbered registers (0-9) to save the most recent 10 yanks/deletes. To insert content from numbered register 1:
Ctrl-r 1
In addition to named and numbered registers, here are another useful register tricks:
Ctrl-r " # insert the last yank/delete
Ctrl-r % # insert file name
Ctrl-r / # insert last search term
Ctrl-r : # insert last command line
Vim has an expression register, =
, to evaluate expressions.
You can evaluate mathematical expressions 1 + 1
with:
Ctrl-r =1+1
You can use expression register to get the value of your vim settings. You can do this with setting name prepended with &
. For example, to insert what undolevel
setting you currently have, you can use:
Ctrl-r =&undolevel
Another way to get values from registers is with @
followed by register symbol.
Ctrl-r =@a
This works with any registers mentioned above, including ",/,%
. I think the first method is a little faster, but I just want to show a different way.
You can also evaluate basic vim script expression. Let’s say you have a function
function! HelloFunc()
return "Hello Vim Script!"
endfunction
You can evaluate its value just by calling it.
Ctrl-r =HelloFunc()
Scrolling
Did you know that you can scroll while inside insert mode?
You can use Vim’s Ctrl-x
sub-mode.
Ctrl-x Ctrl-y " scroll up
Ctrl-x Ctrl-e " scroll down
Autocompletion
Vim has a built-in autocompletion. Although it is not as good as intellisense or any other Language Server Protocol (LSP), but for being so lightweight and available right out of the box, Vim’s autocomplete is a very capable feature.
You can use autocomplete feature in insert mode by invoking Ctrl-x
sub-mode.
Ctrl-x Ctrl-l " insert a whole line
Ctrl-x Ctrl-n " insert a text from current file
Ctrl-x Ctrl-i " insert a text from included files
Ctrl-x Ctrl-f " insert a file name
Ctrl-x Ctrl-] " insert from tags (must have tags)
Ctrl-x Ctrl-o " insert from omnicompletion. Filetype specific.
You can also autocomplete without using Ctrl-x
with:
Ctrl-n
Ctrl-p
To move up and down the pop-up window, use Ctrl-n / Ctrl-p
.
Autocomplete is a vast topic in Vim. This is just the tip of the iceberg. To learn more, check out :h ins-completion
.
Executing normal mode command
Did you know Vim can execute normal mode command while inside insert mode?
While in insert mode, if you press:
Ctrl-o
You'll be in insert-normal
sub-mode. If you look at mode indicator on bottom left, normally you will see -- INSERT --
, but Ctrl-o
will change it to -- (insert) --
. You can do one normal mode command. Some things you can do:
Centering and jumping
Ctrl-o zz " center window
Ctrl-o H/M/L " jump to top/middle/bottom window
Ctrl-o 'a " jump to mark 'a'
Repeating text
Ctrl-o 100ihello
Ctrl-o 10Ahello
Executing command line
Ctrl-o !! curl https://google.com
Ctrl-o !! pwd
The possibility is endless.
Conclusion
I think this is a good place to stop. Vim's real strength is in its modal nature. While most editors only have one mode, vim has different modes for different context. Insert mode is one of them.
If you are coming from another text editor/ IDE, it can be tempting to stay in in insert mode. I think spending too much in insert mode is an anti-pattern. Try to get used going to normal mode when pausing.
Vim's insert mode still contains many useful features apart from normal mode. You can repeat phrases, delete chunks, access registers, scroll, autocomplete, and even execute normal mode commands inside this mode. Use these to boost your productivity.
Thanks for reading. Happy vimming!
Top comments (10)
I wonder if this is the reason the Kinesis-2 puts the
[
key in the upper right.I'll just tell myself that's why to help me cope.
These didn't work.
Also, the commands you posted were not able to pull content directly from theregister please help.
Make sure you are running it from insert mode and make have no other plugin / settingblocking it. I tested it by running my vim without plugin, and it works (v8.2).
Ctrl-r also works fine on my machine when I run vim without plugin. My suspicion is that you may have a plugin/ setting that overwrites Ctrl-r commands.
Amazing article there @iggredible !
BDW, how did you get these key events in the GIF?
Thanks Rahul! Appreciate it :)
I used gifox (gifox.io/). It's a paid app, but it is worth it. Very convenient. I use it almost every day!
Awesome article. I wish I knew
o
andO
commands before. Is there any similar commands that inserts an empty line before (and after) and stays in the normal mode. Sometimes while formatting, I just want to insert empty lines while staying in the normal mode.ah, I can do this instead:
I don't think it exists. But luckily, we can always map it if it doesn't exist. Something like:
You can change
<leader>o
to whatever mapping you want :)EDIT: apparently we posted (partially) exact same mapping within 5 minutes haha!!
Eventually, I used this instead:
It has 3 advantages:
10oo
which inserts 10 lines after the current one!Not my solution. I just copied it from here: vi.stackexchange.com/a/3881/21711 .. That's a great answer!
I think the only thing missing here is how to insert registers literally, for this a suggest these two great posts:
1 - vimcasts.org/episodes/pasting-from...
2 - thoughtbot.com/blog/how-to-edit-an...
Strange there is no exit insert mode and abort all changes done in insert mode (or did I miss something). I'd expect such function from multi-modal design.