The Only Vim Insert-Mode Cheatsheet You Ever Needed
Maybe your like
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
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 modeNotice the lowercase/ uppercase pattern.
Different ways to exit insert modeThere 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 modeAnother common technique is to remap your <caps lock> key to behave like <esc>.
Repeating Insert ModeYou can give a count before you enter insert mode. For example:
10i Enter fullscreen mode Exit fullscreen modeThen 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 modeThere 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 modeBy the way, these shortcuts also work in command-line mode and Ex mode too, not just vim.
Inserting from registerRegisters 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 modeTo insert content from register a:
Ctrl-r a Enter fullscreen mode Exit fullscreen modeVim 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 modeIn 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 modeVim has an expression register, =, to evaluate expressions.
You can evaluate mathematical expressions 1 + 1 with:
Ctrl-r =1+1 Enter fullscreen mode Exit fullscreen modeYou 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 modeAnother way to get values from registers is with @ followed by register symbol.
Ctrl-r =@a Enter fullscreen mode Exit fullscreen modeThis 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 modeYou can evaluate its value just by calling it.
Ctrl-r =HelloFunc() Enter fullscreen mode Exit fullscreen mode ScrollingDid 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 AutocompletionVim 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 modeYou can also autocomplete without using Ctrl-x with:
Ctrl-n Ctrl-p Enter fullscreen mode Exit fullscreen modeTo 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 commandDid 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 modeYou'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 modeRepeating text
Ctrl-o 100ihello Ctrl-o 10Ahello Enter fullscreen mode Exit fullscreen modeExecuting command line
Ctrl-o !! curl https://google.com Ctrl-o !! pwd Enter fullscreen mode Exit fullscreen modeThe possibility is endless.
ConclusionI 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!
Tag » How To Exit Vim Insert Mode
-
Vi Basics
-
Other Ways To Exit Insert Mode Besides Escape
-
Vim Exit From Insert Mode (Example) - Coderwall
-
How To Exit Vim – Vim Save And Quit Command Tutorial
-
Popular Vim Commands - Comprehensive Vim Cheat Sheet - KeyCDN
-
How To Exit Out Of Vim Editor - Linux Hint
-
How To Exit (Quit) Linux Vim/Vi Editor - PhoenixNAP
-
CS107 The Vim Editor - Stanford University
-
How To Exit The Vi Or Vim Editor - How-To Geek
-
Exit Vim - Learn How To Quit Vim Three Different Ways - FactorPad
-
How Do I Switch Between Command And Insert Mode In Vim?
-
How To: Vi / Vim Save And Quit The Editor Command - NixCraft
-
Vim Stuck In Insert Mode - Linux - Super User
-
Custom Shortcut For 'Exit Insert Mode' Exits, But Next Key Gets 'eaten'