vim: basic eight

eight things you need to know how to do for your survival

Here are eight things you should know how to do in vim to be able to “survive” with vim as your text editor if you absolutely have to.

  1. quit without saving
  2. enter code
  3. save
  4. copy/paste
  5. search/replace
  6. show line numbers
  7. goto a line number
  8. save as

Each of these is explained below. But first, step “0” is to learn three basic vim concepts: escape, modes, and the vim command line.

0. Escape, modes, and the vim command line

You gotta learn these three things first.

a. The “esc” key (“escape”)

The “escape” key, sometimes labelled “esc” is at the upper left hand corner of your keyboard. You are gonna need that a lot. Find it now. Ok, good.

Bonus tip: “CTRL-[” can be used instead of the escape key if that’s more comfortable for your fingers.

b. “insert mode” vs. “command mode”

vim is always in one of two modes:

If you don’t know which mode you are in, you are “gonna have a bad time”.

Here’s the basic way of moving between the modes:

There are a few others ways to move between the modes, but that’s enough for “survival vim”.

c. The command line (:)

There are some things you do in vim at the command line.

The command line is at the bottom of the screen, and starts with the character : (colon).

You get to the command line by typing the : character when you are in edit mode.

When you do, the cursor jumps to the bottom of the screen. You get the cursor back in the document by either:

The basic eight

1. quit without saving: :q!

To quit without saving in vim:

2. enter code

To enter code:

Bonus tip:

3. save and quit

To save changes and quit:

To quit with saving changes in vim:

4. copy/paste or cut/paste

From this Stack Overflow answer

5. search/replace

To repeatedly search for a string:

To search/replace foo to bar in the entire file all at once

To search/replace foo to bar one occurance at a time:

(from this Stack Overflow article )

6. show line numbers

(From this article)

7. goto a line number

(From this article)

Bonus tips:

8. save as

Bonus tip:

Even more bonus tips

For now these are not part of the basic eight, though we might add them in future quarters. They arguably should have made the list.

Undo is the u key

When you are in “Command Mode”, the u keystroke will undo. Type it multiple times to do multiple levels of undo.

CTRL-Z does NOT undo

Note that if you are used to typing CTRL-Z this does not work in vim. Instead, it will “put vim to sleep” and put you back at the Unix command line. To bring it back to life, use fg at the command line.

Type jobs to see if you have “sleeping vim” instances. If you did, it will list them. You can use fg to bring them back to life and quit them one-by-one.