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 o开发者_StackOverflow中文版f "making some edit in a few keypresses that someone would've otherwise spent ten minutes doing manually"?
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 :-)
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.
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
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.
- 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.
Several good examples have already been given. Here are some others. I also use visual mode:
- To swap two regions of text.
- To refactor out variables, types, and functions.
- To surround the selection with pair of things (usually brackets)(NB: surround.vim also does this I guess), but also with control statements.
- But simply most of the time to have a more interactive way to yank or change text.
- To paste over without overwriting the current unnamed register.
- To highlight the current selection (with Mark.vim)
- And to do many other things I don't remember right know.
精彩评论