The manual Page
Version française
   
index | glossary | news | downloads | links ]
  Unix
Unix rights
manual pages
Unix commands
how to use vi
cron
rm
 
news
glossary
links
downloads
 
credits
contact
 
 
search
 
last update
19/02/2003
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Mesurez votre audience


  Vi

What is vi?

For many people, vi (to be prononced as 2 separated lines) is a scary word... vi is actually a simple but powerful text editor that comes with all Unix systems. If this program seems to be so frightening it is because it is far from being user-friendly and intuitive, but it is *extremely* efficient and powerful.

To start vi, just type 'vi' at a command prompt. You may type 'vi' followed by the name of the file you want to open. There are a couple of possible options, but we usually never use them.

Use, principle

What makes vi so special is the way to use it. There is no menu or whatever, and any kind of interaction is actually a blind interaction (you rarely see what you are typing). vi uses to different modes to work: a insert mode to add text in the file, and a normal mode use to type vi commands and interact with the program (save the file...). The insert mode may be recognized by a "-- INSERT --" message at the bottom of the screen. When it this message does not appear, you are certainly in the normal mode.

The most 2 important keys with vi are 'Escape' and 'i': we use the Escape key to leave the insert mode and enter the normal mode or to cancel a current action (we even strike this key 3 or 4 times to really be sure we are in the normal mode), thought we use the 'i' key to enter the insert mode.

The key to vi is to know how to use its commands. All interesting actions are performed outside of the insert mode. The basic thing to know before going furter is to know how to go from one mode to another.

Useful commands

These commands are executed in the normal mode. Just strike a couple of times the Escape key to enter this mode.

Most relevant commands are short (just one key or CTRL-key for instance), and in this case keys are enough. However, some commands are long (for instance, when we want to save a file, we may need to specify its name), and then we have to type a ":" before going further. In this latter case, the complete command is displayed at the bottom of the screen.

Standard commands

command description
i enter the insert mode
Escape enter the normal mode
:w file_name save the current text file in file_name. May be used to save the current file with a new name. If no name is specified, the current file is saved as the file open with 'vi file_name
:q quit vi
:q! quit vi but without saving changes
:wq or :x quit vi and save changes
u undo the last action

How to move around

The Linux version of vi supports the arrow key to move around at any moment. However on many Unix systems, moves can be performed only in the normal mode and with the following keys (these keys are also available with Linux):

command description
j up
k down
h left
l right

Some other keys may be used to move faster in the file:

command description
w jump to the beginning of the next word
b jump to the beginning of the previous word
e jump to the end of the next word
0 (zero) jump to the beginning of the current line
$ jump to the end of the current line
:N with N an integer jump to the line number N
:$ jump to the last line of the file

It is possible to combine the letters w, b and e with numbers (to be specified before, like 34w for instance) to jump the given number of words in the specified direction.

Insert/delete characters

This table gathers some useful keys used to delete or insert words:

command description
i enter the insert mode
o (lowercase o) insert a new line below the current one and enter the insert mode
O (uppercase o) insert a new line above the current one and enter the insert mode
a enter the insert mode, but start after the current character
x delete the current characte (character under the cursor)
dd delete the current line
dNd with N an integer delete the next N lines including the current one
D delete everything from the current position of the cursor up to end of the current line
r followed by a character replace the character under the cursor by the specified one
R replace all characters by those we type afterwrads

Search

command description
/word search for the word "word" in the rest of the file
n search the next occurrence of the previously searched word
N search the next occurrence of the previously searched word but in the first part of the file (before the cursor)

Substitutions

vi supports commands that can be used to handle regular expressions. The programmed character substitution is one of those. Let's have an example. Let's suppose we want to replace the word "foo" by "bar". The standard command is:

:s/foo/bar

This will make a change only on the first occurrence on the current line. To make a change on every occurrence of the current line, on uses:

:s/foo/bar/g

Fianlly, if we want to change all occurrences of the file, we do:

:%s/foo/bar/g

The '%' symbol means "for every line"; one must put a 'g' at the end of the command to make the substitution happens completely on every line.

Miscellaneous

This table gathers some intersting commands that cannot be classified in the previous sections:

command description
:se nu display line numbers
:set ff=unix or set ff=dos Linux only used to change the format of the end of line character (respectively to set it to the Unix or Dos style). Very convenient when we open a file in the Dos format and when we want it in the Unix format.

printable format printable format



Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.