Productivity features
So far, we've covered how to move, save and quit, perform simple edits and deletions, and use insert mode. With everything listed on the cheat sheet so far, you should be able to use vi to perform almost any task.
However, vi also has many more powerful commands. In this section, you'll learn how to cut, copy and paste, search and replace, and use autoindent features. These commands will help make vi more fun and productive.
The best way to cut and paste is to use visual mode, a special mode that has been added to modern versions of vi, like vim and elvis. You can think of visual mode as a "highlight text" mode. Once the text is highlighted, it can be copied or deleted, and then pasted.
You enter visual mode by hitting v. You will see the word VISUAL at the lower left of your editor as shown in Figure 3. Then, by moving the cursor using movement commands (typically the arrow keys), you'll be able to highlight a region of text. If your Window manager doesn't take over the mouse keys for you, you can also highlight by simply dragging the left mouse button over a particular region.
Figure 3. Using vi's visual mode
Once highlighted, we are ready to cut or copy the text. If you're copying the text, hit y (which stands for "yank"). If you're cutting the text, hit d. You'll be placed back in command mode. Now, move to the position where you'd like to insert the cut or copied text, and hit P to insert after the cursor, or p to insert before the cursor. Voila, the cut/copy and paste is complete! Test out several copy/cut and paste operations before continuing.
To replace patterns of text, we use ex mode. If you'd like to replace the first pattern that appears on the current line, type :s/regexp/replacement/ and hit Enter, where regexp is the pattern you'd like to match and replacement is the replacement string. To replace all matches on the current line, type :s/regexp/replacement/g and hit Enter. To replace every occurrence of this pattern in your file (normally what you want), type :%s/regexp/replacement/g. If you'd like to do a global replace, but have vi prompt you for each change, type :%s/regexp/replacement/gc (stands for "confirm") and hit Enter.
vi supports autoindentation, for when you are editing source code. Most modern versions of vi (like vim) will auto-enable autoindent mode when you are editing a source file (like a .c file, for example). When autoindent is enabled, you can use ^d (control-d) to move one indent level to the left, and ^t (control-t) to move one indent level to the right. If autoindent wasn't enabled automatically, you can manually enable it by typing in the ex command :set autoindent. You can also tell vi to set the tab size to your favorite setting by using the :set tabstop command; :set tabstop=4 is quite popular.
Well, we've reached the end of the vi tutorial! After adding all the advanced editing commands to your cheat sheet, it should look like this:
Keep your cheat sheet handy, and begin using vi to edit files and compose emails. Refer to the cheat sheet when needed; you'll find that within the week, you'll have nearly all the commands memorized and your vi productivity will shoot through the roof!

