5 Minute Vim Tutorial

Vim works differently from other editors you are used to, but it only takes about 5 minutes to learn the basics you need to edit with Vim. You won’t be a Vim wizard, but you will be able to:

  • Open a file
  • Move around in a file
  • Edit text in a file
  • Save a File

The slightly longer Extended 5 Minute Tutorial you will show you how to search in a file, get help, and access Vim’s own built-in tutorial.

Contents

  1. Vim in 5 Minutes (or less)
  2. The Extended 5+ Minute Tutorial
    1. Normal Mode
    2. Opening a File
    3. Saving a File
    4. Exiting Vim
    5. Navigating a File
    6. Inserting and Deleting Text
    7. Searching
    8. Getting Help
  3. Final Words

Vim in 5 Minutes (or less)

  • Vim operates in Modes. Vim always starts in Normal Mode.
  • Normal Mode is your base mode and you should always return to it.
  • All command / control keys are case sensitive.
  • If you are not in Normal Mode, you (usually) get back to Normal Mode by pressing the Escape key twice.
  • If you see recording in the lower left corner of the screen, press the q key to return to Normal Mode.
  • If you’ve messed stuff up, return to Normal Mode and type :q! followed by the Enter key to exit Vim without saving the file.
  • In Normal Mode you can navigate the file using the h, j, k, l keys (left, down, up, right). You might also be able to use the arrow keys on the keyboard1.
  • When you have navigated to where you want to edit the file (inserting or deleting text), press the i key to enter Insert Mode.
  • In Insert Mode you can only enter or delete text from the current cursor position – you cannot navigate. To navigate, press the Escape key to return to Normal Mode.
  • To save (write) the file, return to Normal Mode and type :w followed by the Enter key.
  • To exit (quit) Vim, return to Normal Mode and type :q followed by the Enter key.

The Extended 5 Minute Tutorial

This adds searching and help to the 5 Minute Tutorial, but, otherwise, keeps the information to the bare minimum so you can quickly be functional in Vim instead of frustrated.
All command / control keys are case sensitive: h is not the same as H!

Normal Mode

Normal Mode is the default mode and it should be the mode you always go back to.
Most times, you press the Escape key once or twice2 to return to Normal Mode.
If you see recording in the lower left corner of the screen, press the lowercase q key to return to Normal Mode.

Opening a File

The simplest way to open a file is to type vim at the command line followed by the name of the file:

user@COMPUTER:~$ vim file.txt

If it is a new file you should see something like this:


~
~
~
~
~
"file.txt" [New File]                       0,0-1          All

If it is an existing file, you should see something like this:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Basic HTML5 Template</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <!-- page content goes here -->
    <p>This is some dummy content that is not lorem ipsum.</p>
  </body>
</html>
~
~
~
"basic.html" 12L, 290C                      1,1           All

The status line3 (at the bottom of the screen) displays the following information:

  • file name
  • total number of lines in the file
  • total number of characters in the file
  • current line the cursor is on
  • current column the cursor is in
  • percentage showing the position in the file (if All : the entire file is visible, Bot : at the bottom (end) of the file, Top : at the top of the file)

In Normal Mode load a file by typing :edit followed by the filename and then pressing the Enter key.:

:edit basic.html

This discards the current file (if any) and loads the specified file.
If the file to be discarded has been modified, but not saved, you will see the following warning in the status line:

E37: No write since last change (add ! to override)

If you don’t care about saving the file, simply append ! to the :edit command:

:edit! basic.html

The ! tells Vim to “just do it!” and ignore any warnings.

Saving a File

You need to be in Normal Mode to save a file.
Save a file by typing :w followed by the Enter key.
If you want to save the file with a different name, type :w followed by the new file name and then press the Enter key:

:w new-filename.txt

Exiting Vim

You need to be in Normal Mode to exit Vim.
Type :q followed by the Enter key to exit Vim.
If Vim warns you that the file has been modified, you can choose to save it or you can force Vim to exit without saving the file by typing :q! followed by the Enter key. The ! tells Vim to “just do it” and exit without saving the file.

You need to be in Normal Mode to navigate a file.
The letters h, j, k, l navigate the cursor through the file. (You may also be able to use the arrow keys present on most keyboards.)
They act like arrow keys and are conceptually laid out like this:

h
j
k
l

When you navigate left (h) to the beginning of a line, the cursor will stop. It will not wrap around and up to the previous line.
When you navigate right (l) to the end of a line, the cursor will stop. It will not wrap around and down to the next line.
If you type a positive integer (which will not be displayed), followed by one of the navigation keys, the cursor will move that many characters / lines.
For example, in Normal Mode, if you type 10j, the cursor will move down 10 lines.

Inserting and Deleting Text

In Insert Mode the only thing you can do is insert or delete text. (You may be able to use the arrow keys on your keyboard to navigate the text while in this mode, but there is no guarantee, navigating is supposed to be done in Normal Mode)
Enter Insert Mode by pressing the i, a, I, or A key.

  • i insert / delete before the cursor (what you would intuitively expect when entering or deleting text).
  • a append / delete after the current cursor position.
  • I insert / delete at the beginning of the line.
  • A append / delete at the end of the line.

You will (probably) be using i most frequently.
When you enter Insert Mode you will see – INSERT – displayed in the lower left corner:

The navigation keys are h, j, k, l.
h moves the̲ cursor one character left
l moves the cursor one character right
j moves the cursor one line down
k moves the cursor one line up
~
~
~
~
-- INSERT --                       3,11      All

Press the Escape key to return to Normal Mode.

Searching

You must be in Normal Mode to search.
In a large file, it is useful to locate items of interest by searching for them.
To search, type / followed by the text you want to find, followed by the Enter key.
Vim treats the search string as a regular expression – which means some characters have a special meaning. If you need to search for the following characters, you will need to type them as follows:

Name Character Need to Type
Period . .
Asterisk * \*
Backslash </td> \
Dollar Sign $ $
Forward Slash / /

Searches are case sensitive.
You can navigate between search results by pressing n (next) or N (previous).

Getting Help

You need to be in Normal Mode to use help.
To get help on something in Vim, simply type :help followed by the thing you want help on.
For example, to get help on the navigation key h, type :help h followed by the Enter key.
You should see something similar to the following:

h         or          *h*
<Left>    or          *<Left>*
CTRL-H    or          *CTRL-H* *<BS>*
<BS>      [count] characters to the left.  |exclusive| motion.
          Note: If you prefer <BS> to delete a character, use
          the mapping:
             :map CTRL-V<BS>    X
          (to enter "CTRL-V<BS>" type the CTRL-V key, followed
          by the <BS> key)
          See |:fixdel| if the <BS> key does not do what you
          want.
m̲o̲t̲i̲o̲n̲.̲t̲x̲t̲ ̲[̲H̲e̲l̲p̲]̲[̲R̲O̲]̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲1̲7̲2̲,̲1̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲1̲2̲%̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲ ̲
Vim will split the display into two panes. In the upper pane
you will see the help text. In the lower pane you will see your
current editing buffer.
Now you know that Vim supports multiple editing buffers - but
that is not the point of this tutorial.
~
~
~
~
a-file.txt                     4,5              All

The help can be pretty dense and not user friendly, but it may be helpful.
To close the help window, type :q followed by the Enter key.
If you type :help, it will bring up the general help file. You can navigate through it for topics of interest.
You might also try :help tutor for instructions on accessing the Vim tutor which is included with every Vim installation.

Final Words

Vim may not be the easiest or most intuitive editor you will come across. However, it (or its ancestor Vi) is quite ubiquitous and this tutorial gives you the basics you need so you are minimally functional with it.
The basics are simple:

  1. Vim operates in Modes.
  2. All command / control keys are case sensitive : k is not the same as K.
  3. The base mode is Normal Mode. You (usually) get back to it by pressing the Escape key once or twice.
  4. Navigating a file is done in Normal Mode using the h, j, k, l keys.
  5. When the cursor is where you want to insert or delete text, you press the i key to enter Insert Mode.
  6. Search in Normal Mode by typing / followed by the string you want to find and then the Enter key. Navigate the search results using the n and N keys.
  7. Save a file by typing :w in Normal Mode.
  8. Exit Vim by typing :q in Normal Mode.
  9. In Normal Mode you can get help by typing :help

  1. Vi (the precursor to Vim) was developed in 1976. Arrow keys were not commonly found on keyboards, so h, j, k, l were used.
  2. Some modes require one key press, others require two. If you press it too many times, the computer will just beep at you. Vim newbies often repeatedly press the Escape key in the hopes of returning to Normal Mode
  3. This is true for the default Status Line. It is always possible that someone has modified the Status Line to display different information, or in a different arrangement.