views:

130

answers:

6

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"?

+1  A: 

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
If I try [v][select lines][yy], yy is typed as text. What am I doing wrong?
Lazer
hit [escape] first
Alex
@Alex, thanks. What happens if I press [v] twice? It still selects text but I am not able to use commands.
Lazer
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
+1  A: 

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
A: 
  • 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
@Eric: I know how to do the last two. How can we do the first two?
Lazer
@Lazer: Insert column:go to start point. <ctrl>-v go to end point. x .
Eric Towers
@Lazer: Insert column of commas: go to start point. <ctrl>-v go to end point. I , <esc>
Eric Towers
+1  A: 

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
+3  A: 

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
omg! I had no idea that [Ctrl + C], [Ctrl + V] worked with vim as well. thanks!
Lazer
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
A: 

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

Luc Hermitte