DEV Community

Igor Irianto
Igor Irianto

Posted on • Updated on

The Only Vim Insert-Mode Cheatsheet You Ever Needed

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

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To insert content from register a:

Ctrl-r a
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Vim has an expression register, =, to evaluate expressions.

You can evaluate mathematical expressions 1 + 1 with:

Ctrl-r =1+1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Another way to get values from registers is with @ followed by register symbol.

Ctrl-r =@a
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

You can evaluate its value just by calling it.

Ctrl-r =HelloFunc()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

You can also autocomplete without using Ctrl-x with:

Ctrl-n
Ctrl-p
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Repeating text

Ctrl-o 100ihello
Ctrl-o 10Ahello
Enter fullscreen mode Exit fullscreen mode

Executing command line

Ctrl-o !! curl https://google.com
Ctrl-o !! pwd
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
v6 profile image
🦄N B🛡 • Edited

Ctrl-[ " exits insert mode and go to normal mode

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.

Ctrl-o 100ihello
Ctrl-o 10Ahello

These didn't work.

Also, the commands you posted were not able to pull content directly from theregister please help.

Collapse
 
iggredible profile image
Igor Irianto

Ctrl-o 100ihello
Ctrl-o 10Ahello
These didn't work.

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).
repeat

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.

register

Collapse
 
rj722 profile image
Rahul Jha

Amazing article there @iggredible !

BDW, how did you get these key events in the GIF?

Thread Thread
 
iggredible profile image
Igor Irianto • Edited

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!

Collapse
 
snawaz profile image
Sarfaraz Nawaz • Edited

Awesome article. I wish I knew o and O 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.

Collapse
 
snawaz profile image
Sarfaraz Nawaz

ah, I can do this instead:

" insert an empty line without leaving normal mode
nnoremap <leader>o o<Esc>     
nnoremap <leader>O O<Esc>
Collapse
 
iggredible profile image
Igor Irianto • Edited

I don't think it exists. But luckily, we can always map it if it doesn't exist. Something like:

:nnoremap <leader>o o<esc>

You can change <leader>o to whatever mapping you want :)

EDIT: apparently we posted (partially) exact same mapping within 5 minutes haha!!

Collapse
 
snawaz profile image
Sarfaraz Nawaz

Eventually, I used this instead:

nnoremap <silent> oo :<C-u>call append(line("."),   repeat([""], v:count1))<CR>
nnoremap <silent> OO :<C-u>call append(line(".")-1, repeat([""], v:count1))<CR>  
Enter fullscreen mode Exit fullscreen mode

It has 3 advantages:

  • first) I dont have to hit two different keys, instead press the same key twice (quickly though),
  • second) the cursor stays on the same line as it was before.
  • third) I can insert more than one lines like this: 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!

Collapse
 
voyeg3r profile image
Sérgio Araújo

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...

Collapse
 
midenok profile image
Aleksey Midenkov

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.