vi Editor: Tutorial
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.
To start vi, at the system prompt type:
vi filenamethen 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.
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.
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.
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.
lp filename
This command will send the file to your default printer.
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.
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
To change part of the word, position cursor to the right of the portion to be saved, type cw, the correction, and Esc
"cut and paste" = delete and put
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.
:set nuLine 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
: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 30When 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
:line#,line# m line#For example, to move lines 3 through 12 to the line following 21, you would type:
:3,12 m 21You can delete a range of lines using the command form:
:line#,line# dFor example, to delete lines 19 through 31, you would type:
:19,31 dNote: to "undo" last-line commands, give the command :u and press Return OR just type u without first typing a colon.
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.
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:
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:
The basic command form:
Note: You can cancel the consulted search and replace by
pressing Ctrl-C.
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.
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:
/.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".
/[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.
: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.
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.
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
:line# r filename
If you do not specify a line number, vi puts the file at the
current cursor position.
: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
: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