Home Linux Info Forums How-Tos Links Tutorials Editorials Reviews Commercial Ports Counter Submit About Book Store Contact
What do you think would help Linux out the most_
Better documentation
User interface
More software
Better hardware support
More agressive advertising by major distributions
None of the above
Current Reviews
Commercial Ports Counts
Name# Of Votes
HP 970 Printer Driver208
Adobe Photoshop581
Quicken628
Microsoft Office413
Ever Quest122
Macromedia Dreamweaver512
Shadowbane50
Printshop from Broderbund97
Visonneer parellel port scanner124
Adobe Acrobat(not Reader)406
Lotus Notes Client254
Microsoft Project112
Corel Draw383
Homesite203
Peachtree Accounting126
PrintMaster77

Mystery Linux Links
Mystery Link #1
Mystery Link #2
Mystery Link #3
Mystery Link #4
Mystery Link #5
Random Linux Links
Central Texas Linux User's Group
t3q security
Penguin Computing
LOSURS (
Terra Soft Solutions

An Introduction to vi

I was first introduced to vi in 1988 and I hated it. I was a freshman in college and majoring in computer science. I was taking the intro level FORTRAN course. We worked on dumb terminals connected to the schools server via a telnet connection and vi was the only editor available, and it was foreign to me. VI seemed archaic, complicated and unforgiving. I could handle the programming assignments with ease, but working in VI was a constant battle and a source of unending frustration. It is now 12 years later and I love vi, in fact it is almost the only editor I use. Why the change_ I actually learned to use vi. You see, the university had neglected to actually show us students new to the UNIX environment how to use vi beyond a few very simple commands. Now I see vi for what it really is, a powerful, full featured, and flexible editor. I use it all the time, in fact nearly all of this web site was constructed with vi.

Why would someone want to use a console editor when so many excellent GUI editors are available with many powerful features such as spell-check, auto formatting, auto-save, and other features_ Well, thats one of the reasons I use vi, it is bare bones and does not have all those so called advanced features that supposedly makes it easier to edit documents. At school we are forced to use Windows NT and MS Office and it is frustrating to me to have MS Office guess what I want, most of the time it is wrong and trying to fix the document usually winds up with me messing the document up even more. Another thing I hate about WYSIWYG editors is that you have to take your hands away from the keyboard in order to perform some command by manipulating a menu or icon with the mouse. Yes, I know, there are short cut keys but I can never seem to keep them straight in my head.

Another excellent reason to learn vi is if you do not have a computer powerful enough to run a GUI. I have an old 386 laptop with 4MB of RAM and a black and white LCD screen. Useless by todays standards, however it runs Linux quite nicely. No, I cant run X but that is OK. I used this laptop quite extensively to write papers for school during 1998 to 1999. I was working full time and going to school full time, I had lots of papers to write and little time to actually write them. However, I would take my ancient laptop to work with me and begin writing the assignments using vi. When I got home I would doctor them up with a WYSIWYG editor, print them out and turn them in the next day.

Starting vi

The vi editor can be started in a few different way. In order to start vi with no file currently loaded and ready to edit simply issue the command:

vi

You will be presented with a blank screen with a column of tildes running down the left side. You may also start vi with a filename or series of filenames by issuing the command:

vi filename1 filename2 ...

This will start vi with the first filename in the editing buffer and ready to edit. Another useful way to start vi is to not only tell it the filename you want to edit, but also the line number you want to start editing on, you can do this with the following command:

vi +n filename

where n is the line number you want to start editing on.

Editing modes

The most foreign htmlect of vi to most people (including me when I began using vi) is the concept of editing modes. vi has 2 basic modes of operation, command mode, and text (also called insert) mode. Although some people include the : (colon) mode as a separate mode however it is really an extension of command mode.

When you first start vi you are in command mode. You may also enter command mode at nearly any time by pressing the ESC key. When you are in command mode any characters you type from the keyboard are interpreted as a command to vi, in other-words the characters will not display on the screen or become part of your document. In order to type characters into your document you must enter text mode.

In order to get into text mode you must first be in command mode. Just to make sure press ESC. There are a few ways to enter text-mode depending on what you want to do. Here is a run down of the commands to enter text mode.

i	inserts text before the cursor
a	inserts text after the cursor
I	inserts text at the beginning of the current line
A	inserts text at the end of the current line

By far the most common method of getting into text mode is using the i command. Go ahead and press the i key now and you should be able to type in text, you can use the backspace key to delete any mistakes. If you want some sample text to type in, then type in the following, press enter at the end of each line.

Snakes is small,
Not tall at all,
They ain't even got no stature,
And when they stare,
They take great care
To always look right ature.

Sorry for the bad poetry, it is not mine, but I am a reptile fan. While in text mode you can use the cursor keys to navigate around the document. Now, to get back into command mode we just press the ESC key again. You may also navigate around the document in command mode via the cursor keys, however, while in command mode you may also navigate the document using the j,k,h and l keys. This is useful if you are using a computer that does not have cursor keys or the version of vi you are using does not support the cursor keys:

j	move one line down
k	move one line up
h	move one character to the left
l	move one character to the right
Remember though, in order for the above keys to work you must be in command mode, just press ESC to get there.

Getting rid of text
In most cases you can delete text in text mode by pressing the backspace key, however vi provides many commands (available from command mode) to delete text, they are:

dd	delete the current line
dG	delete from the current line to the end of the file
d^	delete backwards from the current line to the beginning of the file
d$	delete from the cursor to the end of the line
x	delete the character under the cursor
X	deletes the character to the left of the cursor

Moving text around
No editor would be complete without the ability to cut and paste text and vi as many options to do this. If you are running vi in an xterm it can be very simple. Just use the mouse to highlight the text then simply move the mouse cursor to where you want the text to be inserted and press the middle mouse button, you must be running gpm in order to do this. However, if you are not running vi within an X session you have many commands to cut and paste, they are:

xNdd	delete N lines and move them to x buffer (x may be any single character)
xnyy	delete N lines and append them to x buffer (x may be any single character)
xNyy	put N lines into x buffer (does not delete the lines)
xp	put the contents of buffer x after the current line
P	put the contents of the unnamed buffer above the current line
p	put the contents of the unnamed buffer below the current line
yy	put the current line into the unnamed buffer
Nyy	put N lines into the unnamed buffer
u	undo the last command

You must prefix any named buffer with the " chracter.  For example, to place the current line into a buffer named a you would use the following
command:

"ayy

How to search for text
Searching for text in vi is easy, you only need three commands:

/searchstring	search forward for a string
_searchstring	search backward for a string
n		find the next occurance of the last search
If your search string is found the cursor will be placed at the beginning of the found string.

Saving and exiting vi
In order to save your text or to exit vi you must enter the colon mode. In order to do this you must first be in command mode (by pressing the ESC key) then press the : key. You will see a : appear at the bottom left of the screen, you may use one of the following commands in colon mode to either save or exit vi:

q		quit (will be given a warning if changes have not been saved)
q!		immediately quit without saving changes
wq		quit and write the changes to disk
w filename      write the buffer to filename (filename is optional)
r filename	read filename into the buffer and insert it after the current line
e filename	read in filename for editing
You will notice that I also included the e command which is used to read in a new file into the editing buffer. Remember all of these commands must be used in colon mode by pressing the : key while in command mode (it really is not as confusing as it sounds).

Spell checking

Although vi has no spell checking abilities you can spell check your documents from vi, I use the ispell package which is included with a wide variety of distributions. You can use ispell in two different ways. You can save and exit vi and issue the command:

ispell filename

or you can invoke ispell right from vi using the ! command. The ! command must be issued from the colon mode (press : then !) and allows you to execute any shell command, not just ispell. So, to invoke ispell on a fictional document I am currently editing I would use:

:!ispell document.txt

This fires up ispell and allows me to spell check the document, when ispell quits you will get a message from vi saying the file has changed, simply re read it into the editing buffer with the e command:

e document.txt

That brings in the changes ispell made on the document.

Well, thats about it, this should be more than enough to get you started. If you want to know more you can view the manpage for vi (man vi). If you use it long enough you will find yourself doing weird things, such as pressing ESC-:-wq in a WYSIWYG editor thinking that will save and exit the editor! If you really start using vi on a regular basis you will get addicted to vi.