This document is meant to serve as a simple introduction to vi. I've noticed that many people fumble through vi only when they have to, and use other editors, mainly point-and-click, for every day use. As a result, most people don't understand the power and the ease that vi can provide, and when forced to use vi, are barely functional.
Within this document, I'll try to cover the basics in moving within a document, the various editing commands and methods, setting simple environment variables, and I'll discuss Vim, Vi IMproved.
Before I get into the commands used, a few things must be made clear. First of all, vi has two distinct modes of action: command mode and insert mode. From within command mode almost all commands are issued and movement is performed. Vi automatically places the user in command mode upon entering vi, otherwise pressing the escape key will put the user in command mode.
Alternatively, insert mode is used to type any content which the user wants in the document. There are several different ways to enter insert mode, each of which has a specific purpose and action. The insert modes are defined in pairs, always hinting at an order by which the commands are organized. These will be documented later.
Movement within a document is performed not by the now-typical point-and-click method (which I firmly believe is inferior, anyway), but rather by keyboard commands. I find this is more useful, since the hands can remain on the keyboard, rather than reaching for the mouse.
Vi commands have a simple general form:
Note that if n operations are performed on m objects, the action is performed n*m times.
The important basic operators are:
It is probably important to note that a deletion is akin to the traditional cut command in most editors, where the deleted text is placed in a buffer which can be later placed, replaced or otherwise manipulated.
Objects that are acted upon are the same as the movement commands they represent. Some examples include:
When concerned only with the current line, some shortcuts are define to make things quicker. These are cc, dd and yy, which refer to change line, delete line and yank line, respectively. These shortcuts can also be used with the general command syntax, as shown:
There are a few text editing commands that don't really fit anywhere very well, but are still handy to know:
Most of the following commands are useful in making vi interact with the operating system that it runs underneath. This includes saving files, opening files, spawning shells, and quitting vi.
Most of what can be done with macros is beyond the scope of this document, but there should be some mention of the power that is implied with a few simple commands.
The following characters are not normally mapped in command mode by vi,
although I typically use g to transport the cursor to the home position:
Letters: g K q V v
Control Keys: ^A ^K ^O ^T ^W ^X
Symbols: _ * \
Adjustment of the environment that you use can easily make the difference when choosing your default editor. Like most other editors (or operating systems, in the case of emacs), the enviroment is quite configurable, by way of the :set command, which is used as follows:
There are many options that can be changed with the :set command. This table should summarize the important ones:
Option | Default | Description |
autoindent ai |
noai | In insert mode, indent each line to the same level as the line above or below. |
autoprint ap |
ap | Display changes after each editor command. |
autowrite aw |
noaw | Automatically write file if changed before opening another file or executing a UNIX command. |
beautify bf |
nobf | Ignore all control characters during input (except tab, newline and formfeed). |
errorbells eb |
errorbells | Sound bell when an error occurs. |
hardtabs= ht |
8 | Define boundaries for terminal hardware tabs. |
ignorecase ic |
noic | Disregard case during a search. |
list | nolist | Prints tabs as ^I and end of lines with $ |
mesg | mesg | Allow system messages to display on terminal while in vi. |
number nu |
nonu | Display line numbers on left of screen during editing. |
shiftwidth= sw |
8 | Define number of spaces in backward tabs when using autoindent function. |
showmatch sm |
nosm | When ) or } is entered, cursor displays matching ( or {. If matching brace does not exist, the error bell is sounded. |
showmode | noshowmode | When in insert mode, displays the type of insert being made. |
tabstop= ts |
8 | Define number of spaces that a TAB indents during editing session. |
wrapmargin= wm |
0 | Define right margin. If greater than zero, automatically inserts carriage returns to break lines. |
The :set command can also be written into a startup file, ~/.exrc, which is read every time vi is used. This will allow users to override defaults, and thereby customize their environment.
As an example of this, I offer my own .exrc file:
:set autoindent
:set autoprint
:set autowrite
:set nomesg
:set redraw
:set shiftwidth=3
:set showmatch
:set showmode
:set tabstop=3
:set wrapmargin=5
map g 1G
I'm only going to touch on vim in this document, and point any interested people to the homepage for additional information.
Vim affords all the luxuries and comforts of vi, but, like the name implies, is improved and expanded on. Vim offers to the user split screen editing, mouse action and a limited graphical interface. Features that might not be noticed but are available are expanded limits, unlimited undos and redos and text formatting.
The source code for vim is freely available and compiles easily on virtually everything. It is available in CEB 540, and the adventurous can compile it in CEB531.
Vim is started much like vi, but has a few options available to it.
By typing :help upon entering vim, the user is brought to a hyperlinked text file that will instruct the user in the many many features that they may not be familiar with. These include the basic vi commands that are summarized above. Some of the helpful new options to explore are:
I'm not going to list all the useful commands that vim offers, since they are many. I encourage any adventurous people to try vim. It, like vi, can be used on dumb terminals, but offers more flexability.