VI Tutorial for Beginners

By | 2016-07-09

This vi tutorial should teach you just enough to do some basic editing and understand the documentation. I have tested it with vim and the version of nvi that ships with FreeBSD. It should work with any version.

Vi is a popular text editor for UNIX like operating systems created in 1976. Unlike most word processors and text editors, vi is terminal based and is controlled primarily with the keyboard. There are many clones and implementations of vi with different features, but the core functions covered by this tutorial should be found in any implementation.

Why vi?

While easier to use alternatives exist, vi can be found on pretty much every UNIX like system out there. Vi has some features that make it more powerful than other easier to use editors. Consider that after 40 years, it is still one of the most popular editors on UNIX systems. Its power, popularity, and ubiquity on UNIX like systems should be sufficient reason to at least try it.

Getting Started

I recommend using this tutorial on a UNIX like system and following along with the examples. With a little repetition, remembering the basic commands should be easy. Starting vi is simple. In a terminal, run the command vi. Optionally, add the name of a file you would like to create or edit as an argument to vi. If you pass vi a file name and it does not exist, it will be created when you save it. There is something you MUST understand before you are able to do anything. Vi has two modes: command mode, and insert mode. Insert mode is used for adding text to the file, and nothing more. Command mode is how you do everything else. For those that are following along, we will create a cheat sheet to keep on your system. Let’s get started and launch vi now:

shell-prompt$ vi vi_quick_ref.txt

Vi Modes

As I wrote earlier, vi has two modes. Command mode is used to control the editor. Insert mode is for inserting text. Command mode is where you save files, quit vi, change settings, and execute powerful editing commands.

If you aren’t sure which mode you are in, press the Esc key to switch to command mode. When you are in command mode, key presses are interpreted as commands. I.e. if you press the A key, an A will not appear on your screen. Vi will instead execute the A command, which moves the cursor to the end of the line and switches to input mode.

Vi commands are case sensitive. For example, a and A are different commands. If vi is ever doing unexpected things, check your Caps Lock. Sometimes I will inadvertently turn on Caps Lock and wonder why vi isn’t doing what I want.

Switching Modes

To switch to command mode, press the Esc key. That’s it.

There are several ways to switch to insert mode. This tutorial covers four. The a command switches to command mode and inserts text to the right of the cursor. Type a to switch to insert mode, then type or paste the text below into vi:

vi quick reference

Press the Esc key. You are back in command mode. Press o followed by the Enter key. Now switch back to command mode. The o command switches to insert mode and creates a new line below the one the cursor is on. The i command inserts text before the cursor.

Make sure you are in command mode, then press i to insert the text:

End of my vi reference.

Switch back to command mode when you are done.

The O command switches to insert mode, and adds a new line above the one you are currently on. Use the O command to add the following text above the line you just created:

I am learning vi!!!

Switch back to command mode when finished.

Navigation

Moving the cursor about the file is usually done in command mode. Some, but not all, implementations let you navigate the file with the arrow keys in insert mode. Most versions will let you use the arrow keys to navigate in command mode. There is a set of navigation commands used from command mode that will work with any vi implementation. You can probably get by with knowing only four navigation commands: h, j, k, and l. h and l move the cursor left and right, respectively. j and k move the cursor down and up, respectively. If you are following along, move the cursor around for a bit to memorize them. There are other navigation commands, but these four are enough for now. Now that you should have a decent grasp on navigation, let’s move on to a few basic editing commands.

Command Types

Vi commands come in two varieties. Commands that start with : or /, will move the cursor to the bottom of the terminal and display the command as you type it. This type won’t take affect until you press enter. The other variety will not be displayed anywhere and take affect immediately.

Saving Files

To save a file, use the :w command. Go ahead and try it. Notice that when you typed :w, it and your cursor appeared at the bottom of your terminal. Again, commands that begin with : or / do not execute until you press enter. If you would like to save a copy of the file with a different name, you can follow :w with a space and a file name. Since it is generally a good idea to backup important files before changing them, save a copy of your cheat sheet with:

:w vi_quick_ref.txt.bak

If the file is read-only, and you have permission to override the read-only attribute, you can append ! to :w and force vi to write the file. Try it out:

:w! vi_quick_ref.txt.force

Quitting Vi

Use the :q command to quit vi. If you have made any changes since starting vi or the last save, you will get an error and vi will not quit. As with :w, ! can be appended to :q to quit without saving the changes. Quit vi now:

:q

Saving and quitting can be combined into the command :wq along with an optional filename. As with :w and :q, ! can be appended to :wq to attempt to force saving the file.

Basic Editing Commands

There are two editing commands that should be enough to get by: x and dd. dd deletes the line the cursor is on and x deletes the character the cursor is on. Re-open your cheat sheet. Practice a bit by navigating to the last line of the file and delete a few characters with x, then remove the whole line with dd. Practice a bit more with x and dd until this is all that remains in the file:

vi quick reference

Now use what you have learned to make the file look like this:

vi quick reference

Switch to command mode: ESC
insert mode commands:

a - switch to insert mode and append text after the cursor
i - switch to insert mode and insert text before the cursor
o - switch to insert mode and add a new line below the cursor
O - switch to insert mode and add a new line above the cursor

quitting vi:

:q - quit vi
:q! - quit vi even if the file has unsaved changes

saving files:

:w           - save the current file
:w filename  - save a copy of the file named filename
:w!          - try to save the file, even if it is read only

navigating in vi:

h - move the cursor one character to the left
j - move the cursor down one character
k - mode the cursor up one character
l - move the cursor right one character

saving and quitting:

:wq           - save and quit vi
:wq!          - try to save the file if it is read only, quit if successful
:wq filename  - save a copy of the file named filename and quit
:wq! filename - save a copy of the file named filename and quit,
                override read only permissions if possible

editing commands:

x  - delete the character the cursor is on
dd - delete the line the character is on

Set Options

There are various options that can be used to change the behavior of vi. These can be changed using the :set optionname command. The one I find most useful is number, which shows line numbers to the left of each line. Try the following from command mode.

:set number

Options are turned off by preceding them with no. For example to turn off the number option, you would use this command:

:set nonumber

If you aren’t sure which options are set, you can view them with:

:set

All Done!

You should now be able to use vi well enough to do some light editing and make sense of the manual(s).

Other sources

FreeBSD vi manual
Vim Documentation
Vim Adventures

Discuss