VI CRASH COURSE: VERSION 1.0
This NHF is an introduction to using the vi
text editor. Its main purpose is to help you, the vi
newbie to get started on writing simple documents using
one of the many text editors out there in the UNIX world.
Since you are reading this, I assume you are interested
in learning vi .
HISTORY OF VI
Before we begin the NHF, it is always fun to look back on
history and find out why vi was created. A
long time ago, people used what were called, line editors.
One such example of this, was ed . Using line
editors were hard because you got to see only one
line of text at a time. So people made another line editor
which had more features, and they called it ex
which was a superset of ed . ex
had one remarkable feature, in that it had a visual mode.
Now people could see a page's length of text when they used
the command ex -v . Because people thought that
a visual editor was great, Bill Joy wrote vi .
Since then, there have been numerous vi clones,
ranging from elvis to vim ,
and vi has been ported to various other platforms
including Microsoft Windows systems.
VI MODES
Most text editors are very easy to use. Take for instant
Notepad which comes with Microsoft Windows
systems. You open it up, you start typing, save the file,
and your work is done. Unfortunately, the same cannot be
said for vi . Notepad has but one
mode. That is, to insert your text and then save it. vi
has two modes. An insert mode, and a command mode. Let me
take a while to explain what each mode is for.
Insert mode is used when you want to insert text to the
screen. When you are in insert mode, all you can do is to
insert text, and to rewrite over text using the Backspace
key. Nothing else. In insert mode, you cannot save your
file, or quit vi
Command mode is used for issuing special commands to vi .
When using command mode, you cannot insert text. However,
you can do the following:
- search for a specific string.
- substitute certain words for other words.
- quit
vi .
- save the text to a file.
- enter a shell.
- start the command to insert text.
- etc...
You will find yourself using both modes extensively. If
the idea of using two modes for you seems difficult, I ask
you to give it a try before you decide to use another text
editor. If you can master the basics of vi ,
you will find that your typing speed not only increases,
but the time taken to do your work using a "normal" text
editor decreases significantly, and it is not because
of your typing speed.
First of all, if you are a touch typist, you will find
this short tutorial to be much easier than a non-touch typist.
The reason is that vi was made for the touch
typists, typically programmers, so that their fingers would
not have to leave the keyboard to move over to the mouse
and back. So everything that has to do with vi ,
is located very close to the keyboard's home keys. No mouse
using.
INSERTING TEXT
You start vi from the console using the following
command:
xconsole$ vi
You will be presented with a screen that looks like this:
~
~
~
~
~
Each tilde means "a blank line". When you start out in
vi , you will be in command mode. That means
that you can only issue out commands and not insert any
text. To begin inserting text, you press the i key,
for insert. It is important to note that vi
is case-sensetive! Although typing i and I
may look the same, they are different. Once you have pressed
i you will see the cursor change. You can now start
inserting text. Experiment with this.
QUITTING VI
To quit vi , you need to be in command mode.
In order for you to leave insert mode and enter command
mode, you press the Esc key. The cursor will change
again, indicating that you have now switched to command
mode. In order to quit without saving any changes, type:
:q!
What does that command mean? The ":" tells vi
that you want to run an ex command. Yes, you
can run ex commands from vi , which
is actually very common. The q means quit.
The "!" means, force. In short, we force vi
to quit without saving the changes made to the file.
MOVING AROUND IN VI
Moving around in vi is a little more complicated
for those who are used to using the mouse. Fire up vi
and enter the following lines into the editor:
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.
Now return to command mode. Your cursor should be positioned
on the last character of the last line as denoted by the
underlined ".". How would you proceed to move the
cursor to say, the fifth character of the second line? For
some vi clones, using the keyboard's arrow
keys is sufficient. However, the correct way of moving around
in vi is by making use of the home keys. Here
is a list of what each one does:
- h - move left one character.
- j - move down one character.
- k - move up one character.
- l - move right one character.
Now that you know what each home key does, practice using
it to move around. It will be very awkward at first, but
if you practice long enough, you'll get the hang of it.
Why bother to learn this when you can just use the cursor
keys? Because not all releases of vi recognize
the arrow keys. But all releases of vi recognizes
the home key movements. So learn them because it will be
worth it.
SAVING A FILE
Previously, we just saw how to quit a file without saving
the changes. Now, type the command:
:q
Leave off the "!" and see what happens. vi
complains, saying that changes were not saved. That is what
the "!" is for. If you use it, then vi
will not complain if you have not saved the changes made
to the file. So let us save the file. Saving the file means,
writing it, so the command for it is:
:w practice.txt
w means write. So this command will save
the text to a file called practice.txt. Saving a
file does not mean that vi will quit the file.
If you want to save and quit the file, then you have to
issue the command:
:wq
That means, write and quit. So as you can
see, you can use more than just one command, when issuing
commands in vi . If vi should issue
a warning when you run the command :wq , then
you can use the "!" to force it to do what you want:
:wq! In fact, an alternative to using :wq
is by just pressing ZZ in command mode. The
difference is that one is an ex command, the
other is not. So quit vi for now and we will
get into another example.
DELETING TEXT
So far, we have been looking at how to insert text, but
how do you delete them? You will find that you are unable
to delete text that is on a different line by using the
Backspace key. There is a command for deleting text.
First, we will open the file practice.txt using the
command:
xconsole$ vi practice.txt
If you give an argument to vi , it will take
it that you want to open that file and work with it. That
is exactly what we want to do, and so vi will
open it up for you. Stay in command mode, and position your
cursor to the second character of the first line, and press
x. The character should disappear. x will
delete one character at a time. What if you want to delete
an entire line? Position your cursor anywhere on the line
you want to delete and press dd for that. Try it
so that the first line is completely deleted. Now how do
you delete a word? First you must position your cursor to
the first character of the word you want to delete. Then
you use the command dw, delete word.
Notice that you do not need to prefix these commands with
a ":". Rather they are done when the cursor is on
the text you want to perform the operation on, and right
away. Remember to be in command mode for this.
COPYING AND PASTING
One of the most fundamental features of any text editor
should be the ability to copy a certain region of text and
to paste it in another location. vi has this
ability when it is in command mode. Simply move your cursor
over to the line you wish to copy, and press yy to
yank the line of text to the "clipboard". Now there
are two ways to paste text. Pressing p will paste
the text right at the bottom of the cursor, and pressing
P will paste it above the cursor.
PAGE VIEWING
There will be times in vi that you have to
view huge documents and moving through them one line at
a time can get tedious. It is possible to view one page
at a time, or half a page at a time. Pressing Ctrl-d
will move you half a screen down. Pressing Ctrl-u
will move you half a screen up. Pressing Ctrl-f
will move you one screen forward, and pressing Ctrl-b
will move you backwards by one screen.
SEARCHING FOR STRINGS
vi allows you to search for specific strings.
This can be done with the "/ operator. The slash
operator searches forward, whereas the question mark operator,
"?" searches backwards. To show an example of this,
try the command:
/string_to_search_for
If the string exists, the cursor will jump right to it.
Pressing "/" again and pressing the ENTER key will
continue the search forward. If you want to search backward,
just replace the "/" with a "?".
SLIGHTLY MORE ADVANCED BUT COOL STUFF
I said this was a crash course, but I thought I would put
in a few advanced commands to show you how vi
can make your document typing much faster. Let us say you
have a file with the following contents:
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.
Your cursor is at the first character of the first line, as denoted by the
underlined "T". Now let us say you wanted to go to the 5th
character of that line. The command you give, is 5l. That means,
5 characters left. This works for all the navigation
commands, as well as for the other commands like copy and paste. Try it
out.
How about substitution? Let us say you wanted to change the word
"line" in each of the five lines to the word "sentence"? The
quickest way to do this would be to run the command:
:%s/line/sentence/g
Run it and see the magic take place. The syntax for the substitution
command is simply
%s/change_this_word/to_this_word/g
But what if you decided later to substitute the first two lines with the
words "sentence" back to lines? No problem! Use the command:
:1,2s/sentence/line
Amazing huh? You have full control over what you are editing, and you are
doing it at twice the speed that you would normally do on an ordinary text
editor. What has happened here is that instead of using "%" which
means, change all occurrences of to a specific range. "1,2"
means substitute from line 1 to line 2.
The dot "." operator is powerful here in vi . If you
ever had to write 1000 lines of "I must remember to bring my book to
class." for a teacher, then you will definitely love the dot operator.
First of all, fire up vi and write:
I must remember to bring my book to class.
Be sure you press the ENTER key after you type the sentence to add the
newline character. Now for the cool part! Type the command 1000.
and sit back, and watch the show! 1000 lines accomplished in a matter of
seconds. How did this happen? The dot operator repeats the last thing you
did. If you just press the "." without specifying a number, it will
repeat what you just did. Specifying a number means, repeat the last
action n number of times. What? Your teacher wants each line
numbered??? No problem! The command:
:set number
takes good care of that.
CONCLUSION
Well, what you have learned today from this NHF are all the basic things
that will get you going with vi . There is more to learn. A
lot more. But if you can get comfortable with the above we
discussed, it should not be a problem learning the more advanced features
of this text editor.
X_console shellscope@yahoo.com
|