Vi is a very capable editor. In this lab, we learn some new features for it.
To start, please copy the file \fBtovi\fR to your account from my account: On toolman, use:
cp ~leff/tovi .
On uxb, use:
cp ~mflll/tovi .
Of course, remember to type the indicated periods.
Now edit the file by typing:
vi tovi
There are step-by-step insructions in tovi. Please follow them:
As we learned in the first lab, the commands to move the cursor are h, j, k and l
However, vi provides many conveninet items to move around more quickly:
The command w moves forward
one word and b moves backward one
word.
You can use f followed
by a character
and F followed by a character to go to the
previous occurence on the line. Thus, fx
will move the cursor to the next location of x on
the current line and Fx will
move to the previous location.
You can go to a particular line by typing the colon followed by the line
number. This is useful when you have a syntax error message that
gives you a line.
For example :50 moves one to line 50 of the file.
The search is simply the forward slash followed by what to search
for--its case sensitive. One can use ?
to search backwards.
Lastly, ^ and | move to the beginning of the line--see if you can later figure out the
subtle difference between them. And $ moves
us to the end of a line.
delete the next word
delete to the end of the line
delete to the beginning of the line
delete to the leftmost position (That is the vertical bar, often just above the Enter key on your keyboard).
You can change text in one command. For example, to change a word, press c and w. A $ will appear temporarily to show you what is changing. Then, enter the new word to replace it and then press ESC to get out of insert mode.
Similar commands exist that analagous to the delete commands. The change commands are:
change the next word
change to the end of the line
change to the beginning of the line
change to the left most position
A very useful option is the period. When you press the period, the system will repeat the last command that changed text, such as an insert, a cw or a dd.
We can delete several lines by pressing the number of lines to be deleted and then dd . When you delete lines, they are put in a text-register. Each text register is known by one of the ASCII characters. By default, they are put in the text register, 1.
However, you can specify the text register you want by proceeding the delete command with register-name. Thus, the sequence: "q5dd deletes five lines and puts them in the text register q.
You can put the lines somewhere by typing a double-quote, the register-name and a p. This adds the contents of the register-name after the given line. If you use a capital P, the register-name will be copied before the current line.
If you want to copy the text, the yank command will do this. The sequence is "register-name number-linesYY. You then put the text yanked using "register-namep as before.
Keep in mind that you can put different sections of text in several different registers and then put them where you want to with "register-namep
If you are moving or deleting large blocks of text, it is annoying to have to count the lines.
The system also supports marks. Marks contain a line number. They are also named with the ascii characters. They are refered to with single quotes.
To set a mark simply press the m and the mark-name.
To move to a mark, simply press the single quote and the mark-name.
To delete from the current line to the mark, enter "register-named'mark-name. Note that one can delete a block of text into a register-name by marking the bottom line and then moving to the top line and entering the command. As an alternative, one can also mark the top line and then move to the bottom. (Of course, you can omit the " and register-name and have the text be deleted into text-register 1.)
You can also use marks with the yank command as well. To yank from the current line to the mark into a text register, use "register-namey'mark-name. Then use "register-namep to place the yanked text somewhere.
vi supports ed commands. You get to them by pressing the colon key. Then type the ed command. As you will recall from the ed lab, you can specify a range for an ed command by the form
address1,address2 command
Thus, the command
5,10s/cat/dog/
will change all occurrences of cattodog in lines 5 to 10.
You can use the notation 'mark-name as one of the addresses.
You can use the notation address1,address2wfile-name to write a segment of your file out to somewhere else. You can then read that file back in in another vi session. This is how you move text from one place to another. If the filename already exists, vi will complain. Use w! instead of w to force the write if you wish.
One editor command supported by vi is the exclamation mark. You can replace a set of lines with result of running the lines through any UNIX command.
Thus, the sequence
address1,address2!unix-command
will put the lines from address1 to address2 into a temporary buffer. Then, it will be fed as standard input to the unix-command. The output will be stored in a temporary buffer. Lastly, the lines from address1 to address2 will be removed from your file and replaced with the second temporary buffer.
You can read a file into the current text while in vi by typing
:r name
It will insert the lines of text of that file after the current line. Thus, the sequence to move text from one file to another is:
Using the address1,address2:w command, write out a sequence of lines to a temporary file.
Leave the current vi session.
Start up a vi session on the target file.
Move to where one wants to insert the lines.
Type :r and then the temporary file name used in step one.