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 by issuing the command:
vi filename
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 aspect of vi to most people is the concept of editing modes. vi has 2 basic modes of operation, command mode, and text (also called insert) 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
(appending text)
I inserts text at the beginning
of the current line
A inserts text at the end of
the current line
o insert a new line below
the current line
O insert a new line above
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.
University of Wisconsin-Whitewater
Web Server and Unix Administration
I am creating this file using vi editor.
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
ndd delete n lines
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
Movement within a line:
A line is not necessarily the same length as the visible line that appears on the screen. A line is any text entered between newlines.
Two usful commands that involve movement within a line are:
0 Move to beginning of
line.
$ Move to end of the line.
Movement by text blocks:
The w command moves the cursor forward one word at a time.
The b command moves the cursor backword one word at a time.
Changing Text:
You can replace any text in your file with the change command c. In order to tell c command how much text to change, you combine it with a movement command:
cw to the end of a word
c2b back two words
c$ to the end of the line
c0 to the beginning of
line.
cc change the entire line.
To replace a single character, use r command.
No editor would be complete without the ability to cut and paste text and vi has 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:
yy put the current line into the buffer
Nyy put N lines into the buffer
P put the contents of the buffer above the current line
p put the contents of the buffer below the current line
u undo the last command
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
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).
Replacing text
In order to do this you must first be in command mode (by pressing the ESC key) then press the : key
:s/sam/SAM/
Substitute "SAM" for "sam" on the current line
:s/sam/SAM/g
Substitute "SAM" for every word "sam" on the current line
:1,10s/sam/SAM/g
Substitute "SAM" for every word "sam" on lines 1-10
:1,$s/sam/SAM/g
Substitute "Sam" for every word "sam" in the entire file
Display line numbers:
Use the command
:set nu
This will display line numbers.
To remove line numbers, use
:set nonumber
command.
Spell checking
Although vi has no spell checking abilities you can spell check your documents from vi using the ispell package which is included with a wide variety of distributions. You can save and exit vi and issue the command:
ispell filename