Before you edit a program someone needs to write it and that someone may be you. In Vim, you write code in Insert mode. You’ve seen a little bit of Insert mode when using the
c command but let’s dive into this mode some more.
There’s two core commands that put you into Insert mode:
ifor insert and,
i insert command puts you in insert mode before the cursor. While the
a append command puts you in insert mode after the cursor (as if to append stuff wherever the cursor is placed). From then on you’re in Insert mode and Vim pretty much behaves like any other editor (welcome back VSCode!).
Like with many other Vim commands
a have uppercase counterparts that do stronger versions of inserting and appending:
Iputs you in Insert mode at the beginning of the current line whilst,
Aputs you in Insert mode at the end.
In addition to
a, there are another three super useful commands that I love to use to drop into Insert mode:
oinserts a new line below the current one and drops you into Insert mode (mnemonic open a line below)
Oinserts a new line above the current one and also drops you into Insert mode
giputs you into Insert mode at the last place you left Insert mode. This is great if you drop from Insert mode by mistake and want to go back where you were and continue typing.
Ok. So let’s say that you are now in Insert mode, typing away and you make a mistake (ouch!). Something like a typo. Do you go back to normal mode, fix the typo and go back into insert mode? Please no!
Sometimes. Oftentimes, it is faster to just delete the incorrect letters or words and type again. There’s a couple of key bindings that let you do that right from within Insert mode:
CTRL-hlets you delete the last character you typed
CTRL-wlets you delete the last word you typed
CTRL-ulets you delete the last line you typed
Eventually, you’ll want to exit Insert mode and do other stuff. There are three ways to do this:
CTRL-C. Of all of these, the easiest one to type is
CTRL-C so start by using that one. In later chapters, we’ll learn how you can create a custom mapping that will make leaving Insert mode a breeze.
Written by Jaime González García , Dad, Husband, Front-end software engineer, UX designer, amateur pixel artist, tinkerer and master of the arcane arts. You should follow him on Twitter where he shares useful stuff! (and is funny too).Follow @vintharas