VI-Getting started

1 Conversation


This page is meant to introduce you to VI, the ubiquitous editor. There are numerous tutorials on the net to deal with this topic, but H2G2 would not be complete without it, so I will try to have a go at it. If you have inspiration to add, please do.

Modal editor


VI has different modes. This has a simple reason: there are lots of functions and VI was originally designed to work on terminals. Because VI had such a good design, its core was never altered and VI still relies mainly on the standard keys of the keyboard: every command is mapped to codes having an ASCII code lower than 128. To accomodate the plethora of functions with the limited number of keys available, three modes were introduced: command mode, insertion mode and line command mode.


In command mode, you can move around your text, issue commands like cut and paste and go into insertion mode or line command mode. Most of these commands are executed immediately upon hitting the corresponding key.


In insertion mode, the text you type is inserted directly under the cursor. You have no possibility of moving the cursor other than typing text or erasing it. This has been alleviated in newer clones of VI, but beware of growing used to this commodity: when you use an older version, there is no telling what happens when you hit one of the cursor keys. You can switch back from this mode to command mode by hitting the ESC-key.


In line command mode, a subset containing the most important commands of EX gives you access to still more functions.

Getting simple tasks done


In this entry, only simple editing tasks will be explained. More
difficult subjects will be described in an entry of their own
(pattern matching, substituting text, copy & paste)

Moving


This works only when in command mode. Each of these commands may be prefaced with a number. This number indicates the number of repetitions: 5l moves the cursor right by 5 characters, 5/text[Enter] moves forward to the fifth occurence of text starting from your actual position.
Equivalent for the cursor keys
j
h l
k
Moving by words
Forwardw stops at any non
alpha-numeric character
W stops only at spaces or tabs
Backwardb stops at any non
alpha-numeric character
B stops only at spaces or tabs
To the end of the worde stops at any non alpha-numeric
character
E stops only at spaces or tabs
Moving with search
In the same line

f x moves forward to next
occurence of x
t x moves forward just before
the next occurence of x
F x moves backward to next
occurence of x
T x moves backward just before
the next occurence of x

In the whole file/anytext[Enter]: move
forward to next occurence of
anytext
?anytext[Enter]: move
backward to the next occurence of
anytext
anytext is a search
pattern and may contain wildchars and
metacharacters fot complex matching purposes


These commands are also moving commands, but prefacing them with a number is not always possible, or does not have the repetitive effect mentioned above.

Moving to an absolute adressnumber| moves to the column given by
number (| is the pipe symbol
(ALT+124, number 124 in the ASCII code))
numberG moves to line
number. When no number is given, the
editor moves to the last line in the file
Moving, line related_ (underscore) moves to the first non space
char of the line (no number preface possible)
0 (zero) moves to the first column (no number
preface)
$ moves to the end of the line
(number$ moves to the end of the line
number-1 lines lower)

Writing text


There are lots of possibilities to switch from the command mode into
insertion mode. Whenever you are in insertion mode, you can leave it
with the ESC-key.

Insert texti inserts text under the cursor
I inserts text in front of the first character of
the line
o opens a new line below the cursor and switches
into insertion mode
O opens a new line above the cursor and switches
into insertion mode
Append texta appends text after the cursor
A appends text at the end of the line (equivalent
to $a)
Overwrite texts substitutes the character under the cursor with
the new text (numbers substitutes
number characters with the new text)

cmove substitutes the zone defined by
move with the text.
move is any valid move. When the cursor
movement spans more than one line, the change affects
complete lines (starting line included). If you preface
the command with a number, it is used for the
move: 2c2j is equivalent to
c4j.
rchar replaces one character with
char
(numberrchar replaces
number characters with
char)
R puts you in overwrite mode

Saving your work and leaving


Leaving VI is probably one of the main reasons why new users
dislike this editor. When you start the editor to have a quick
look at its abilities, it is a sobering discovery that no usual
key combinations terminates the editor. The following commands
are all valid for command mode (press ESC to
be sure you're not in insert mode).

Saving text:w writes the file (strictly speaking the
: introduces line command mode).
It is possible to save only parts of the text and to
specify a filename as in :10,20w foobar
that writes lines 10 till 20 into the file foobar.
:x saves the text and leaves the editor, same
as :wq
ZZ or :q quits if the text has not
been modified since the last saving operation
:q! quits without saving


Bookmark on your Personal Space


Entry

A309188

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


Written and Edited by

References

h2g2 Entries

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more