Unix Manuals

vi Tutorial

Part 1 - Creating a simple file

NOTICE - New forum for all UNIX learners, users and programmers - click here

The best way to learn vi is by using it. With our 'vi reference guide' and 'frequently asked questions', there should be little else you need to become an expert in the editor. However, many peoples' first experience with vi is when they are in a rush to get something done, which can be problematic when you don't have time to learn the editor. This tutorial will get you to the stage, in about 10 minutes, where you can run vi, load a file, make basic edits, and save it again. That may be all you'll need for now. When you need to move onto more advanced territory, use our 'vi reference guide' to learn additional commands.

Running vi

To run vi and create a new file, simply run the command 'vi' from any shell prompt:

$ vi

Alternatively, to load an existing text file into vi, run the command 'vi [filename]', from the shell prompt:

$ vi myfile.txt

Your file will be loaded into vi, and the cursor will be placed at the beginning of the first line. Note that an empty line is shown as a tilde (~).

Learning the vi Editor

Buy it from Amazon - click on the image for more details

Command Mode and Insert Mode

vi is different from many editors, in that it has two main modes of operation: command mode, and insert mode. This is the cause of much of the confusion when a new user is learning vi, but it is actually very simple to understand.

When you first load the editor, you will be placed into command mode. To switch into insert mode, simply press the 'i' key. Although nothing will change on the screen to indicate the new mode, any thing that you type from now on will appear in the screen - this is what you are used to if you have ever used any other editor, or word processor. Try typing a few lines of text. When you press 'return' or 'enter' a new line will be created, and you may continue typing.

When you have finished typing, you may return to command mode. This is done by pressing your 'Esc' key. In command mode, key presses do not appear on the screen, but instead are used to indicate various commands to vi.

At first, you may often mistake command mode and insert mode. For example, you may think you are in insert mode, and start typing your text, when in fact you are in command mode, and each keypress you make will issue a command to vi. Be careful - you may accidentally modify or delete parts of your file.

If you are unsure which mode you are in, press 'Esc'. If you were in insert mode, you will be returned to command mode. If you were already in command mode, you will be left in command mode (possibly with a 'beep', to indicate that you were already in command mode).

An Exercise

Try this simple exercise to get used to the two modes:

1. Load vi (if you haven't already), by typing:

$ vi

(don't forget - the $ is the system's prompt, and may be different on your system. You only type the 'vi' part, shown in bold.)

The screen should show a blank file, with each blank line represented by a tilde (~), for example:


By default you are in command mode.

2. Switch to insert mode, by pressing the 'i' key. Then type some text, using ENTER or RETURN to start new lines. For example:

Hello.  This is my first session in the vi editor.
This is the second line of text.

3. When you have finished entering your sample text, press 'Esc' to return to command mode.

4. Now we will learn a useful command: to save the file. The command for this is ':w' (note the colon before the 'w'). After the 'w', put a space, and the name you want to store the file as. For example:

:w firstfile

Type it now. Notice how the text ':w' appears at the bottom on the screen. When you have finished the command, press ENTER or RETURN. You should see a confirmation that the file has been saved, which may include the number of lines in the file, and possibly the file size.

5. To finish this simple introduction to the editor, we will learn one final command: How to close the editor. The command for this is ':q' (again, with a colon before the 'q'). Don't forget you need to be in command mode. If you're not already in command mode, or you're not sure which mode you're in, press 'Esc' now. Then issue the command:


You should be returned to the UNIX prompt.

vi Editor Pocket Reference

Buy it from Amazon - click on the image for more details

A final note:

Sometimes, you may have made changes to a file that you do not want to save. This may be because you have decided the changes are incorrect, or you have become confused using vi (not usual at first!), and incorrectly made some changes, maybe by typing into command mode instead of insert mode. To exit vi without saving, and ignoring any warnings about unsaved data, use a variation of the ':q' command, with an exclamation mark after it:


This will return you to the prompt, without saving any changes to the file, and with no warnings about unsaved data. Use this command carefully.

Ready to move on?

Before you move onto the next segment, it may be worth just playing with what you've already learned. Practice loading vi, either with an existing file, or with no filename so it creates a new file. Try going into insert mode, inserting some text, going back into command mode, saving the text, going back into insert mode, etc. You should be able to do the following:

Move on to segment 2 - basic editing commands

Unix Tutorials Contents Page.

vi Reference