Complete, Concrete, Concise

Practical information without the bloat

5 Minute Vim Tutorial

Sometimes you find yourself at a *nix command line and the only editor available is Vim or Vi. It works quite differently from modern editors.

The typical editing operations we are interested in are:
* Opening a file
* Navigating a file
* Inserting or deleting text from a file
* Saving a File
* Searching a file

Vim works in modes, we are interested in two of Vim’s modes:
* Normal Mode
* Insert Mode

In Normal Mode you can:
* navigate the file
* issue commands (open, close, exit)
* search

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
    9. The Built-in Tutorial

Vim in 5 Minutes (or less)

  • Vim operates in modes. The base mode is Normal Mode.
  • All keys are case sensitive.
  • Usually, you get 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! 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 keyboard.
  • Once 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
  • To exit (quit) Vim, return to Normal Mode and type :q

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 keys are case sensitiveh is not the same as H!

Normal Mode

Normal Mode is the default mode and it should be the mode you always come back to.

Most times, you press the Escape key once or twice1 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:

[email protected]:~$ 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 line2 (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:

: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.

If you want to save the file with a different name, type :w followed by the new file name.

:w new-filename.txt

Exiting Vim

You need to be in Normal Mode to exit Vim.

Type :q 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!. The ! tells Vim to “just do it” and exit without saving the file.

Navigating a 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. (It might if you are using the arrow key.)

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. (It might if you are using the arrow key.)

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.)

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 an 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 \ \\
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 somthing 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 and you will see the help text above and
your current editing buffer below.

So, 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

If you type :help, it will bring up the general help file. You can navigate through it for topics of interest.

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 keys are case sensitivek is not the same as K.
  3. The base mode is Normal Mode. You (usually) get back to it by presing 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. 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 
  2. 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. 
%d bloggers like this: