VI-Getting started
Created | Updated Jan 28, 2002
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 |
| ||||||
---|---|---|---|---|---|---|---|
Moving by words |
| ||||||
Moving with search |
|
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 adress | number| 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 text | i 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 text | a appends text after the cursor A appends text at the end of the line (equivalent to $a) |
Overwrite text | s 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 |
---|