VI tutorial - Lesson 10 - Miscellaneous Information

The following section is a collection of information about the VI editor that you should know about for successful editing. Read this section, apply what is useful now, and tuck the rest away to return to later after you have used the editor for a while.

10.1 Creating Line Numbers

The developers of the VI editor designed VI to be used without line numbers, feeling that the average user would locate and manipulate text by content rather than by line numbers. Additionally, because line numbers are constantly changing due to insertions and deletions, adjusting what you "think" is in a particular line can be dangerous.

The VI editor allows you to view line numbers in your file four ways: determining your current line number, a quick glance at line numbers for the complete file, insertion of line numbers for the current editing session only, and inclusion of line numbers for every session.

To determine the line number for the line the cursor is on, type "control g" as discussed in the Positioning Text on the Screen section.

To have a quick one time glance at line numbers, while you are in the file, type:



This will cause line numbers to be assigned to all lines; unfortunately, this also causes the complete file to scroll past quickly stopping on the last screenful of text. Now if you are working with a short 20-line file, this is great; however, if your file contains 200 lines, you will see the first 180 lines whiz past. To stop the scrolling action, you must press the "control s" to "stop" screen movement and later "control q" to "quit" the frozen screen. Some people get very good at timing the "control s" in order to get the exact point of the file to stop on the screen, most do not.

Many users find it more convenient to have numbers added to a file for the current editing session; knowing that the next time the editor is invoked, the numbers will not appear. To have line numbers inserted for the current session, type:


     :set number

Immediately you will see the line numbers appear in your file and they will remain until you exit the editor or type:


     :set nonu

The long-term line numbering option available with VI is to place a line numbering command in the .exrc file located in your HOME directory. The .exrc file is your personal control file to instruct all faces of the editor (VI, EX, and EDIT) on how you want it to perform when invoked. Not everyone creates a .exrc file. For those without this file, the automatic defaults of the editor are imposed, such as no line numbers. If you place the command "set number" in your .exrc file, the next time you invoke VI the editor will check this file for instructions and will present the file to you with line numbers. Later, if you decide you prefer to forgo line numbers, open your .exrc file and delete the "set number" line.

Type the following to get automatic line numbering each and every time you use the VI editor on all files in your .exrc:


     %  cd
     %  echo  'set number'  >>  .exrc

Alternately, you can set the EXINIT to set nu.

You can set this in your .login or your shell start-up files.

  1. Open your editor to edit your .login or your shell start-up file. Add the following lines:

    For C-Shell:

    	setenv EXINIT "set nu"
    For Bourne or Korn Shell:
    	EXINIT="set nu"; export EXINIT     
    For Korn Shell Only (alternate method):
    	typeset -x EXINIT="set nu"
  2. Either log-out and log back in or "source" your shell start-up file.


10.2 Lines and Sentences in VI

To be successful in your editing, it is necessary to understand what the editor considers a line and a sentence. Just for clarity, a line and a sentence are different animals to the editor. To the editor, a line begins on the left of a screen and terminates at a carriage return . The carriage return is the invisible character placed in your file every time you press the "RETURN" key. A sentence to the editor is a string of characters of unspecified length (a few characters to many lines) terminating with the punctuation marks " . ", " ? ", " ! " followed by either a carriage return or two blank spaces.

Technical typists, secretaries, and students who produce lots of reports and papers find that editing is much easier to complete if as you are keying in text you make lines very short. The breaking up of sentences into many lines is helpful. Placing a carriage return after phrases and punctuation will make editing words and lines less of a problem.

Some people like wraparound typing (straight typing without inserting carriage returns), this is not recommended. The next example demonstrates the use of the delete operator with the end-of-line scope "d$" on text where the "RETURN" key follows phrases and punctuation versus using the same operator-scope command on wraparound typing. The results are drastically different.

carriage return terminates each line

May 6, 1969. Indiana Assembly<CR>
accepted $150,000 from John<CR>
Purdue, $50,000 from Tippecanoe<CR>
County, and 100 acres from local<CR>
residents to establish Purdue.<CR>

After d$ Command

May 6, 1969. Indiana Assembly<CR>
accepted $150,000 from John<CR>
Purdue, $50,000 from Tippecanoe<CR>
County, and 100 acres from local<CR>
residents to establish Purdue.<CR>

wrap-around used for keying text (no carriage returns)

May 6, 1969. Indiana Assembly
accepted $150,000 from John Pu
rdue, $50,000 from Tippecanoe
County, and 100 acres from loc
al residents to establish Purd

After d$ Command

May 6, 1969. Indiana Assembly
accepted $150,000 from John Pu
rdue, $5


When you print out your file using a text processor, the computer will connect lines and phrases plus insert spacing between sentences to make your material look presentable. For example:

as the text appears in the file

Dear Mom,<CR>
I have a great dorm room.<CR>
It is in the basement<CR>
of Mill Dewey Hall<CR>
at Swampfog College<CR>
Send money.<CR>

as the text appears after printing

Dear Mom,

I have a great dorm room.
It is in the basement of
Mill Dewey Hall at Swampfog
College. Send money.



10.3 Joining Lines

As you are editing files, you will find it is desirable to combine or join lines. This is easily done using the "J" (join) command. An illustration of joining lines is given below. The cursor is located on the top line when the "J" command is issued. VI will move the lower line and butt it to the end of the upper line. The editor takes care of necessary spacing for you.

Before Join Command

was the first
university to use computers
to schedule classes in 1957.

After Join Command

Purdue was the first
university to use computers
to schedule classes in 1957.


10.4 Redrawing the Screen

The VI editor requires cpu (central processing unit) time to function. Each command and action takes a minute amount of time. This interaction is not a problem if only one or two people are accessing the system. However, the Engineering Computer Network has thousands of users and at any given moment many hundreds of users may be editing, running programs, and other system jobs, all competing for cpu time.

The VI designers foresaw that the editor could be a "time hog" and decided that one way to reduce some of the editing interaction with the cpu was to minimize redrawing the screen. This is why when you are in the text input mode you do not see the screen being updated until the action is completed and you return to command mode.

With time conservation as the impetus, the screen is also not redrawn each time a line is deleted. Rather, a removed line is replaced with the "@" symbol to symbolize an empty line, much as the tilde (~) is used. Further editing of the text that remains on the screen is still possible. These "@" symbols remain viewable as long as editing continues on this screen even though they are never inserted into the text.

Sometimes these "@" symbols are distracting and annoying. If a user would prefer not to see the "@" symbols, the screen may be redrawn and the "@" symbols eliminated by issuing the command "control r" or "control l". Experiment to see which command works on the terminal you are using.

10.5 Accessing the EX Editor

The VI editor has a powerful and useful companion editor, EX. In reality VI and EX are different faces of the same editor; both were developed from and use the same program base. VI is a screen oriented editor, while EX is a line oriented editor. Since both of these editors are built upon the same base, when one editor face is installed on a computer by default the other editor face is also there. This offers great advantages to the user because each face possesses individual strengths. Easy to use commands allow you to transfer from one face to another in order to increase your editing power by permitting you to use the desirable features of both editors and thereby better meet your editing requirements. The "global substitute" and "text marking" are two favorite EX commands.

All VI users, frequently without knowing it, make use of the dual face capability of this editor. For example, ":w", ":wq", or the ":quit!" are in reality EX commands. Also, the "read" command discussed in the Text Insertion section is an EX command! During an editing session, anytime you wish to invoke a single EX command, you must first make sure you are in command mode then type ":" followed by the command. The cursor will hop to the bottom of the screen and the command is echoed. When you press "RETURN", the EX command is executed and you are brought back to the command mode of VI.

Sometimes it is useful to issue a series of EX commands. This is done by typing "Q" while in command mode and you will enter and remain in the EX face until you type "vi" at the EX prompt (:), such as ":vi". Immediately you are returned to VI and can continue the editing session using VI commands.

It is suggested that users do not try to learn both VI and EX at the same time. Learn VI well, then proceed to learn EX. Attempting to learn both faces of the editor at the same time leads to frustration and confusion, as invariably the user mixes up which command to use when.

10.6 Repeating a Command

To make life a bit easier, VI allows text alteration commands to be repeated by using the "." (repeat) command. A handy way to illustrate the repeat command is with the "cw" command replacing a single word with two new words throughout a paragraph.

In this example, the first occurrence of "PU" is located with the search command PU". Then with the cursor on the "P" of "PU", the "cw" command is issued followed with "Purdue University" and the "ESC". The "n" key is pressed to find the next occurrence of "PU". The cursor relocates on the "P" of the next "PU" and all that is required to change it to "Purdue University" is to type "."

Original Text

PU has 217,000 living
alumni. 75% of PU's
students are from Indiana,
23% from other states, and 2%
from foreign nations.

Altered Text

Purdue University has 217,000 living
alumni. 75% of Purdue University's
students are from Indiana,
23% from other states, and 2%
from foreign nations.

10.7 Temporarily Interrupting VI

The ability to access the UNIX shell while keeping the current file open with VI is a true convenience. To do this, you should first "write" the current buffer contents to the disk file with ":w". The current editing session may be interrupted by then typing ":!" followed by the desired command. When the requested command action is completed, the message:


     [Hit return to continue]


will appear at the bottom of the screen. After pressing "RETURN", you will be returned to the VI editor to the same location you were at when you temporarily interrupted the editing session.

A common way to use this interrupt ability during a current editing session is to read a recently received mail message, ":! mail". As you work more with UNIX, you will begin to see many ways to make this command work for you.

10.8 Editing Multiple Files Using VI

The VI editor provides an advanced feature which allows a user to invoke the editor and then edit multiple files by use of the ":e" (edit) command. This ability to access multiple files without leaving the editor permits a user to look up information in another file without exiting the editor. Additionally, because files are opened within the same editor invocation they can share the same named buffers, thereby making the transfer of text possible between the files. The example on page 40 demonstrates how two lines can be "yanked" from the file oranges, placed into a named buffer "k", and then "put" into the file apples.

When VI is invoked, a work area called a buffer is created for editing purposes. It is into this work space that a copy of a specified disk file is placed. The editor permits only one file copy in this buffer space at a time. Thus after making changes to a file (delete, add, or change), you must inform the editor what you wish done to the current buffer contents before you will be permitted to bring another file into this space. You do this by use of the ":w" (write current buffer contents to opened file), ":e!\ newfile" (toss current buffer contents, no update to opened file, and place a copy of newly called file in buffer), or ":quit!" (exit editor and toss buffer and buffer contents). The editor is smart enough to know that if all you have done is copy or read from a file, it can dispose of the unneeded buffer copy without further instructions from you when a new file is called.

When you have two files open, VI permits toggling between files by use of ":e\ #". This works because whenever VI sees the character "#" used in a command where a filename is expected, it substitutes the "#" with the name of the previous file. For example if you had been in apples then opened oranges, the command ":e #" would return you to where you were in the apples file. Repeat ":e #" and you would be back in oranges.

Another method to "cut" and "paste" between files is to use the mark command (m) in conjunction with named buffers ("). Move the cursor to the first line you wish to copy from oranges and type "ma" (m to mark the line and a to specify which mark). Next move the cursor to the last line you wish to copy and type ""zy'a". This command tells VI to open up "z (buffer named z) and y (yank) a copy of all the lines from the line marked 'a to the current line and place them in the aforementioned buffer. Return to apples with ":e #" and proceed to paste the contents of "z by moving to the desired location and p (put) the buffer contents by typing ""zp".


  1. Open the original file with "vi apples".


  2. Correct typo.


  3. Write the contents of the buffer to the file apples using the ":w" command.


  4. Issue the command ":e oranges" to open the second file.


  5. Yank a copy of two lines from the file oranges and put them into the named buffer "k" using the ""k2yy" command.


  6. Recall the original file, apples, to the buffer using the ":e apples" or ":e #" command.


  7. Contents of buffer discarded when apples is recalled to the buffer. The disk copy of oranges remains unaltered.


  8. Position the cursor and put the lines from the buffer "k" into apples using the ""kp" command.


  9. Write and quit the editor with the ":wq" command.


  10. Buffer is discarded upon leaving the editor.

10.9 Printing from VI

Print the current file (printer name is required if no default printer is set): :!lp [-d printername] % Print others files :!lp [-d printername] filename


previous | MENU | next


Last modified: 2009/09/01 09:46:26.251000 GMT-4 by christopher.n.deckard.1
Created: 2007/11/06 14:24:30.993000 US/Eastern by brian.r.brinegar.1.


Search the Knowledge Base


Quick search results

    Admin Options: Edit this Document