Vi(pronounced vee-eye, short for
visual editor) is, in the UNIX world, the truly ubiquitous editor.
Whatever the vintage, flavour and size of your system, you can be
confident vi will be among the available tools.
Aquiring basic vi skills is among the most productive single steps
you can take in order to enable yourself to do something
useful with your UNIX account.
There have been sightings of IT-professionals using it as
their sole text editing tool.
On the downside, vi
requires you to memorize a number of keystroke combinations, that might take
some time to get used to. Still, those same combinations are used in other
useful tools found in a UNIX environment, which definitely turns out to
be a boon if you spend a lot of time in the shell.
To run vi, just enter:
$ viNote.
Ok, vi started.
What you have before your eyes is a column of tilde
('~')characters. This is vi's
representation of an empty document.
The interface can be said to be Spartan, but there's actually
even worse stuff out there.
Typically, there are at least two sorts of things one would expect to be doing
with a text editor: type text, and give it commands (open or save documents,
exit...)
Vi has two corresponding modes, which we may call
insert and
command respectively. In order to switch between these two modes,
you use diverse key combinations.
Vi starts in command mode. Profit from this situation, and learn your first
command, which will enable you to get out of there. Type:
:qThat's it: colon, 'q' and line feed. Out you go.
:q!Another eample of the usage of !:
:w!This will force wrinting to a write-protected file.
Alritee, that's enough now. It's time to save the fruit of your typing
efforts. You will need to switch back to command mode first. You always
do that by pressing esc.
That's somewhere in the upper left portion of most
keyboards I have seen of late.
Now enter the write command (':w') followed by
a name for the file you save your text to:
:w myfileThat is: colon, 'w', space, file name and line feed.
$ cat myfileAnd now...
There is a wealth of commands allowing you to move (the cursor!)
through the text in a variety of ways.
In order to have a look at the commands as they are mentioned, you should
open a sizeable document first. If you already have something you can
use, fine. Otherwise, you could make a copy of, for example /etc/termcap, and
use that as a scrap book.
Any which way, start vi and open your file.
Do it now, in one single step:
$ vi fileOr you can start vi as done earlier, and then use the edit command:
:e fileNow move around one character at a time, in the four directions left, down, up, right by just pressing the keys h, j, k, l respectively.
You can move the cursor by word boundaries by pressing the keys
w(forward one word), and b(backward one word.)
Moving around by sentence or paragraph is somewhat less intuitive. You use
round parens ( '(' and ')') to move to the
beginning of the previous and next sentences, respectively. You use curly
parens ('{' and '}') to move to
beginning of the previous and next paragraphs.
One feature that is particualrly interesting for developers is vi's ability to
have the cursor move by text block as defined by parens pair:
if you position the cursor on any kind of parens character,
you can use the command '%'
to jump to the matching opening or closing parens. It
goes without saying that this is useful with huge blocks or
when the structure of nested blocks gets complicated.
Then again, that could be the time to consider
refactoring the code you are working on instead... ;^)
For those of you who are familiar with regular expressions, it will not be a
surprise that vi's command to move the cursor to the
beginning and end of a
line are '^' and '$' respectively. If you
mean to move up and down by line, and you think that the up and down command
mentioned above don't cut it, you can use
'-' and '+' to move to the
beginning of the previous and next lines respectively.
If you are in a hurry, you can move up and down by half-screens
(Ctrl-U, Ctrl-D), or by whole screens
(Ctrl-B, Ctrl-F.)
You can move to a specific line by typing the line's number followed by
'G', or by typing colon, fllowed by the line number:
:69
In the unlikely case you want to correct the text in your hands, here's a few relevant commands. Firstly, you erase one character, the one currently under your cursor, by the command:
xYou can erase several characters at a time, starting from the one under your cursor and forward, by typing the number of characters to be deleted before the 'x' command:
666xThe majority of vi commands can be combined with numbers thusly, to multiply the number of times they are applied to the text by the editor.
dwand to erase, say, six words in one swoop:
6dwWhich shouldn't surprise you if you paid attention so far. To erase the whole line your cursor is on, use:
ddEven 'dd', as most commands in vi, can be instructed to act x times by prepending a number:
96ddwill erase 96 lines from the cursor's position.
uYou are stuck with a one-level undo in most versions of our fine editor. Think twice cut once®.
Pto paste before the cursor, and
pto paste after the cursor.
y20lThat is: "Yank 20 characters to the left". Horribly bothersome for long words. In this case, we should be more creative and do:
ywinstead. That is "yank one word". Guess what you do in order to yank several words?
:s/pattern/substitutionThis will substitute the first occurrence of pattern with substitution. If you want to substitute all occurrences of pattern in your current line, you specify that by doing:
s/pattern/substitution/gwhere 'g' stands for 'global'. Apply to the next 13 lines by doing:
s/pattern/substitution/g13You tell vi to apply the pattern substitution to the whole file this way:
:%s/pattern/susbstitution/gThe '%' is taken by vi to stand for the open file. Weird? I agree.
:%s/pattern/susbstitution/gc
:.,+3!sortThat is: from the line we are on ('.') and three lines forward, filter with 'sort'.
:.,+3! tr '[a-z]' '[A-Z]'
:r dateThat is read the output from date.
| vi file vi file1 file2 filen... vi +n file vi +/pattern file |
edit file edit files sequentially edit file, jumping first to line n edit file, jumping first to the first occurrence of pattern |
| h, j, k, l w, b (, ) {, } % |
Left, down, up, right by one character forward, backward by one word beginning of previous, next sentence beginning of previous, next paragraph to matching parens |
| x dd y[ndirection] yw yy p, P |
erase character erase line yank ntimes in direction yank one word yank one line put buffer's content after, before cursor |
| /,? n, N |
Search forward, backward repeat search, repeat by inverting search direction |
| :q :q! |
quit file. quit file. force exit if the text has not been saved |
| :x, :wq :wq! |
Write, save if needed, and quit file. As above, but force save if the file is write-protected |
|
repeat last command indent left, right read into text escape to shell and execute cmd |
i
The VI Pages - All About VI and its clones
The VIM (Vi IMproved) Home Page
The vi Powered! logo (U.S. Mirror)
There is also an excellent nutshell
book about vi.
Ah, and don't forget to do man vi!.
The '$' character here stands for the prompt the shell you
are using provides you with. It could be some other character, or look
completely different. You don't want to type the '$', in other words.
Ask your local guru if in doubt ®.
If nothing happens, or if your shell complains, for example to the effect that
the program is not to be found, well, that's your sysop's department. Give
him/her a call, get a cup of coffe and relax.