The target audience for this tutorial is people that are new to UNIX like operating systems and have a basic familiarity with using a command line interface. This tutorial should teach you enough about vi to get by if vi is all you have available. It should leave you with enough understanding to use official documentation and more advanced tutorials.
What is vi?
Vi is a popular text editor for UNIX like operating systems created in 1976. Unlike most word processors and text editors that have a graphical user interface, vi is terminal based and is controlled exclusively with the keyboard. There are many clones and implementations of vi with different features, but the core functions covered by tutorial should be found in any implementation.
While easier to use alternatives exist, such as GUI text editors and nano, vi can be found on pretty much every UNIX like system out there. Vi also has some features that make it much more powerful than the easier to use terminal editors. It is powerful enough that after 40 years, it is still one of the most popular editors on UNIX systems. Vi being popular, powerful, and ubiquitous on Linux and UNIX systems should be sufficient reason to at least learn the basics.
I highly recommend using this tutorial on a UNIX like system and following along with the examples. Without a little repetition, remembering the commands will be rather difficult. Staring vi is simple. In a terminal simply 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. Once vi is started, there is something you must understand before you are able to do anything. There are two modes: command mode, and insert mode. Insert mode is used for adding text to the file and nothing more. Command mode is where you do everything else. For those that are following along, we will create a handy reference to keep on your system called vi_quick_ref.txt Let’s get started and launch vi now:
shell-prompt$ vi vi_quick_ref.txt
Let’s start by learning how to switch between command mode and insert mode. If you aren’t sure which mode you are in, just press the Esc key to switch to command mode. Another important thing to remember is vi commands are case sensitive. For example, the a command and the A command do different things. There are several ways to switch to insert mode. This tutorial will cover four. Typing a will allow you to add text to file after the cursor. It is easy to remember if you think a for append. Go ahead and type a to switch to insert mode. Try typing or pasting the text below into vi:
vi quick reference
Now press the Esc key. You are back in command mode. Now type 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.
If you are following along, escape to command mode, and then press i to insert the text:
End of my vi reference.
Escape back to command mode when you are done.
The last insert mode we will learn is O. 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.
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 to get used to navigating. There are other navigation commands, but these four are enough for now. Now that you should have a decent grasp on navigation, lets move on to the commands you should know to be able to do basic editing.
Vi commands come in a two varieties. One type of command, those 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.
To save a file, you use the :w command. Try this now if you are following along. 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 saving them, save a copy of the file you have open by using this command:
If the file is read-only, and you have permission to override the read-only attribute, you can append ! to :w and attempt to force writing the file. Try it out:
Quitting vi is done with the :q command. If you have made changes to the file since starting the editor 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:
Open the file back up just as you did at the beginning of the tutorial. 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. Practice a little 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
:q - quit vi
:q! - quit vi even if the file has unsaved changes
: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
x - delete the character the cursor is on
dd - delete the line the character is on
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.
Options are turned off by preceding them with no. For example to turn off the number option, you would use this command:
You should now be able to use vi well enough to do some light editing and make sense of the manual(s).