You should now have the basic skills to create, navigate, and edit a text file in vi. After you get familiar with the basic commands, you can create and edit files about as quickly as you can in a more conventional text editor. A few things are missing from your repertoire, though. This section teaches you how to cut, copy, and paste. You can do multiple iterations of a command at one time, repeat commands, search the document, and use undo and redo. This editor meets the major functionality found in other text editors and uses the fast-access keyboard command style.
In vi, any time a piece of text is deleted, it's automatically
stored in a buffer (like the clipboard in Windows). You
already know how to do a cut command by using
Similar commands are available to copy data without deleting
it -- this process is called yank in vi -- :
- Press the y key twice to copy a whole line.
- Press the y key and w key to copy an individual word.
- Press the y key and $ key to copy a line starting at the current cursor location.
Copying data isn't much use without knowing how to paste
it. So, before testing these commands, you should learn the
paste command, which is enacted with the p key. Like many other
commands in vi, a lowercase p key pastes data after the cursor
location, whereas an uppercase P key pastes data before the
To copy and paste, navigate to the first line of your text file and press the y key twice. Then, move the cursor down to line 2 and press the p key once. Doing so creates a copy of line 1 on line 3. See Figure 18.
Figure 18. To copy a line, use the yy command and paste using the p command.
Try doing a cut and paste by moving the cursor to the second line and press the d key twice. Then, press the p key to paste the line below line 2. See Figure 19.
Figure 19. To cut and paste a line, use the dd command and the p command
At this point, you might wonder how to do some of these commands on more than one piece of data at a time. For instance, you probably often copy and paste entire paragraphs, as opposed to single lines. vi lets you preface just about every command in the application with a number, which causes the command to operate multiple times at once. This is an extremely powerful, important part of what makes vi a great editor for power users. To try a simple example of cutting and pasting two lines at a time, navigate to line 1 of the file, press the 2 key, press the d key twice, and then press the p key. See Figure 20.
Figure 20. Cut and paste two lines at a time by using a number 2 preceding the dd command and the p command
The same concept can be used during the
paste command to
replicate a line multiple times. To do so, go back to line 1, copy
the line with the
yy command, and then press 10 before pressing the
p key. Now you have 10 more lines of comments about vi. Before
moving on, press the 5 key and then press the d key twice to remove some of the
extra lines. See Figure 21.
Figure 21. Paste multiple lines by preceding the p command with the number 10; then delete some of them with the number 5 preceding dd
Experiment with numbers in front of navigation commands as well. For instance, pressing the 30 key and then the l key moves the cursor 30 spaces to the right. Pressing the 7 key and then the G key moves the cursor to line 7 in the file. Pressing the 5 key and then the w key moves the cursor to the fifth word. When you're finished experimenting, you can move on to the next step.
Another useful command in vi is executed with the . (period) key.
The . key repeats the last command, which is an important
feature for getting work done quickly. For instance,
navigate the cursor to the word
cool on the first line, and then use the
cw command to
change the word to
fast. Don't forget to press the Esc key when you're
finished typing the word
fast. Move down to another instance of the word
cool, and then press the . key to change that word to
fast. You can also move to the word
line on the last two lines of your file and replace
those with the . key. See Figure 22.
Figure 22. Repeat commands with the period key, which lets you produce document edits quickly
Searching text in vi is also fast and efficient. To start
searching for a string, press the / key (slash key) followed by the string you want to search for and then press the Enter key. To combine some of your vi skills, press the / key, type
Enter, and then use the
cw command to change the word to know.
Don't forget to press the Esc key when you're finished.
If you want to do the same thing on line 2, press the n key to find the next instance of think, and then press the . key to change the word to know. The ? key does a search just like the / key, but it searches the document backward instead of forward. After you've replaced think with know, press the ? key followed by the word fast to search back to it. See Figures 23 and 24.
Figure 23. Search for strings by using the slash key followed by the string you're searching for
Figure 24. Search for strings backward by using the ? key followed by the string you're searching for
If you make any mistakes, vi has the modern convenience of undo and redo to make sure you can restore your document to the proper state. Undo is accomplished by pressing the u key in command mode, and redo is executed by pressing Ctrl-r in command mode. Try undo and redo, as follows (see Figure 2):
- Go to line 3 in your text file and remove a few lines.
- Press the 3 key and the G key to go to line 3.
- Press the 2 key and the
ddcommand to delete two lines.
- Oops! That was a mistake, and you want to get the two lines back. To do so, press the u key to undo the previous command.
- If you change your mind and want the two lines removed, press Ctrl-r to redo the command.
Figure 25. To undo a command, use the u key; to redo a command, press Ctrl-r