Crash Course on Running the vi editor
What Vi is:
vi stands for visual, because it was an early text editor that actually showed you the text you were creating and what the resulting changes looked like.
Why you should learn vi now:
Vi is generally available on every UNIX system you might come across for the rest of your life. There are other editors, such as pico, which are much easier to learn (and if you need to get a file composed RIGHT NOW, I suggest you stop reading this file, create the file with pico, and then come back and learn to use vi). However, not every system will have pico or emacs available. It is simple to search for text within a document, and vi is very powerful in regards to replacing text and moving text around.
Lesson 1: INSERT and COMMAND mode:
There are two “modes” in vi. When you first start vi up (type vi) you are in COMMAND mode. Any key you type will be interpreted as a shortcut to a COMMAND. The i key is a shortcut for switching to INSERT mode. The escape key will exit you out of INSERT mode and back into COMMAND mode. Once you are in the INSERT mode, you can type and type and type. Please hit the return key once you reach the end of the line (especially if you are using vi to compose email).
Escape will get you back into the COMMAND mode. You need to be in COMMAND mode to move your cursor (this may be a flashing or solid box or underline, and it is a placemarker for where action will take place), save the file to disk, delete lines, and all the other goodies available.
Again, i will get you back into INSERT mode from COMMAND mode so you can start typing and inserting text.
Lesson 2: The cursor and moving it around
Usually, any arrow keys on a keyboard will work as you might guess, moving the cursor around. (You may get strange character combinations like [[^Y if you press an arrow key while in INSERT mode). You should learn the hjkl cursor keys (they are all next to each other) because not all keyboards have arrow keys:
* h moves the cursor left a character * j moves the cursor up a line * k moves the cursor down a line * l modes the cursor right a characterOther basic movement include:
key where the cursor will end up
—— ————
^ beginning of the current line
$ end of the current line
G end of the file
:# line number # of the file
Lesson 3: Changing text
You probably make mistakes, too. From COMMAND mode, the most popular ways of dealing with mistakes:
key what it does
—— ————
x erases the character that the cursor is at
r replaces the character that the cursor is at
with the character you type next
dd deletes the whole line that the cursor is at
u undo: undoes the last thing you did
(but pressing u again will undo the undo)
Lesson 4: Saving your work
When you type a colon in the COMMAND mode, you will be taken to the bottom of the screen. From here you can issue commands like:
:w file writes the file “file” to disk.
:w writes the file to disk (if “file” is already defined)
:q quit vi
:wq save some typing: same as :w and then :q
Lesson 5: Finding text
When you type a / from the COMMAND mode, you will again be taken to the bottom of the screen. Here, type the text you want to find, then hit return. If the text is in the document, your cursor will move there.
Type n to find the next occurrence of the text you are searching for.
Lesson 6: Search and replace text
Learn this and teach it to two friends.
The basic form is (g stands for global, s for search, and c for confirm):
:g/search-string/s//replace-string/gc
So if I wanted to replace all occurrences of COMMAND in a file with command, I would type
:g/COMMAND/s//command/gc
Vi will underline and found occurrence of “search-string” and wait for you to press y or n , to confirm or deny that specific change. If you don’t want this checking, omit that last c:
:g/search-string/s//replace-string/g
Lesson 7: Some more things to know
When you type a number before issuing a command, the command will be repeated that many times. Like 7dd will delete 7 lines. 5j will move up five lines. Control L (represented in text like this: ^L) will replot the screen.
^G displays the file name, and how many lines it has, etc.
O Opens a new line above the cursor, then sets INSERT mode
o does the same, except below the cursor
a append: sets INSERT mode, so you start typing after the character
the cursor is at.
i insert: sets INSERT mode, but anything you type is inserted before
the character the cursor is at.
:r file READs the contents of file “file” into the current document. If
you omit “file”, the current document will be read in again after
the cursor, which can be very confusing.
:e file EDIT another file “file”, quitting the current document.
Lesson 8: Moving blocks of text around
There is a mystical land called the “buffer”. When you delete text (like with dd) it goes into the buffer. When you delete more text, the newly deleted text replaces the contents of the buffer (the old deleted text goes away for good). You can delete 7 lines with 7dd and then those 7 lines will be in the buffer.
yy YANKs text into the buffer without first deleting it from the text.
7yy will yank 7 lines of text into the buffer.
p PUTs the contents of the buffer on a new line after the cursor’s line
P just like p, but puts it ABOVE the cursor’s line
The contents of the buffer change quite a bit, so you may want to use the move stable “named buffers”:
“ayy YANKs text into a specially named buffer. The buffer’s
name is “a”. Again, inserting a number before the command
will yank that many lines.
“ap PUTs text from buffer a
10“zdd deletes 10 lines from the text and yanks them into buffer z.
Lesson 9: Getting more information and help
The book Learning the vi and Vim Editors is published by O’Reilly and Associates, and is a great source.
Novice users are frustrated by the “man pages” of UNIX systems, but there are tables of commands available by typing “man vi”.
You can email comments to me at helpseries@arekdreyer.com.
Arek wrote this crash course on using ‘vi’ back as a SunOS/Solaris system administrator for the University of Illinois Department of Statistics.