views:

286

answers:

6

I'm relatively new to the world of Vim. I've been learning my way around it but have yet to find a practical purpose to enter visual mode.

What are some scenarios when visual mode is especially useful?

Are there actions that can only be performed from within visual mode?

+4  A: 

I use visual mode when I want to highlight a section of text. I start by typing v in standard mode, which then enables the visual mode. Then I use the arrow keys to move the cursor. This causes the text between my starting point and the current cursor location to be highlighted. Once you select a section of text like this, entering a command (e.g. search/replace) in command mode (by typing :) will only affect the selected area.

Another useful visual command is shift+v (visual line). This does the same as above, but it selects entire lines at a time instead of individual characters.

AJ
Another (sometimes) handy command is ctrl-shift-v to use visual block mode, where you select text in square shape instead of just by line. I find it handy for log files.
C4H5As
@i, that is very cool...just tried it! thx.
AJ
FWIW, to go into column select ("square shape") select mode, you only need ctrl-v (Vim sadly can't tell the difference between ctrl-foo and ctrl-shift-foo).
overthink
+3  A: 
  1. When you want to comment a block of text.
    In command mode :
    Shift + v ,ctrl +v, j or k, I , #(comment character) and then Esc

    Vim inserts the comment character to the start of the block..

  2. is when I am using Gvim, I find it much easier to copy data to the clipboard through visual mode.
    In command mode :
    Shift + v , j or k , " , + ,y

    Here + is the clipboard register

    This to me is much more legible that using markers

  3. is for manual indenting

    Shift + v,
    Shift + > for moving to the right. Shift + < for moving to the left. . repeats

this is fun :-)

Cherian
@Cherian great technique using the button images for keyboard input. What is the markup for this?
AJ
@AJ its <KBD></KBD>
Cherian
@Cherian, awesome...thx!
AJ
+1  A: 

Visual mode is useful if you want to apply a command to a section of text that isn't easily described as a primitive movement command. You can select some text in visual mode with a complex sequence of movements and then apply a command to that selection.

sth
+1  A: 

In addition to the other (great) answers, it's an easy way to define scope for an action. For example, to limit a search & replace to a specific method...

Say you have this code:

function foo() {
    abc();
    while (1) {
        def();
        abc();
    }
}

You can place the cursor on any of the braces or parentheses and press v, %, :, s/abc/xyz/g and your search & replace will have a defined scope in which the action will occur.

C4H5As
A: 

I often find myself using visual-block mode (Ctrl + v) more than any of the other visual modes.

You can easily remove indentation, comments, etc. once you are aware of this mode. In my experience, this is often faster than figuring out how to form an equivalent search-and-delete statement.

You can also add indentation (or comments as Cherian stated) by selecting a block of text and pressing I, typing whatever you want to add, and pressing Esc (note: you may need to redraw the screen (e.g. by moving the cursor) to see the effects of this).

Richard Marquez
A: 

One of the nice things about visual mode is that, because of Vim's focus on modality, you can perform most of the commands that you are used to (such as search/replace with :s, d to delete text, or r to replace text) while also seeing exactly what will be affected -- this allows you to determine the exact scope of whatever you are doing.

Furthermore, as someone else mentioned, you can easily insert a prefix (like a comment character or, say, & for alignment or \item in LaTeX) by selecting the first character of each line in visual block mode (ctrl+v), pressing I to insert before the first character, typing in whatever you want to insert, and then Escing back to normal mode.

The last kind of visual mode is visual line (Shift+v), which allows you to quickly select a number of lines. From there, you can change their indentation using > or < (prefix this with a number to indent by that many tabs), use d or y to delete or copy those lines, use zf to create a new fold from those lines, or use any other selection-based command.

Finally, there are a lot of other cool things you can do with visual mode, including gv to reselect your last visual[line/block] mode selection, gU to convert a visual selection to uppercase or gu for lowercase, and many more.

Toli