Learning the vi Editor/Getting acquainted
Introduction[edit | edit source]
Overview[edit | edit source]
vi is a powerful editor that is ubiquitous amongst Unix and Unix-like operating systems, but is available on many other operating systems, even on MS-DOS, Windows and the Macintosh. If not the original vi, there is usually at least a good clone available that runs on your system. Even if you use another editor you must have a passing knowledge of vi as an administrator. Sometimes vi is the only editor available when your computer crashes leaving a minimal system for you to repair.
vi, pronounced like 'vee eye', was originally written by Bill Joy for BSD Unix in Berkeley in 1976 and became quickly part of many vendor-specific versions of the (at that time) original AT&T Unix. It was later directly added to AT&T's System V Unix, too. Bill Joy later went on to co-found Sun Microsystems, and became the company's Chief Scientist at that time. vi stands for visual and was an enormous improvement of the classic Unix editor called ed. ed is a line-editor. If you are still familiar with MS-DOS, then you may know the MS-DOS edlin editor. ed is similar, although more powerful than edlin, which doesn't mean much.
vi also has a line-mode, called ex. In fact, one can argue that the program is indeed two editors in one, one editor called vi, another called ex. It is possible to switch between line and visual mode during editing. It is also possible to choose the mode during startup. However, pure usage of ex is rare. The visual mode is the prevailing mode.
Although vi stands for visual, classic vi is mainly operated via the character keys, and not via the mouse or the cursor keys. Once you are used to this, it becomes extremely convenient, because there is less movement of the hands to the cursor keys or mouse involved.
vi also served as a kind of incubator for Unix's terminal control capabilities. Because of vi's need to control the terminal and the many different types of terminals at that time, the termcap (terminal-capabilities) database was introduced (later replaced with the more flexible terminfo database). vi's internal high-level screen control library was later separated, and became curses - the Unix standard library for CRT screen handling.
Conventions[edit | edit source]
- A single character, such as 'a' or '1'.
- <ESC>, <Ctrl-[>
- Indicates that the Escape (Esc) key on your keyboard should be pressed, which is identical to Control and '['.
- Indicates that the Carrier Return (Enter) key should be pressed.
- Indicates that the Tabulator key should be pressed
- <Ctrl-x>, <C-x>
- Indicates that the Control key and the 'x' key should be pressed simultaneously. 'x' can be almost any other key on your keyboard.
- <Shift-x>, <S-x>, <X>
- Indicates that the Shift key and the 'x' key should be pressed simultaneously
- <Meta-x>, <M-x>
- Indicates that the Meta or Alt key and the 'x' key should be pressed simultaneously.
- :quit, :q
- An Ex command. started with <:>, followed by the command and ends with <CR>. For many Ex commands there is a long form (:quit) and a short form (:q).
- :set nocompatible
- represents a setting.
- strlen ()
- represents a function.
- /pattern/, ?pattern?
- A Search pattern. Search pattern in vi are regular expressions.
- :ranges/search/replace/options, :global /pattern/ delete
- A Search pattern combined with an Ex command.
All commands in vi are case sensitive.
- Sometimes references to Unix commands are used in this book. On first occurrence such a name of a command is written in the typical Unix style. This style consists of the command's name followed by the section of the manual pages in which the command description can be found, in brackets. E.g. sed(1) refers to Unix's sed command which is usually documented in section 1 of the Unix manual pages (sed is the Unix stream editor; a tool for manipulating text without user interaction).
Getting vi if you don't have it already[edit | edit source]
If you're running a Unix system, or a Unix-like system (for simplicity from now on we will refer to both as a "Unix system"), such as a BSD or Linux distribution, or even Mac OS X, you're sure to have vi or one of its variants on your system.
Noted vi variants[edit | edit source]
As mentioned, vi has a number of variants. They have been created because vi was only available on rather expensive Unix operating systems. Although vi itself, as well as nvi, was created in Berkeley for the free BSD Unix variant, usage of BSD Unix required an original AT&T Unix license (this has later changed, see below). Original vi, for example, used code from AT&T's ed(1) editor.
Over time, BSD replaced much of the original AT&T code up to the point where today there is no such code any more in BSD, and an original Unix license is no longer needed. As part of the effort to replace all AT&T code in BSD, Keith Bostic undertook the work to create a clone of vi that was free of AT&T code, called nvi. nvi then became BSD's standard vi instead of the original vi. Another vi clone is Elvis, which was written by Steve Kirkendal.
Over time, nvi was enhanced – for example, supporting multiple windows – but originally it was not supposed to be an enhancement, 'just' a pure clone.
BSD's original vi (with the ed code inside) lives on as the vi which is distributed with System V Unix, because AT&T decided a long time ago to take it from BSD and add it to the official Unix. Of course AT&T didn't have a problem with an AT&T Unix license, so they probably never replaced the ed code inside the original vi.
Yet, some find nvi still to be too minimal, and so vim was born. vim (vi-improved), is another effort to extend vi's capabilities. Unlike nvi, vim goes even further to extend vi's capabilities. However some find that vim is often too much. vim comes in two variants, a text-only version, and a GUI version, the latter is called gvim.
Other vi clones are the already mentioned elvis and stevie. These clones were born in the CP/M and home computer area to bring the editor to these platforms, too. Of course, they were later ported to MS-DOS and Windows. These days, however, vim seems to be the prevailing vi-clone on free/open platforms and proprietary platforms as well.
You should choose the version you feel most comfortable with – if you have an editor you feel displeased with, it will affect your productivity.
Getting around vi[edit | edit source]
Starting the editor[edit | edit source]
If you are running a Unix system, you can start up vi by typing
at the command line. If you are running X, with a desktop like GNOME, KDE, CDE/Motif or OpenLook you may have a launcher button handy to start the editor - if you have such a setup, you can just click the icon.
If you are running Windows or DOS with elvis, you can start up the Windows editor by double-clicking "winelvis.exe", or in DOS, you can start the editor by typing in "elvis" at the command line.
You will be greeted with a screen similar to:
~ ~ ~ ~ ~ ~ "No File"
Quitting the editor[edit | edit source]
To quit for now, press the Escape key (the editor should beep), then enter the three characters :q! and press Return:
Just before you type the final <CR> the screen will look similar to
~ ~ ~ ~ ~ ~ :q!
:q! is the short form of :quit! which quits the editor, without saving.
You should be dropped back to your operating system (or, rather, the shell from where you started).
There are other ways to quit, e.g. pressing <Z><Z> (<Shift-z><Shift-z>) will save any unsaved work and quit the editor. Typing :wq will always save, even if there are no unsaved changes, and then quit the editor. :x will write if there are no unsaved changes, and it will quit. :wq and :x requires that you had previously provided a file name, so it will not work for the above simple example. Typing :q will quit if there have been no changes made; if changes have been made, vi will print a warning similar to "No write since last change".
Don't worry[edit | edit source]
Many first time vi users stop at this point, and never touch vi again. If you tried to enter some text after you started, you will most likely have been greeted with a series of beeps and rather erratic behavior. Don't worry. This is perfectly normal for vi, and the editor is not broken. You will soon see why this is normal vi behaviour.
Continue[edit | edit source]
Now that you know how to start the editor and quit it, let's move on to getting things done in vi: see Learning the vi Editor/Basic tasks