vi is one of several popular editors available to UNIX programmers. Unlike many other popular text file editors, vi does not have a menu bar across the top of its interface frame and this can make it difficult for new users to create and edit files. This tutorial will introduce you to vi and show you how to create, edit and save files with this powerful and popular UNIX file editor. Although, there is a modest learning curve for new users, almost all experienced vi users, including myself, maintain that it is worth the effort.
This tutorial will not teach you every vi command that you will ever need to know. Rather, the goal of this tutorial is to introduce you to some of the commands that are available when editing files using vi. Also, I hope to inspire you to investigate vi further on your own for other commands that may be just as important to your particular writing and editing style.
This tutorial uses the notation [i]
to indicate the "i" key
on the keyboard. Do not type the "[" and "]" characters,
just type the key that is indicated.
If the character indicated is in upper case like in [I]
,
you must hold the [Shift]
key down while the character is pressed.
Quotes are used to indicate that an entire
line of text should be entered as shown.
I assume that you know how to login, logout and open xterm windows in UNIX.
When you create or open a file with vi, you are in the command mode by default, where you can delete characters, words, lines, etc. and perform other types of edits as well. However, to add information, you will have to change to one of several input modes. Also, you will learn the purpose and how to use vi's line editing mode.
In this tutorial you will learn the purpose of each mode and how to use it. After completing this tutorial, you will have your own quick reference sheet that you can print and add to as you learn more commands.
Creating your own quick reference sheet of vi commands will give you practice using vi to create and edit text files as well as providing a document for you to refer to if you forget a particular command that you remember learning.
vi vi_reference.txt
and press Enter. You are now in command mode and any keys that
you type will be interpreted as commands instead of
text for the file.i
to enter insert mode. At this point, any
keys that you type will be interpreted as text to be added to the file.Here's what insert mode looks like:
vi Quick Reference Sheet ------------------------ i - type i to insert text before character under cursor Esc - press the Esc key to return to command mode ZZ - save changes and exit vi
Your file should now look like this:
Notice the INSERT MODE
indication in the lower right hand corner.
[Esc]
key to exit insert mode and
return to command mode.Your file should now look like this:
ZZ
to save your work and exit vi.
You are now back at your shell prompt and your work has been saved in the
file vi_reference.txt
Although, there are several modes in vi, you really only have to learn the difference between command mode, input mode and the line editing mode. Here is a brief description of each mode:
Operations like cut, copy and paste must be performed while in command mode and they may be performed on characters, words, sentences and larger blocks of text. Most commands are specified with only one or two typed characters. Note: The command characters must be typed in the correct case.
Each of the other modes is accessed by typing keys that change to that mode.
For example, in practice exercise #1,
you typed [i]
to enter Insert mode, which allowed you to insert (add)
new text to your file.
Command mode is also the mode that vi is in when it is first started. There are many ways to manipulate text in vi and each type of edit has several forms that are executed within command mode.
Insert mode allows you to enter new text into your document before the cursor
or at the start of the current line.
You enter insert mode by typing [i]
or [I]
while in
command mode. However, even though both lower and upper case [i]
and
[I]
will change to insert mode, they will insert text in different
places with respect to the current position of the cursor.
The lower case version [i]
will insert new text at the current
position of the cursor, while the upper case version [I]
will allow
you to insert new text at the beginning of the current line.
Type the [Esc]
key to exit insert mode (or any other mode) and
return to command mode.
The append mode allows you to enter new text into your document after the cursor.
Use [a]
to change to append mode and add new text immediately after the
character under the cursor.
Use [A]
to append text to the end of the current line.
You will be able to continue entering text including new lines until you
exit append mode. Use [Esc] to exit append mode and return to command mode.
The open mode is like append and insert in that it allows you to enter
new text into your document. It also has two versions [o]
and
[O]
.
Use [o]
to open a new line of text after the current line.
From any point in the current line, you type [o]
and your
cursor moves to the start of a blank line that has been added after the
current line.
Use [O]
to open a new line of text before the current line.
From any point in the current line, you type [O]
and your
cursor moves to the start of a blank line that has been added before the
current line.
As with insert and append mode, you type [Esc]
to stop adding
new text and return to command mode for further editing of the file.
Change mode is entered when you want to change the existing text to some other text. You can replace the existing text with more or fewer characters than the text object that you are changing.
This is different from the previous input modes described.
To change existing text in your file, you don't simply type [c]
.
You must also tell vi what word, line, sentence, etc.
you want to change. These are known as the object to change.
FOr example, to change one word of a file, you would move your cursor to the
start of the word and then type [c][w]
. This will replace the current
word with the new word typed.
You can even
execute other UNIX commands like ls
from within vi.
vi vi_reference.txt
and press Enter to open the file you created in topic #1.i
command.I
to enter insert mode.
Where is the cursor located? Esc
key to return to command mode.A
to enter append mode.
Where is the cursor located? Esc
key to return to command mode.O
to insert a new line before the current line in open mode.
Where is the cursor located? Esc
key to return to command mode.i
to enter insert mode.
Where is the cursor located? Esc
key to return to command mode.a
to enter append mode.
Where is the cursor located? Esc
key to return to command mode.o
to insert a new line after the current line in open mode.
Where is the cursor located? Esc
key to return to command mode.In this topic, you will modify your reference sheet and add each of the commands shown in this topic. These commands were used for entering new text into file. Each command first moved the cursor then started a new mode, which allows the user to enter new text at the point of the cursor.
There are many commands in vi that can be used
to reduce the time it takes to edit to your files. You can move the
cursor line by line and character by character using the arrow keys,
but there are better ways if you need to move far away from the current
position of the cursor. You've already seen that [I]
,
[A]
, and [O]
each move the cursor farther than
arrow keys before beginning a version of inserting.
There are commands that move the cursor by words, lines, sentences, paragraphs
and more. The cursor can be moved forward in the file or backward.
Each of these commands can be preceded by a number to indicate how many
time to execute the command. For example, the command [4f]
moves
the cursor forward four words. Rather than provide you with an exhaustive
list of commands to learn, this tutorial will explain the use of the more
common commands and show you how to find more information when you're ready
to learn even more commands.
Follow these steps to modify your vi_reference.txt file. You should review the the commands explained in topic #2 and try the new ones suggested.
:
(colon) key.
This will open a whole set of additional vi commands.set number
[Enter]
to complete the command.4G
to move to line #4 of the file.o
to open a new line after the insert command line.a - type a to insert text after the character under the cursor
o - type o to open a new line after current line
I - type I to insert text at start of current line
A - type A to append text to end of current line
O - type O to open a new line before current line
[Esc]
to return to command mode after you've finished typing the
lines above.Your reference sheet should look like the following perhaps with a few typing errors that you'll correct in the next exercise.
vi Quick Reference Sheet ------------------------ i - type i to insert text before character under cursor a - type a to insert text after character under cursor o - type o to open a new line after current line I - type I to insert text at start of current line A - type A to append text to end of current line O - type O to open a new line before current line [Esc] - press the Esc key to return to command mode ZZ - save changes and exit vi
In this exercise, you will correct any typing mistakes that you may have made during the previous exercises. Since, it is not possible for me to know exactly which typing errors you may have made, this exercise will be different from the earlier exercises. Instead of a step by step approach, I'll just provide a list of vi commands that can be used to edit characters in your file. Be sure to try each of them as you edit your "vi_reference.txt" file.
/Esc key
to move the cursor to the next instance of the phrase "Esc key".0
to move the cursor to the start of the current line
without starting adding new text.i
and the following text "I - type I to insert text at start of current line"
.I
In this topic you will view the on-line programmer's manual page for vi. These pages explain each of the options that can be used when starting vi.
In this lesson you have learned how to create, edit and save text files using vi, one of the more popular editors available to UNIX programmers.
As you work in vi and learn new commands, be sure to add them to your quick reference sheet so that you will be able refer to them in the future.
A
is typed. Text is inserted at the end of the
current line.i
is typed. Text is inserted before
the cursor. Use I
to begin inserting text at the beginning of
the current line.
o
is typed.
Text is inserted on the line following the current line.
man vi
at a command-line prompt for even more
information about the vi editor and its command line options.Last modified: 6/16/2003
Feedback, questions or accessibility issues.
Copyright © 2001 Deb Deppeler