Vi Tutorial

Although vi has a steep learning curve, it is an excellent choice of editor when working in a Unix/Linux environment.  One of the chief advantages is that if you know how to use it in a lab, you know how to use it at home since all commands are text based.  The following is a guide to the features you're most likely to need when starting out in vi.  At the end of the guide are some links to guides for users who want to explore vi's advanced features.

Vi vs vim:
Vim stands for "vi improved" and is a much more user friendly version of vi.  If vim is available (it is on most Purdue servers), it'll make your life easier to use it rather than vi.  Everything mentioned in this guide applies both to vi and vim.

Opening vi:
Opening a file in vi is very simple.  To do so, type "vi" (or "vim"), a space, the file's name, and return in your Unix/Linux shell.  You can simply open vi by typing "vi" in a shell, but this is not recommended.

Different modes:
vi has several different modes that it operates in.  The two most important are input and command.  Input mode is where you'll do all your typing.  Command mode is where you instruct vi to do things like save your file or exit.  When you start vi, it will be in command mode.  To type, you will need to enter input mode.  To exit input mode, you will need to hit escape.  All of the commands presented in this guide should be in command mode.

Entering text:
When you want to type, you will need to enter input mode using one of the commands that follows.  Remember to exit input mode by hitting escape.  Note that older versions of vi will not allow you to move around while in input mode.  Also, if you delete text, some versions of vi will not show the text as deleted until you hit escape.

i - enter input mode where everything you type will be entered before the cursor
a - enter input mode where everything you type will be entered after the cursor
shift + i - enter input mode at the beginning of the line
shift + a - enter input mode at the end of the line

Moving around:
Although most versions of vi now allow you to use the arrow keys, some old versions will require you to use the keys h, j, k, and l to navigate around the screen.

up arrow or k - move up
down arrow or j - move down
left arrow or h - move left
right arrow or l - move right
: line_number - move to a certain line number
control + f - move forward a page
control + b - move back a page

Saving and exiting:
When you're done editing your file, you'll want to use the following commands to save and exit.

:w - write (save) your file
:w file_name - where file_name is what you'd like to save your document as.  NOTE: future ":w" commands will not write to this file name; they will write to whatever file name you used when you opened vi!
:q - quit vi (vi will not let you quit if you haven't saved)
:q! - quit without saving changes
:wq - write your file and then quit

Cut, copy, and paste:
It's hard to get much done without these three essential commands.  The basic commands are "x" for cut, "y" for copy, and "p" for paste, but there are many variations.

x - cut the character the cursor is on
X - cut the character before where the cursor is
dd - cut the current line
number + dd - cut number lines
yy - copy the current line
number + yy - copy number lines
p - paste where the cursor is
shift + p - paste before the cursor

Undo/redo:
If you ever make a mistake, undo can come to your rescue!  But, there's one MAJOR caveat.  Old versions of vi may not have redo.  So be careful not to undo work you want to keep.  (All forms of vim have undo.)

u - undo
control + r - redo (not available in all versions of vi!)

Searching and replacing:
There's a lot of advanced ways to search, but the following is a very simple way to do it.  Searching will search anything in front of the cursor.  So, if you want to search your entire file, you will want to go to the first line ":0" and then search.  This is barely the tip of the iceburg for vi searching and replacing.

/ + search_string - search the file for search_string (this is case sensitive)
:%s/ + search_string + / + replace_string + /g - this will search for search_string and replace it with replace_string throughout the entire file.  An example of this command could be ":%s/vi/vim/g" which would
change everything in the file that currently says "vi" to say "vim".

Advanced features:
There are a lot of advanced but extremely helpful features like visual mode and advanced find and replace, but this document isn't going to cover these.  Instead, check out the wealth of knowledge at some of the following sites.  Also, remember that google is your friend when you have specific questions about vi and vim.

Additional vi references and tutorials:
http://tnerual. eriogerg.free.fr/vimqrc.pdf - a huge cheat sheet of vim commands
http:// newbiedoc.sourceforge.net/text_editing/vim.html - A very extensive beginner's guide to vim.
http://www.cs.fsu. edu/general/vimanual.html - the vi manual
http://www.eng.hawaii. edu/Tutor/vi.html - a guide on how to master vi