vi Editor: Tutorial

Chapter 1 --- Editing and Writing with vi

1.1 What is vi?

vi is the standard SunOS text editor ("vi" stands for "visual display editor"). Since vi is not window-based, it can be used on any kind of terminal.

You can enter and edit text with vi, but it is not a word-processor: you format vi text by inserting codes that are then interpreted by another program, a formatter.

In this chapter you'll learn many of the most useful vi commands. You'll find that it is a powerful text editor, but that it will take a little time in order to become proficient. More---and more technical---information can be found in "Using vi, the Visual Display Editor," in Editing Text Files.

Note that there is a read-only version of vi called view. When you open a file with view, you can use vi commands, but you cannot write (or save) your changes.

1.2 Creating a File

Starting vi

To start vi, at the system prompt type:

     vi filename
then press RETURN. If filename already exists, vi will open it; if this is a new file, vi will create it. For example, if you wanted to create a file called "malleable", you would type:
     venus% vi malleable

The vi screen will appear in a moment.

The cursor appears in the upper lefthand corner of the screen. Blank lines are indicated by a vertical series of tildes (~). The last line of the screen shows the name of the file and indicates that you're creating a new file.

Entering Text

One way to enter text in "malleable" is to type the letter a. This is a command meaning "append" -- it won't show on the screen. Now type a few short lines of text, ending every line with a Return. For the moment, you can correct your mistakes by backspacing and retyping a line before you press Return. Later you'll learn how to make corrections in more sophisticated ways.

When you've finished typing in malleable, press ESC (escape). The cursor will move back onto the last character entered.

Saving Your Work
Saving your work creates a record on disk of your file and any changes you've made to it.

To save the text you've entered in malleable, press ESC (to insure that vi doesn't think you're still adding text). Now type a colon(:) followed by a w (for write). Press Return.

After you type Return, vi displays the filename, the number of lines in the file, and the number of characters in the file.

Quitting vi

Now that you've saved(or written) the contents of malleable, you can safely quit vi.

To quit vi, type a colon(:) followed by a q. Press Return.

The system will display the command prompt to indicate that you have quit vi.

Printing a File
Unformatted files can be printed out with the lp command:
     lp filename

This command will send the file to your default printer.

Some Theory: Command, Last-Line, and Input Modes

vi has three modes---command, last-line, and input. You have already used features of each: When you started vi, it came up in command mode. Typing a put you in input mode. Typing ESC took you out of input mode and put you back in command mode. Typing the colon in commands like :w or :q put you in last-line mode (also called "colon mode").

If vi seems to respond unpredictably, you may have inadvertently entered the wrong mode. You can always be sure that you're in command mode by pressing ESC.

1.3 Moving Around in a File

In the previous section you learned how to create a file. Now that you have something to work with, you'll want to find out how to look at different parts of the file and how to move around.
Moving the Cursor
When you start vi, the cursor is in the upper lefthand corner of the vi screen. You can move the cursor with a number of keyboard commands. Some letters, the arrow keys, and the keys for Return, Back Space, and Space Bar can all be used to move the cursor when in command mode.

CAUTION: Most vi commands are case-sensitive: the "same" command typed in lower-case and upper-case could have radically different effects.

Arrow Keys

If your machine is equipped with arrow keys, try these now. You should be able to move the cursor freely about the screen using combinations of the up, down, right, and left arrow keys. Of course, you can move the cursor only across existing text or input spaces. (If you're using a remote terminal, however, the arrow keys may not work correctly, depending on your terminal emulator. Arrow keys substitutes are discussed below.)

Arrow Key Substitutes (work just like the arrow keys)

You can move a character or line at a time or, by holding down a key longer, move the cursor rapidly across the text.

One Word: w, b; W, B

Scrolling

press Ctrl key with: f, d, b, or u ---scrolls through text 1/2 screen at a time, backwards or forwards

1.4 Inserting Text

vi has many commands for inserting text; this section introduces you to the most useful. By using a combination of cursor keys and the letters a, A, i, A, o, and O, followed by Esc, you'll learn how to insert text anywhere in a file. Note that any of these commands puts vi in input mode.
Append Mode
a = insert text to the right of the cursor
A = add text to the end of the line
Insert Mode
i = insert text to the left of the cursor
I = insert text to the beginning or the line
Open Mode
o = opens a line below the cursor position
O = opens a line above the cursor position

1.5 Changing Text

Changing text involves substituting one bit of text for another.
Change Word - cw
Position cursor at the beginning of the word to be replaced and type the replacement text.

To change part of the word, position cursor to the right of the portion to be saved, type cw, the correction, and Esc

Change Line - cc
Position cursor anywhere on the line and type cc. The line will disappear, leaving a blank line for your new text.
Change Part of Line - C
Replaces the part of the line to the right of the cursor.
Substitute Character(s) - s
To substitute one or more characters for the character under the cursor, type s, followed by the new text.
Replace Character - r
To replace character under cursor with another character, type r, followed by the new character.
Transpose Characters - xp
Place cursor over the first letter to be moved and then type xp. The lst and 2nd letters will trade places.
Undo Previous Command - u
Typing u once reverses your previous action. Typing it a second time reverses your first undo.
Undo Changes to Line - U
Reverses all changes to a line.
Break Line - r
Breaks line without affecting text
Join Lines - J
J joins lines together.

1.6 Deleting Text

Delete Character - x
Deletes the character under the cursor.
Delete Word - dw
Deletes the word containing the cursor.
Delete Line - dd
Deletes line containing the cursor.
Delete Part of Line - D
Deletes everything to the right of the cursor.

1.7 Using Repeat Factors and Repeating Commands

Placing a number before the command tells how many times the function should be repeated. After performing a function, typing a (.) period repeats the previous command.

1.8 Copying and Moving Text--Yank, Delete, and Put

"copy and paste" = yank and put

"cut and paste" = delete and put

Copying
To copy a line requires two commands, using yank or put.
Moving
To move a line requires two commands: dd(delete) and p or P dd = position cursor anywhere on line to be deleted and type dd move cursor to line above the cursor by typing p or P

Note: by placing a number before the command, instructions will be issued to the effect of repeating the command as many times in a row as the # before the command.

1.9 Last-Line Commands

Turn line numbers on by typing the command:
     :set nu
Line numbers will appear in the left margin. Note that these numbers do not print out on hard copy of a file. Visible only on the screen.

Turn line numbers off by typing:

:set nonu
Copying Lines
The basic form of the last-line copy command is:
     :line#,line# co line#
The first two numbers(separated by a comma) specify the range of lines to be copies. The third number is the line before the insertion point. For example, to copy lines 15 through 20 of malleable and place the copy after line 30, you would type:
:15,20 co 30
When specifying line ranges, you can use the abbreviations . (period) to denote "from cursor" and $ (dollar sign) to denote "to end of file."

Thus, to copy the range "from cursor position through line 20" and insert this block after line 30, you would give the command :.,20 co 30

To copy the range "from line 15 through the end of the file" and insert this block after line 5, you would type:

     :15,$ co 5
Moving Lines
The basic form of the last-line move command is similar to the copy command discussed above:
     :line#,line# m line#
For example, to move lines 3 through 12 to the line following 21, you would type:
     :3,12 m 21
You can delete a range of lines using the command form:
     :line#,line# d
For example, to delete lines 19 through 31, you would type:
     :19,31 d
Note: to "undo" last-line commands, give the command :u and press Return OR just type u without first typing a colon.

1.10 Searching and Replacing

Finding a Character String
A character string is simply one or more characters in a row. It may include letters, numbers, punctuation, special characters, blank spaces, tabs, and carriage returns. A string may be a grammatical word or it may be part of a word.

To find a character string, type / followed by the string, and then press Return. vi positions the cursor at the next occurrence of this string.

Type n to go to the next occurrence of the string; type N to go to the previous occurrence.

You can type ? instead of / to search backward in a file. In this case, the directions of n and N are reversed. In any event, vi searches in either direction wrap around the end of a file, looking for the string wherever it may occur.

Searches normally are case-sensitive: a search for "china" will not find "China."

To look for a string in malleable, first type / and then type a common word, disinformation, for example. Press Return.

If vi finds the string, the cursor will stop at its first occurrence. If the string is not found, vi will display Pattern not found on the last line of the screen.

Refining the Search (for the advanced user)
To match the beginning of a line, start the search string with a caret(^). To find the next line beginning with "Disinformation" give the command /^Disinformation.

To match the end of a line, end the search string with a dollar sign ($). To find the next line ending with "disinformation", give the command /disinformation\.$ (Note the period is quoted with a backslash.)

To match the beginning of a word, type \< at the beginning of the string; to match the end of a word, type \> a the end of the string. Thus, to match a word, combine the end-of-word and beginning-of-word tags in the search pattern. To find the next occurrence of the word --as opposed to the string -- "disinformation", give the command /\.

To match any (unknown or variable) character, type a period (.) in the string. To find the next occurrence of "disinformation" or "misinformation", type:

     /.isinformation
Brackets may be used to search for alternative characters in a string. /[md] string will find strings beginning with either "m" or "d". On the other hand, [d-m] string will find strings beginning with any letter from "d" through "m".

To match zero or more occurrences of the last character, type an asterisk (*) in the string. You can effectively combine brackets and the asterisk to look for well-defined alternatives. To find all strings beginning with "a" through "z" and ending with "isinformation" and to find all occurrences of the string "isinformation", type:

     /[a-z]*.isinformation
Replacing a Character String
The procedure for replacing a text string is based on the search procedures discussed above. All the special matching characters for searches may be used in search-and-replace.

The basic command form:

     :g/search-string/s//replace-string/g
Thus, to replace every occurrence of the string "disinformation" with "newspeak", you would type:
     :g/disinformation/s//newspeak/g
You can modify this command to halt the search and make vi query whether you want to make the replacement in each instance. The command
     :g/disinformation/s//newspeak/gc
(adding the c for "consult") will make the substitution. Respond with y for yes or n for no.

Note: You can cancel the consulted search and replace by pressing Ctrl-C.

Finding a Specific Line
vi provides ways a accessing parts of a file by either opening the file to a specific line or by finding a line in an already opened file.
Going to a Line with G
You can go to the last line of an open file by typing G. Return to the first line of the file by typing 1G.

You can go to any other line by typing its number followed by G.

For example, say you've quit the malleable file while editing line 51. You can access that line by opening the file and typing 51G.

Opening a File at a Specific Line
You can also start vi and open a file at a specific line. At a command prompt, type:
     vi +line# filename
To start and open malleable at line 51, type:
     venus% vi +51 malleable
(By not supplying a line number after the plus sign, you can make vi open malleable at the last line of the file.)
Opening a File at a Pattern
You can open a file to the first line containing a specific string by typing at a command prompt:
     vi +/pattern filename
For example, to open a malleable at the first line containing the string "2+2=5", you would type:
     venus% vi +/2+2=5 malleable
Enclose multiple-word strings in double-quotation marks, e.g.:
     venus% vi +/"Andre Breton" malleable

1.11 Inserting a File Into a File

vi makes it convenient to "read"(insert) a file into the file you're editing. The general form of the command is
     :line# r filename
If you do not specify a line number, vi puts the file at the current cursor position.

If you were working on the file malleable and you wanted to read in another file called orwell, and you wanted it placed at line 84, you would type:

     :84 r orwell
Or you could position the cursor on line 84 and type:
     :r orwell

1.12 Ending a Session

Using the Buffer
When writing or editing a file in vi, your changes are not made directly to the file. Instead, they are applied to a copy of the file that vi creates in a temporary memory space called the buffer. The permanent disk copy of the file is modified only when you write(save) the contents of the buffer.
Saving Changes and Quitting

1.13 Customizing vi

To view a list of variables that affect behavior and appearances by typing:
     :set all
For additional information view the man page for vi. You can also view the man page in a Unix session by typing:
     man vi