When is Visual mode used in Vim?
Asked Answered
W

7

15

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?

Watershed answered 28/1, 2010 at 3:37 Comment(1)
I still haven't figured out the use of it after reading the answers. I'm sure there are but not for me who still prefers TextWrangler / BBEditPhenica
N
10

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.

Nyssa answered 28/1, 2010 at 3:40 Comment(2)
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.Ify
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).Darladarlan
K
5

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.

Kathline answered 28/1, 2010 at 4:26 Comment(1)
With regard to "see exactly what's affected": Several of Vim's functions (I know that :s and :g are like this) work always on whole lines, even when just part of the line is selected.Pogonia
S
4
  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 :-)

Scissure answered 28/1, 2010 at 3:44 Comment(2)
@Scissure great technique using the button images for keyboard input. What is the markup for this?Nyssa
I don't understand the first example, I think it's demonstrating how to insert a single character at the start of a block, but I don't see why visual mode is useful to insert a single character, when you could just navigate there and type the single character. I expected to see the char inserted at the start of each line. If I've misunderstood, which action/keypress specifically does it for each line?Ify
I
3

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.

Ify answered 28/1, 2010 at 4:7 Comment(0)
P
1

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.

Patriciate answered 28/1, 2010 at 3:48 Comment(0)
M
0

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).

Midweek answered 28/1, 2010 at 4:15 Comment(0)
R
0

I haven't seen the following mentioned, probably because they're subtle.

1 - Less hassle with the unnamed register

Whenever you copy (yank) some text, and then want to d to change some other text, e.g., diw to "delete inner word," Vim will place the deleted text into the unnamed register. Then if you try to paste, it will just paste the deleted text right back unless you do "0p to paste from the 0 register.

But with visual mode, you can just do something like viwp and don't have to mess with registers.

So, for comparison, to copy and replace inside some parens:

yiw -> move somewhere -> vi(p

vs

yiw -> move -> ci(<C-r>0p

yiw -> move -> "_di(p

yiw -> move -> di("0P

Note: this also works for deleting text and pasting it back over a text object. See here.

2 - Jumping to parts of a text object

If you want to jump to the beginning or end of a text object, you can select it in visual mode and press o. For example, va" to select anywhere inside quotes, and then press o to jump to the matching quotes, kind of like % for matching brackets.

Refine answered 22/4, 2020 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.