This chapter continues the discussion of the vi text editing program. The objectives important to this chapter are:
vi is capable of opening a file when you start it. You can also just start it by entering "vi". If you do, you can save a file with the write command, and you can name it then as well. Try entering some text in vi and giving it the command :w filename. If the file already exists, vi will tell you. It will also tell you to use :w! filename to force the overwrite.
For those who are afraid of making changes, you can start vi in a read only mode, by fooling it into thinking that the file you want to see is read only. Try vi -R filename to see it. It looks the same, and acts the same until you try to save the file.
You can give vi a series of filenames as object arguments, and it will open the first file in your list. When you want to go on to the next file enter :n and the first file is replaced by the next. Better save the first one before you do that. To call up a file that you didn't think of when starting vi, use :e filename and it will switch to the named file.
vi has several cut and paste functions. To delete the current line, just press dd. It isn't really deleted, only cut to a temporary buffer. To paste it back, position your cursor, and press P to paste above the current line or p to paste below the current line.
If you don't want to cut, the yy command copies the current line to a buffer. (It is called yanking the line.) Pasting from that buffer is done the same way as above.
In both of the commands above, we used the command key twice to indicate to vi to carry out the cut or yank command on the current line. The area to carry out a command on is called the scope of the command. Instead of pressing the command key a second time, you can use the dollar sign to mean "from the cursor position to the end of the line" or the zero key to mean "from the start of the line to the character before the cursor position". This was written in the days before editors that could highlight text. Does anyone want to send Bill Gates a thank you note?
A useful feature is the undo command, issued by pressing the letter U in upper or lower case.
Deleting text can use scope commands, too. d$ will delete from the cursor to the end of the current line. dw will delete the current word and the space after it. Putting a number in front of the d will delete that many words.
The buffers that vi uses can be accessed by the user. Every time you do a deletion, the text deleted goes into a Buffer 1. The next time you do a deletion, the text also goes into Buffer 1 and what was in Buffer 1 goes into Buffer 2, and so on up through Buffer 9. You can paste from any of these buffers with a quote, the number of the buffer and the upper or lower case p. If that isn't enough buffer space for you, there are also twenty-six buffers, each named with a lower-case letter of the alphabet.
There are several environment variables you can set for vi that will make some tasks easier. The syntax varies a bit from one to another. See table 6-4 in your text for a list of these features. If you like any of them enough to make the change a regular feature of vi sessions, you can store the command to set the option you like in the .exrc file. You can edit this file with vi to see the current default settings that kick in when you start vi. Change the settings to what you like, and save the file. It is read by the system every time vi runs. The file is a personal file, meant to be stored in your home directory. Don't change the one in other people's directories unless they want you to do so, or you like to have people plotting to get even with you.
Finally, you can run actual shell commands in the middle of a vi session, by beginning the command with a colon, following it with an exclamation point, a space, and the command to be carried out. The result of the command can be copied into your vi document by inserting the letter "r" and a space before the exclamation point.