5

I have been using vim for a couple of years now, and though I have learnt a lot of time saving shortcuts, but I have never used the Visual mode, which is supposed to be all powerful :

... Visual block mode (to edit columns) is something many editors lack, but that I can't live without. I have shocked and awed people at work using just this, making some edit in a few keypresses that someone would've otherwise spent ten minutes doing manually.

I want to understand why and when should I be using Visual mode.

Can someone give me an example of "making some edit in a few keypresses that someone would've otherwise spent ten minutes doing manually"?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Lazer
  • 90,700
  • 113
  • 281
  • 364

6 Answers6

5

If you see CTRL-CCTRL-V and recognise what it does, you should use visual mode.

If, like me, you see A:esc0df:$p$x as an edit command, don't bother :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • omg! I had no idea that [Ctrl + C], [Ctrl + V] worked with vim as well. thanks! – Lazer Oct 20 '10 at 18:47
  • Are you using gvim.exe ? If so, Ctrl-V is remapped by default to Ctrl-Q due to mswin.vim. This is really evil. – Benoit Oct 21 '10 at 08:02
  • I found it very difficult to understand what you're trying to say with this example because `A` `:` `esc` `0` `d` `f` `:` seems like a long-winded and less reliable way to do `0` `d` `$` which requires additional cleanup after paste. I agree that we can compose motion and edit commands in ways that are just as good as most uses of visual mode, but if you're going to use an example which involves inserting anchor characters for later commands, you should probably use one that's not trivially reproducible without the anchor characters. – mtraceur Apr 10 '23 at 17:53
  • @mtraceur: those two things are not actually equivalent (think of what happens if the line contains a colon already). Your question appears to actually *support* my contention that, if you grok the complexities of things like that monstrosity, you can use them rather than visual mode :-) – paxdiablo Apr 10 '23 at 18:28
  • @paxdiablo That's what I was referring to when I called it "less reliable", but now that I know you *intentionally* wanted that behavior, I see how it can be useful. You're giving an example of turning `foo: qux` into ` qux:foo` (or less ambiguously, `foo: qux: alp` into ` qux: alp:foo`). – mtraceur Apr 10 '23 at 21:53
1

When I use visual mode, it's to select whole lines or blocks. For example you can do [esc][shift+v][y] to copy the currently line I'm on. Here's more information.

shoebox639
  • 2,312
  • 15
  • 14
  • If I try [v][select lines][yy], yy is typed as text. What am I doing wrong? – Lazer Oct 20 '10 at 18:25
  • @Alex, thanks. What happens if I press [v] twice? It still selects text but I am not able to use commands. – Lazer Oct 20 '10 at 18:30
  • Na, just press [v] once, and use [h][j][k][l] to move the cursor around and select. Pressing [v] twice will enter start and end selection, so nothing happens. – shoebox639 Oct 20 '10 at 19:59
1

Visual mode allows you to perform an operation on a block of text. It is the only way to perform an operation on a block in Vim.

A simple example of this would be copying or moving text.

A more advanced example would be sorting the lines in a certain part of a file. You can do this by entering visual mode, selecting a block of text, pressing Esc to enter command mode, and typing !sort. You can see more details about his example and how it works here: http://www.oualline.com/vim-cook.html#sorting_visual

Alan Geleynse
  • 24,821
  • 5
  • 46
  • 55
1

I actually just did a screencast showing off great uses for visual mode. You can check it out at http://lococast.net/archives/241

As other's have said, it's great for doing any sorts of editing (edit, remove, search/replace) withing a specific range of code.

Rick
  • 15,484
  • 5
  • 25
  • 29
0
  • Insert a column of commas.
  • Delete a column of commas.
  • Act on rectangular selections.
  • Act on a section of text that is not a line or word.
Eric Towers
  • 4,175
  • 1
  • 15
  • 17
0

Several good examples have already been given. Here are some others. I also use visual mode:

Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83