LiveFire Labs: Online UNIX Training with Hands-on Internet Lab

"Thank you for the use of your system and the lesson material on UNIX Shell scripting! I now have a firmer grasp on the Korn Shell as a whole, and give much credit to LiveFire Labs. Thank you again, and I will definitely consider your company in the future for anything else I may need!"

John M. Baughman
Synergetics Incorporated
Fort Collins, Colorado

My company installed the new Warehouse Management Systems based on the Unix platform six months ago and I am a mainframe COBOL programmer. I bought a few Unix books but it did not help me much, so I was looking for a class that could teach me basic Unix shell scripting.

After research, LiveFire Labs was the only class that was affordable for me; other professional classes cost $1000+. I registered for the Unix Shell Scripting course and was able to work at home or office during my spare time. I liked the hands-on exercises, which are vital for learning programming skills. I recommend LiveFire Labs to anyone who wants to learn Unix skills.

Mike C. Wang
Long Beach, California

Read more student testimonials...

Receive UNIX Tips, Tricks, and Shell Scripts by Email

Custom Search

LiveFire Labs' UNIX Tip, Trick, or Shell Script of the Week

Basic UNIX Commands for Beginners: vi Tutorial - Part I

Regardless of what you use a UNIX or Linux system for, you will sooner or later need to create a new text file or edit the text in an existing file. In order to accomplish this task you will need to know how to use a text editor. This tutorial will teach you the basics of using the vi text editor.

The vi editor is available on most systems running the UNIX or Linux operating system (it's also commonly found on computers running other operating systems), is widely used by UNIX and Linux users, and is perfect for editing files over remote or low-speed connections.
[ If you are new to UNIX and need an overview of important UNIX commands and concepts, check out our Basic UNIX Commands and Concepts Tutorial for Beginners ]

Since vi has a large number of features to support its extensive functionality, this vi tutorial is designed to just introduce you to the basics so that you can start using vi immediately.

(1) Entering vi
When you start the vi editor, you can…

(a) open an existing file for editing:

$ vi existing-filename

(b) open a new file for editing:

$ vi new-filename

(c) start editing a file without specifying the name of a file:

$ vi

If you open an existing file, you will see the contents of the file. The lines following the file's contents will contain a single ~ (tilde) character at the beginning of each line. These lines will not become part of the file's contents until text is added to them. When you start vi with the name of a new file or without specifying a name, every line except the first (top) line will have a ~ in the first position of the line.

The vi editor does all of its work in a work buffer. This means that the editing you do is not written to disk until you exit vi or explicitly instruct vi to write (save) the buffer's contents to disk.

When you start a vi session, the default mode of operation is Command Mode. This means that vi is ready to receive and act on the commands you give it. This may at first be a little strange to you because word processors by default start in Input Mode, ready to add text immediately.

In Command Mode you can maneuver the cursor through the work buffer, delete or copy and paste text, save the work buffer's contents to disk, and exit the vi session. All keystrokes in this mode are interpreted as commands, are not displayed to the screen, and do not require you to press the Enter key for them to be executed.

(2) Exiting (Quiting) vi
There are several different ways you can exit a vi editor session. To exit (quit) vi from Command Mode, you would use the ZZ (two uppercase z's) command. As mentioned in the previous section the two Z's will not be displayed to the screen. The contents of the work buffer will be written to disk prior to exiting the editing session and then you will be returned to the command prompt.

You can also exit the editor from what's called Last Line Mode. All commands starting with a : (colon) character puts vi in Last Line Mode. When you press the colon (:) key on your keyboard the cursor will move to the bottom left-hand side of the screen where you will enter the command you want to execute. Unlike Command Mode commands, you need to press the enter key on your keyboard to execute the Last Line Mode commands.

The Last Line Mode command used to write your changes to disk and then quit vi is:


To quit without writing your changes to disk, you would enter:


If you did not make any changes during your editing session, vi will return you to the shell prompt immediately. If you made changes during your session, vi will inform you of this and ask you to confirm your desire to quit without saving the changes. Adding a ! (exclamation point) to the end of the quit command will inform vi that you want to quit even though you haven't saved your changes:


As you have seen, pressing the colon (:) key will change the mode from Command Mode to Last Line Mode. If you wanted to go from Last Line Mode to Command Mode, you would just need to press the Esc key on your keyboard.

HINT: If you are not sure about which mode you are in, just press the Esc key on your keyboard a few times to verify you are in Command Mode and go forward from there.

(3) Saving your Work Without Exiting (Quitting)
If you edit a file for a long period of time it would be a good idea to save the work buffer's contents to disk occasionally.

The Last Line Mode command used to write your changes to disk without quitting vi is:


If you did not specify a filename when you started your vi editing session, you could specify a file to save to using the following Last Line Mode command:

:w filename

(4) Switching to Input Mode to Add Text
The only way to add text to the work buffer is by switching from Command Mode to Input Mode. If text does not appear in the editor as you type, you are not in Input Mode. There are a few different ways to enter Input Mode.

Insert Mode

The first is by pressing the i key (lowercase i) to insert text. Text will be inserted exactly where the cursor was when you pressed the i key. If you enter an I (uppercase i), text will be inserted at the start of the line the cursor is on.

Pressing the space bar on your keyboard will put a space between text, and pressing the Backspace key on your keyboard will erase over what you have typed. It is important to know that the Backspace key will only erase text that is on the current line of the buffer (you can not backspace up to the previous line of text). You also can only backspace over text that was inserted during the current Input Mode session.

Pressing the Enter key on your keyboard while in Insert Mode will cause the cursor to advance to the next line. You should avoid letting text lines automatically wrap from one line to the next by pressing the Enter key before you get to the end of a line.

The Input Mode session ends when you press the Esc key on your keyboard, which returns you to Command Mode. When you press the Esc key the cursor will move to the left one position.

Append Mode

You can also enter Input Mode by pressing the a key (lowercase a) to append text. Text will be appended one position to the right of where the cursor was when you pressed the a key. If you enter an A (uppercase a), text will be appended at the end of the line the cursor is on.


The last method we will cover for entering Input Mode is by pressing the o key (lowercase o) to open a line for adding text below the line the cursor is on. The cursor will be moved to the beginning of the line and you are ready to enter text. If you enter an O (uppercase o), a line for adding text will be opened above the line the cursor is one. This would be useful if you wanted to add a new line of text to the beginning of the buffer (file).

(5) Diagram of vi Editor Modes

This diagram shows the different vi editor modes of operation and the methods for changing from one mode to another:

vi Tutorial Editor Modes