Visual Mode

The third mode of the vi editor, visual mode is actually three modes in one: line mode, character mode, and block mode. To enter line mode from command mode, hit V; to enter character mode hit v, and to enter block mode, hit Control-v. You can exit any of these by hitting the ESC key; this places you back in command mode.

Visual mode has one purpose: it allows you to select text using keyboard commands; you may then perform various operations on these selections. First, let us see the selection mechanism at work.

Go into a file and position your cursor in the middle of a line. Hit v to enter visual character mode. Now use the arrow keys; notice how the selected text changes in response to arrow key movement. Try entering gg and G and see what happens. Hit ESC to finish. Now enter visual mode and use the / search facility to search up something on the page. What happens? Search backward and try that too.

Now enter visual line mode by hitting V; now try the keystrokes we just indicated and see how the selection behaves. This mode only selects whole lines.

Finally if you enter Control-V and you enter visual block mode, you can select a rectangular block of text from the screen by using the keyboard.

Now let’s see what you can do with these selections. First let us look at character and line mode, as block mode behaves a little differently. You can cut the selected text to Mabel by hitting d. You can yank it into Mabel by hitting y. Upon typing either command, you will be put back into command mode. Once any text is placed into Mabel, you can paste it with p as you would any other text yanked there. If you hit c, the selection will be deleted and you will be in insert mode so you can change the text. The stricken text is cut to Mabel.

In block mode, things are a little different. If you hit d, the selected block will be deleted, and the lines containing it shortened. The stricken text is cut to Mabel. If you hit y, the block will be yanked just as in any other visual mode, and its line structure will be preserved. If you hit c, and enter text, the same change will be made on all line selected provided you do not hit the ENTER key. If you do, the change will only be carried out on the first line. You can insert text rather than change by hitting I, entering your text, and then hitting ESC. If the text you enter has no newline in it, the same text will be added to each line; if it has a newline, only the first line is changed.

If you hit r then any character in any visual mode, all selected characters are changed to that character.

Here is a very common use for character or line visual mode. Suppose you are editing a document and the lines end in very jagged fashion. This sort of thing will commonly happen when maintaining a web or if you are editing a LaTeXi document, where the page that is subjected to repeated edits. Use visual mode to select the affected paragraphs and hit gq (think Gentleman’s Quarterly) and your paragraphs will be tidied up.

You can also do search-and-replace using visual mode to select the text to be acted upon. Simply select the text in visual mode. Then hit

: s/outText/inText/g

to perform the substitution in the selected text. For example if you select text in visual mode and change every w to a v, you will see this.

:'<,'>s/w/v/g

The <,'> is a quirky way of indicating you are doing a visual-mode search-replace operation.

Replace Mode

In vi if you hit r then a character, the character under the cursor is replaced with the character you it. If you hit R, you are in replace mode, and any test you type “overruns” existing text. Experiment with this in a file you don’t care about.

Replace mode is fabulous for making ASCII art such as this.

< Galactophagy >
 -------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

You should play around with this. Do a Google search to learn about ASCII art.