Skip to content

Vim guide

Kyle Terrien edited this page Oct 31, 2016 · 1 revision

Vim is a text editor originally based on vi, the classic UNIX screen editor. The name "Vim" is a shortened form of "vi improved".

Unlike most other text editors, Vim uses editing modes. This means that instead of merely positioning the cursor and entering text, you must position the cursor and enter an editing mode (e.g. insert mode (i)) before entering text. (And after finishing your change, you must exit the editing mode (Esc) you are using.)

The biggest hurdle for new Vim users is getting used to the modal editing paradigm.

Learning

Full installations of Vim come with a built-in tutorial called vimtutor. To access vimtutor, simply run vimtutor from a command line.

Frequently used commands

ViEmu has put together a [nice cheatsheet of Vim commands] cheatsheet.

Note: ^C means Ctrl+C.

Navigation:

  • h, j, k, l - move left, down, up, and right by one char (respectively). (Think of Dance Dance Revolution)
  • w, b, e - move one word forward, one word backward, and to the end of the word (respectively)
  • W, B, E - same, but the word boundary is only whitespace
  • 0, ^ - Go to the beginning of the line, go to the beginning of the line (minus whitespace)
  • ^E, ^Y - scroll down one line, up one line
  • ^D, ^U - scroll down half of a screen, scroll up half of a screen
  • ^B, ^F - scroll down one screen, scroll up one screen
  • H, M, L - position cursor at top of screen, middle of screen, or bottom of screen
  • z<Enter>, z., z- - scroll so that the cursor is at the top of the screen, middle of the screen, or bottom of the screen

Editing: (To exit a mode, press <Esc> (^[))

  • i - Insert text before the cursor
  • a - append text after the cursor
  • I - insert text at the beginning of the line
  • A - append text at the end of the line
  • s - substitute a char with text
  • r - replace a single char
  • R - replace (overwrite) text
  • v - enter visual mode (for selecting text)
  • V - enter visual-line mode (for selecting entire lines)
  • ^V - enter visual-block mode (for selecting rows/columns of characters)

Combine these commands with a motion command to act on a group of characters (e.g. cw to change a word). Also, entering the command twice will make the command act on the entire line (e.g. dd to delete a line).

  • c - change
  • d - delete
  • y - yank (copy) text

Searching:

  • / - search for a string
    • The search string is a regular expression
    • End the string with \c to make the search case insensitive
  • ? - search backwards for a string
  • n, N - search again, search again in the opposite direction
  • %s/foo/bar/g - perform a global substitute (find/replace) of "foo" with "bar".

Frequently asked questions/comments

How do I exit Vim?

  • :q<Enter>
  • :q!<Enter> if the buffer was modified

Vim is in a funny mode.

Press <Esc> several times until you are out of whatever mode you are using.

A good habit is to exit all modes whenever you are not using Vim (e.g. switch to another application).

Vim is displaying a command prompt (:) and prints successive lines in the text file when I press enter.

You entered ex mode (Q). Abandon all hope ye who enter here. Enter the command vi to return to visual mode.

The Q command can be disabled by inserting the following into your vimrc.

noremap Q <Nop>

This is insane. How does a human use an editor like Vim?

Practice makes perfect. After some time, each command becomes muscle memory.

Can you use Vim to code (do actual work)?

Yes! That's what Vim was designed for.

See also