======================================================================= U N I X T U T O R I A L by Glenn Chappell ======================================================================= Unix Tutorial (September 1994 version) by Glenn Chappell (Feel free to distribute this document however you want.) Chapter VI. vi ----- Part 2 - Cutting, Pasting and Searching ----- Covered: vi commands: X dw D p P yw yy / ? n N . vi has lots of deleting commands. You already know "x", which deletes a character, and "dd" which deletes a line, but there are many others. Three you may find useful are "X", which deletes the character to the left of the cursor (like backspace-x), "dw", which deletes a word, and "D", which deletes to the end of the line. Try these to see how they work. Just as with "x" and "dd", all of these (except "D") can be preceded by numbers to increase their effect. So, e.g., "10dw" deletes ten words, etc. Whenever you delete anything in vi, the characters you deleted are stored (in a place often called the "cut buffer" -- Macintosh & Apple IIGS users know it as the "clipboard") for later use. Now, you could bring back the deleted characters with the "u" command, but it is useful to be able to bring them back somewhere else. For this, vi has a pair of "paste" commands: "p" and "P". "p" inserts the contents of the cut buffer after the cursor, while "P" inserts before the cursor. Thus, you can delete something, move the cursor somewhere else, and re-insert the text you deleted. Try this out. Just as with the deleting commands, "p" and "P" can be preceded by numbers to increase their effect. Unfortunately, this seems to happen in a rather inconsistent way. For example, "x10p" deletes a character, then pastes ten copies of it, while "dd10p" only pastes a single copy of the deleted line. As near as I can tell, multiple pasting works when the cut buffer contains only characters and/or words, while it fails when it contains whole lines. This is probably a bug. Pasting can sometimes happen in slightly unexpected ways. For example, "xp" deletes a character then inserts it in the next position; the effect is that of transposing two characters. Similarly, "ddp" transposes two lines. However, "dwp" does not transpose two words. You can move the cursor around all you want without affecting the cut buffer; thus, you can look around all you want for a place to put the text you have deleted without fear that the cut buffer is going to go away. Pasting also does not affect the buffer contents. Thus, you can paste the same thing in several different places. You can even save the file without affecting the buffer. However, going into any of the typing modes (e.g., insert or append) will destroy the cut buffer contents. Sometimes you want to put text in the cut buffer without deleting it. In vi this is called "yanking". Just as deleting commands usually begin with "d", yanking commands tend to begin with "y". Two useful ones are "yw", which yanks a word, and "yy", which yanks a line. Yanking commands can also be preceded by numbers. For example, "5yy" yanks five lines. My next topic is searching. Like all good editors, vi will look through the document being edited for occurrences of any string you specify. This is accomplished using the "/" and "?" commands. In vi command mode, type "/". Just like ":", this gives you a prompt at the bottom of the screen. You can now type whatever you want to search for at this prompt. For example, if you type "abc" and press , vi will move the cursor to the next occurrence of "abc" (note: not the *first* occurrence in the file, but the *next* one after the current cursor position). If "abc" could not be found, vi will tell you so. When searching reaches the end of the file, it will start back at the beginning, i.e., it "wraps". You can also search backwards, using "?", which otherwise works the same as "/". Note: when you use the "/" and "?" commands, vi does not, strictly speaking, search for exactly the characters you type. Rather, it interprets what is typed at the "/" or "?" prompt as a "regular expression". If you know how to use regular expressions in Unix, you can use them here. If not, you can still use vi's search commands without much trouble, but if you try to search for strings containing characters like "*" or "." or "$", and a few others, vi may not find exactly what you expect it to. Using these characters won't wreck anything though. I'll say a little bit more about regular expressions in chapter VII. Another note: using "/" and "?" for searching is another Unix tradition. Many programs use them for this. If you want to search for the same thing several times, you can just type "/", and press , which repeats your last search, but there is an even shorter way, the "n" command. "n" repeats your last search. Its companion command, "N", repeats the last search in the opposite direction. The final command I want to mention here is ".", which repeats the last change you made, i.e., the last typing or the last deletion. So, if you type, say, "ixxx" and press , which inserts "xxx" before the current cursor position, you could then move the cursor somewhere else and type "." to insert "xxx" again. I mention "." here because I have found it most useful in conjunction with searching. Typically, when you search, you will want to do something at each occurrence of the string you search for. For example, you might want to turn each occurrence of "yyy" to "xxxyyy". The long way is to type "/xxx" and press return, then type "iyyy" and press , then do the whole thing all over again. A slightly shorter way uses "n" instead of re-typing the search command each time. An even shorter way uses ".", which will repeat the insertion. Thus, "n" redoes the search, and "." redoes the insert. Repeatedly typing "n." can do quite a bit of changing very quickly. ------------------------------------------------------------------------