The vi editor
Okay, why on earth would anyone want to know how to use vi?
Well, although unix's tend to have lots of editors available, and virtually all
of them are so much easier to use than vi, vi is the only screen editor garanteed
to be on a system. Imagine this, you've just arrived someplace to start editing
code, and your usualy editor (DopyEzEd) hasn't been installed on this new system,
what do you do?
Do you
- Refuse to edit code and get fired.
- Waste a day finding the binaries for DopyEzEd that the system can run, or
- Use vi, which you learned on the off chance that you'd end up in this situation!
Well of course the third option is the best... especially if you don't get paid until the
job's done!
Starting vi
To start editing a file in vi, you would type the command
vi [file-name(s)]
If the file exists you will get its contence displayed on the screen, otherwise
you'll see an empty screen with lots of ~'s down the left hand side.
Quiting vi
The most important command you need to know at this point is how to leave vi without affecting the file you're editing in any way. Do this with :
:q![enter] (press the escape key, the ':' 'q' and '!' keys, then then press [enter])
Okay, let me explain a few things quickly... vi works in two modes, the first
is command mode, where you can delete and manipulate text (cut n paste etc),
this is the mode you start in. Then there is insert mode, where text typed
is added to the file at the current cursor position. Actually there is a third
mode, Ex mode, vi uses the unix command ex to do some interesting things, which
someone might want to discuss in another tutorial?
To get from command mode to insert mode you press one of the following keys...
- i - insert at current cursor position
- I - insert at the start of the current line
- a - insert after the current cursor position
- A - insert at the end of the current line
- o - insert on a new line below the current line
- O - insert on a new line above the current line
- R - Overwites preceding characters
- C - deletes text to end of line and then you can add text
Once in insert mode you can move about the document with the cursor
keys and continue inserting at various positions around the file. Don't count
on this though, as it varies between vi's! Deleting should also work, but again
this depends on your terminal, which you should try setting to VT100.
To get back to command mode from insert mode, press ESCAPE, if that doesn't
work try pressing ctrl-c, if _that_ doesn't work, yell for help!
Commands. (remember to press ESCAPE to get into command mode)
Quitting
- :q! - Quit without saving any of the changes
You can try quitting with a [ESC]:q, which will not let you quit if you've
made any alterations to the document.
Saving
- :w - to save and remain in the editor.
- :wq - to save and quit the editor. You can also use [ESC]ZZ
- :w [file_name] - save the file as [file_name]
Reading & status
- :e [file_name] - edit new file
- :r [file_name] - read the file and insert it after the cursor
- :f - prints the name and status of the file
- :f [file_name] - change the name of the active document
- :n - moves on to the next file (when multiple files are specified)
- :rewind - return to editing the first file
Moving
You may find that the cursor keys dont work, so, while in command mode you can use the :
- j - up
- k - down
- h - left
- l - right
keys to move about. Try changing your shell's terminal type with 'set term vt100' or TERM=vt100
before starting the vi editor which may activate cursor keys. Additionally:
- :[num] - move to line [num]
- [num][cursor-key] - move [num] characters in [cursor-key] direction
Searching
- /[keyword] - search for an occurance of [keyword]
- /[ENTER] - search again for the last entered [keyword]
Other commands.
- r[char] - replace the letter under the cursor with [char]
- [num]r[char] - replace the next [num] letters with [char]
- x - delete a single letter under the cursor.
- X - delete a single letter forward of the cursor.
- [num]x - delete [num] number of of charaters
- dd - delete the current line
- [num]dd - delete [num] number of lines
- [num]d[cursor-key] - delete [num] characters or lines in the direction
- u - undo last operation
The characters are deleted from the document and moved into a buffer from
where they can be pasted using the p command. Be warned though that the
buffer is deleted if you make virtually any other command!
Thats enough to get you going, there are many other commands available in
vi, and if you happen to be running linux, DOS or have and Amiga, there
is also a vi-clone called vim, which is an extended version of vi!
There is a tutor program for vi that you can install, it's called vilearn.tar.Z(compressed tar file)
I'd like to thank Lisa Jayne for the majority of this document
[Return to tutorial page] [Return to main page]
Page last updated by John-Lucas Brown 03-Apr-1997