Calvin Cheng
Calvin Cheng

Reputation: 36506

Vim: faster way to select blocks of text in visual mode

I have been using vim for quite some time and am aware that selecting blocks of text in visual mode is as simple as SHIFT+V and moving the arrow key up or down line-by-line until I reach the end of the block of text that I want selected.

My question is - is there a faster way in visual mode to select a block of text for example by SHIFT+V followed by specifying the line number in which I want the selection to stop? (via :35 for example, where 35 is the line number I want to select up to - this obviously does not work so my question is to find how if something similar to this can be done...)

Upvotes: 248

Views: 206728

Answers (20)

J.Dubbs
J.Dubbs

Reputation: 21

Shift+v $ %

Visual mode, then $ to move the cursor to the beginning bracket, then % to move to the matching end bracket.

Upvotes: 1

Blcknx
Blcknx

Reputation: 2431

To visually select the current paragraph I use {V}. It's intuitive for me.

Vscode does support a vim mode, though it is limited. Vap does not work.

Upvotes: 1

Samuel-Zacharie Faure
Samuel-Zacharie Faure

Reputation: 1152

Text-objects will help you, especially if you don't want to count lines manually, but VIM has no native text-object based on indentation.

Thankfully, this plugin does just that.

Upvotes: 0

Pascowl
Pascowl

Reputation: 11

Presss V to select the current line and enter the line number on keyboard and the press G.

Upvotes: 1

yoann guillard
yoann guillard

Reputation: 46

I use this with fold in indent mode :

v open Visual mode anywhere on the block

zaza toogle it twice

Upvotes: 1

ReyApr
ReyApr

Reputation: 385

simple just press Shift v line number gg

example: your current line to line 41 Just press Shift v 41 gg

Upvotes: 15

SergioAraujo
SergioAraujo

Reputation: 11790

It could come in handy to know:

In order to select the same ammount of lines for example use 1v You should have done some modification to be able to use 1v, blockwise or linewise.

Today I saw this amazing tip from here:

 :5mark < | 10mark > | normal gvV
 :5mark < | 10mark > | normal gv

You can also reset the visual block boundaries doing so:

m< .......... sets the visual mode start point
m> .......... sets the visual mode end point

Upvotes: 1

Sergio Abreu
Sergio Abreu

Reputation: 2889

For selecting all in visual: Type Esc to be sure yor are in normal mode

:0 

type ENTER to go to the beginning of file

vG

Upvotes: 0

Peng Zhang
Peng Zhang

Reputation: 3585

Shift+V n j or Shift+V n k

This selects the current line and the next/previous n lines. I find it very useful.

Upvotes: 12

iggy
iggy

Reputation: 1743

You can press vi} to select the block surrounded with {} brackets where your cursor is currently located.

It doesn't really matter where you are inside that block (just make sure you are in the outermost one). Also you can change { to anything that has a pair like ) or ].

Upvotes: 13

Jay
Jay

Reputation: 57899

In addition to what others have said, you can also expand your selection using pattern searches.

For example, v/foo will select from your current position to the next instance of "foo." If you actually wanted to expand to the next instance of "foo," on line 35, for example, just press n to expand selection to the next instance, and so on.

update

I don't often do it, but I know that some people use marks extensively to make visual selections. For example, if I'm on line 5 and I want to select to line 35, I might press ma to place mark a on line 5, then :35 to move to line 35. Shift + v to enter linewise visual mode, and finally `a to select back to mark a.

Upvotes: 268

jackson
jackson

Reputation: 41

You can always just use antecedent numbers to repeat actions:

  • In visual mode, type 35 and the cursor will move down 35 times, selecting the next 35 lines
  • In normal mode:
    • delete 35 lines 35dd
    • paste 35 times 35p
    • undo 35 changes 35u
    • etc.

Upvotes: 4

kzh
kzh

Reputation: 20598

Vim is a language. To really understand Vim, you have to know the language. Many commands are verbs, and vim also has objects and prepositions.

V100G
V100gg

This means "select the current line up to and including line 100."

Text objects are where a lot of the power is at. They introduce more objects with prepositions.

Vap

This means "select around the current paragraph", that is select the current paragraph and the blank line following it.

V2ap

This means "select around the current paragraph and the next paragraph."

}V-2ap

This means "go to the end of the current paragraph and then visually select it and the preceding paragraph."

Understanding Vim as a language will help you to get the best mileage out of it.

After you have selecting down, then you can combine with other commands:

Vapd

With the above command, you can select around a paragraph and delete it. Change the d to a y to copy or to a c to change or to a p to paste over.

Once you get the hang of how all these commands work together, then you will eventually not need to visually select anything. Instead of visually selecting and then deleting a paragraph, you can just delete the paragraph with the dap command.

Upvotes: 121

mabeiyi
mabeiyi

Reputation: 367

} means move cursor to next paragraph. so, use v} to select entire paragraph.

Upvotes: 1

bjfletcher
bjfletcher

Reputation: 11508

v%

will select the whole block.

Play with also:

v}, vp, vs, etc.

See help:

:help text-objects

which lists the different ways to select letters, words, sentences, paragraphs, blocks, and so on.

Upvotes: 9

michaelmichael
michaelmichael

Reputation: 14125

v35G will select everything from the cursor up to line 35.

v puts you in select mode, 35 specifies the line number that you want to G go to.

You could also use v} which will select everything up to the beginning of the next paragraph.

Upvotes: 48

bheeshmar
bheeshmar

Reputation: 3205

G                       Goto line [count], default last line, on the first
                        non-blank character linewise.  If 'startofline' not
                        set, keep the same column.
                        G is a one of jump-motions.

V35G achieves what you want

Upvotes: 134

mateusz.fiolka
mateusz.fiolka

Reputation: 3100

For selecting number of lines:

shift+v 9j - select 10 lines

Upvotes: 16

&#181;Bio
&#181;Bio

Reputation: 10748

v 35 j

text added for 30 character minimum

Upvotes: 6

Related Questions