Super-fast introduction to the Vi editor
A text editor is a program that can be used to create and
modify plain text files (not word processed documents). One of the most
popular editors on Unix systems (it is also available for Windows and
many other
platforms) is vi (pronounced "vee-eye").
Tutorial
This tutorial assumes you are logged into a Linux system.
Open a terminal window.
The following command will make a copy of a system configuration file
that you can use as a practice file.
cp /etc/screenrc practice.txt
Invoke the text editor on the file:
vi practice.txt
You should see the text of a configuration file appear on the entire
screen.
The bottom line shows the name of the file and how big it is. The
cursor rests on the first character in the file.
Move the cursor down, right, up, and down using the letter keys:
h move cursor one character to left
j move cursor one line down
k move cursor one line up
l move cursor one character to right
Notice these keys are conveniently located on the "home" row of the
keyboard to make it easy to move around in the file. Since the
keys h,j,k,l are adjacent and easily accessible with the fingers of
your right hand, you can quickly reach them to move the cursor, instead
of fumbling around for the arrow keys as with many other editors
(though they can be used in vi too). You will find that this use of
h,j,k,l become second nature to you very quickly, very much increasing
your speed, efficiency and enjoyment of text editing.
Some additional movement keys are:
w move cursor one
word to right
b move cursor one word to
left
ctrl-f scroll forward one screen
ctrl-b scroll backward one screen
Note that these command keys are case-sensitive.
Now use the movement keys to position the cursor at the start of the
word "wide" in line 5. Press the
following keys:
x (delete the
character
under the cursor)
dw (delete the current word)
dd (delete the current line)
u (undo the prevous operation)
ZZ (save file and exit.
(Note upper case letters)
You
should see the command prompt. You may view
your changes:
more practice.txt
Inserting text
Invoke the text editor on the file:
vi practice.txt
Now we will insert some new text into the file. Move the
cursor to second line of the file. Type the letter ‘i’
(it
stands for “insert-text mode”), and then type the following
(including hitting the Enter key at the end of each of the three lines):
The quick
brown
fox will return.
Then hit the Escape key to leave insert-text-mode. This mode-oriented
aspect of the vi editor differs from many other editors. You must
be in Insert mode before you can enter text, and you must leave insert
mode before you can move around in the file. The key to
mastering vi is to keep in mind the difference between
insert-text mode and command mode. In the latter mode, as its name
implies, one issues commands, such as the movement commands. The
characters you type will appear on the screen if you are in insert-text
mode, whereas they will not appear on the screen while you are in
command mode. By far the most frequent problem new vi users
have is that they forget they are in insert-text mode, and so their
commands are not obeyed, but simply inserted as new text into the file.
Now save the file and exit vi by typing ‘ZZ’.
You
should
see the command prompt. Issue this command to view
your changes:
more practice.txt
Learning More
Vi has many powerful commands, far beyond what you find in
Notepad or other simple editors. There are many good tutorials on
the web for learning advanced features of the editor.
You might start with Vi For Smarties
then use a search engine to look for "vi tutorial" and see what else
you find.