Chapter 9.Table of ContentsCheatsheet

Inserting Text a la Vim

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 i and c commands but let’s dive into this mode some more.

There’s two core commands that put you into Insert mode:

  • i for insert and,
  • a for append.

The 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 i and a have uppercase counterparts that do stronger versions of inserting and appending:

  • I puts you in Insert mode at the beginning of the current line whilst,
  • A puts you in Insert mode at the end.
Insert mode commands

In addition to i and a, there are another three super useful commands that I love to use to drop into Insert mode:

  1. o inserts a new line below the current one and drops you into Insert mode (mnemonic open a line below)
  2. O inserts a new line above the current one and also drops you into Insert mode
  3. gi puts you into Insert mode at the last place you made a change. This is great if you drop from Insert mode by mistake and want to go back where you were and continue typing. (This behavior is slightly different to Vim where gi sends you to the last place you left Insert mode).
More Insert mode commands

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-h lets you delete the last character you typed
  • CTRL-w lets you delete the last word you typed
  • CTRL-u lets 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: <ESC>, CTRL-[ and 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.

Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García