[Access to Wang masthead]

vi Basics

The editor everyone loves to hate

From "Migration",  Access to Wang, January 1995
  [ Prior Article ]     [ Return to the Catalog of articles ]     [ Next Article ]  

Next to a system's application software, no other software impacts technical users as much as the editor. Even users who have little contact with formal programming have need to edit scripts, prepare text for publication, and maintain their own profile data.

It's no secret that the vi editor is one of the reasons many first-time users do not like the Unix technical environment. Many of the complaints center on vi's use of a modal approach (the change in key functionality depending on the current mode of the editor), but this same design philosophy used in the Wang editor.

Whatever your opinion about vi as a development environment, many other Unix commands and utilities assume familiarity with its use, so learning vi is an important part of becoming fully functional in Unix. The following is a quick course in the basics of vi.

Getting in and out

The usual method of starting an editing session with vi is to run it from a command prompt, specifying the file to be edited as an argument; e.g. vi myfile.txt. If you wish to start at a particular area in the file the you can enter that at the command line, too (vi +150 myfile.txt). Multiple files can be opened with a single command, and wild cards can be used. For example, vi *txt will open all files ending with "txt".

Once started, vi enters Visual Mode, one of three modes. Visual Mode is similar to the Wang DISPLAY mode: you are able to scroll through the text and enter commands but no changes can be made. The other modes are Input Mode, similar in some respects to Modify mode in the Wang editor, and Command Mode, which is sometimes known as "Colon Mode" because of its use of the colon preceding most commands.

This initial editing screen is without much other information other than the text to be edited and a single line of command information at the bottom of the screen; there is no indication of valid command choices or other hints on usage. Unfilled text area at the bottom of the screen shows as tildes (~).

Like the Wang editor, the screen area in Visual Mode may be moved one page at a time or one line up and down; unlike Wang's editor, line scrolling begins when the cursor "hits" the bottom or top of the screen, displaying the next or prior lines as the cursor is moved. The appearance of the screen can vary according to the settings in a personal configuration file (.exrc, located in the user's home directory) or changed by use of the :set command; more on these options later.

When you wish to leave vi, you must switch to Command Mode and enter the appropriate commands. The commands you select depend on whether you've made any modifications to the text and whether you wish to keep any such modifications. Here are some alternatives:

Command Description
:q Quit editor (only works if no changes made)
:q! Quit editor and discard any changes
:w Write changes; don't quit editor
:wq Write changes and quit editor
ZZ Write changes and quit editor

(Note that the last command variation did not require a preceding colon.)

If you learn nothing else from this article, remember the :q! command to exit vi without saving!

Moving around the editor

You have several choices for screen movement. To move a page at a time, press CTRL+F (forward) or CTRL+B (backward). Moving the cursor keys to the top or bottom of the screen area will also cause the screen to scroll. vi uses four typing keys for cursor movement: h (left), l (right), j (up), and k (down). (Some versions of vi may also understand the terminal's arrow keys, but don't count on it!)

Movements between words are simple: press w to go to the next word, b to back up to the previous word. Pressing 0 (zero) will position the cursor at the start of the line, while $ (dollar sign) will go to the end of the line.

The G (go to) command is used to move to specific lines in the text. To go to the top of the file, enter the line number (1) followed by a G. The G command alone goes to the bottom of the file.

Adding and changing text

Text modification requires that you enter Input Mode by pressing a command key, perform the desired changes, and return to Visual Mode by pressing the ESCAPE key. If the options are set to do so, vi will indicate this change in the lower right, displaying INPUT MODE or REPLACE MODE as appropriate.

There are several ways to modify existing text, depending on your goals. To insert more text at the cursor, press i (insert). Like most editors (except the Wang editor), vi will "push" old text to the right as new text is entered and wrap lines as each exceeds a present line length. Press a (note capitalization!) to add more text, beginning at the current cursor position, or A to add text at the end of the current line.

There are also several methods of replacing text. Press r (note lower case!) to replace a single character or R to replace multiple characters. (In effect, the R command makes vi behave somewhat like the Wang editor, overstriking existing text.) The C command allows you to change all information in the line from the cursor position on.

The vi editor also allows easy change of single words. For example, pressing dw will delete the current word, while cw will change the current word. ("Word" in this case is any characters from the current cursor position to the next punctuation character, space, or tab.) When combined with the rapid movement between words, this makes editing most program text fast and safe. Since the ESCAPE key is always used to exit Input Mode commands, it's a good idea to get in the habit of hitting ESCAPE once in a while to confirm you are not in an add or change mode. Other good habits include writing the file to disk often (enter :w) and remembering to use the u command to undo the results of the last edit.

Cutting and pasting text

Cut and paste operations in vi involve use of the buffer, a temporary storage location for text, and marks placed at locations in the text. Any text copied or deleted is placed in the buffer and can be yanked back into the file if necessary. For example, entering dd will delete the current line and place it in the buffer. By positioning the cursor to another location and pressing p (paste after cursor) or P (paste before cursor), the deleted information is yanked back from the buffer and inserted at that point. The contents of this buffer remain until replaced by another cut or copy operation, so you can paste additional copies of the buffer as much as you want.

Copying also involves the buffer. To copy a block of text, perform the following steps:

  • Move to the beginning or end of the area to be copied and mark the point (enter m plus a letter to name the mark; e.g. mi to create a mark named i).

  • Move to the other end of the area to be copies and create a named buffer by entering a double quote and a letter for the name (e.g. "a).

  • Without moving the cursor, "yank" (copy) the material into the buffer by pressing y, the accent grave character (located with the tilde on most keyboards) plus the name of the mark; e.g. y`i. The text is now copied into buffer a.

  • Move the cursor to a new location, open the named buffer (double quotes plus the buffer name) and enter p to paste the contents of that buffer. Example: enter "ap to paste buffer a. Continue as often as you like.

Other useful delete commands include x (delete one character at the cursor), D (delete to end of line), and dw (delete word).

Advanced options

The vi editor has been the subject of many books, and many books have been written within vi. There are many more options that can be covered here. Some of the more interesting choices include:

  • Recovery mode: If you enter vi with the -r option, it will attempt to recover text saved in a system file prior to a system or workstation crash.

  • Setting options: An alternative to typing :set commands in vi is to create a configuration file with your preferences.

  • Long lines: vi will edit any file with lines up to 1024 characters.

  • Special characters: You can insert non-text control characters by pressing CTRL+V followed by the letter of the desired code. For example, you can enter a CTRL+G character - the ASCII value that sounds the workstation bell - by pressing CTRL+V followed by a G. This capability is particularly helpful when you are using sed or awk to globally replace control characters in a file.

  • Automatic indents: vi can be set up to indent new lines based on judgment about the prior line.

  • Bracket matching: Another option will highlight matching brackets, braces, and parentheses so you can easily see the start and end of the statement. This is helpful in C language and shell script programming.

(All of these options are covered in detail in vi references, including the on-line manual pages provided by the operating system. Look for reference to the :set command.)

The vi editor is well suited to editing specific types of text - particularly C code and script languages. It is also a staple of the Unix environment and a model for the behavior of other Unix tools. Understanding its peculiarities is an important step to becoming a Unix guru.


  [ Prior Article ]     [ Return to the Catalog of articles ]     [ Next Article ]  


Copyright © 1995 Dennis S. Barnes
Reprints of this article are permitted without notification if the source of the information is clearly identified