VIM Basics Handbook

In 10x and gitFebruary 12, 20214 min read

I remember the first time that I had to "deal" with VIM. I was going to merge my first PR when after rebasing, I got this:

I had no idea what to do with this, so I google how to close this screen to keep working. This ended up in my first commit, a commit with a terrible message. 😅

Time has passed since then. My skills have improved a lot, but I still get the same feeling every time VIM pops up, which is not good.

This post tries to dissect VIM basics for you to feel confident enough to handle simple editing tasks in this marvelous tool.

VIM has five modes, and depending on the active one, each key and combination of keys will have a different meaning. These modes are:

  • Normal mode
  • Insert mode
  • Visual mode
  • Command mode
  • Replace mode

The more capable you become with Vim, the more switching between modes that you'll have to do. That is why it is an excellent practice to develop the habit of pressing ESC anytime you want to change, as this will bring us always back to the Normal mode from which we will be able to access all the other ones.

Now we will look to the most "relevant" modes for a beginner and see the more useful commands in each of these modes.

Insert mode

I'm starting with the second mode because this is what you would expect from any other text editor, where you can type things and see them appear on the screen. To enter the mode, press ESC to go to the Normal mode and then any of these options:

  • i enters Insert mode at the current position of the cursor.
  • a enters Insert after the current position of the cursor.
  • o enters Insert in a new line below the current line.
  • I enters Insert at the beginning of the current line.
  • A enters Insert at the end of the line.
  • O enters Insert in a new line above the current line.

The better you become with Vim, the less time you'll spend in this mode because the rest of them are what makes Vim so unique and different from any other text editor.

Normal mode

Normal mode is Vim's default mode and our starting point. As Vim users, we should spend most of our time in this mode, making Vim unique.

Navigating

To move around, we can use either use the arrow keys like any other text editor or:

  • h move one character left
  • j move one row down
  • k move one row up
  • l move one character right

The benefit of using this last method is that we can prefix it, e.g.:

  • 5l moves five columns to the right
  • 1k moves one row up

But we also have word and line movements that are much powerful than simple caret moves:

  • w moves to the beginning of the next word
  • b moves to the beginning of the previous word
  • e moves to the end of the word
  • W moves to the start of the next word after a whitespace
  • B moves to the beginning of the previous word before a whitespace
  • E moves to the end of the word before a whitespace
  • 0 moves to the beginning of the line
  • $ moves to the end of the line

Editing

Normal mode exposes also features to handle content

  • dd to delete current line
  • u to undo the last action
  • p paste contents of the clipboard into cursors position

Visual mode

Visual mode is used to make selections of text. It is Vim's approach to select text with a mouse:

  • v to enter select mode
  • y to copy after selecting(yanking)

Searching

  • /<term> to search forward

    • n to move to next match
    • N to move previous to previous match
  • ?<term> to search search backward

Command mode

We can access command mode by pressing : and from there we can write the command. Eg:

  • :q to exit the file
  • :q! to exit the file discarting any previously made change
  • :5 jump to line 5
  • :$ jumpt to end of the file

Bonus

We can add/remove the numbers to the lines by typing:

  • : set number
  • : set nonumber

We can split the window with and toggle between pannels with:

  • :sp <file2> or :vsp <file2>
  • CTRL + w + w to switch between screens