Sean
Sean

Reputation: 789

How can I highlight multiple lines in gVim without using the mouse?

Vim noob here. I am trying to select multiple lines of code to copy and paste in other areas. Is there a way to do this without using the mouse?

Upvotes: 3

Views: 3102

Answers (3)

romainl
romainl

Reputation: 196526

A few other ways that don't use visual mode at all:

using marks

  1. leave a mark somewhere with ma

  2. move somewhere else

  3. yank from here to there with y'a

using search motions

  1. localize some unique token at the end of the part you want to yank

  2. yank from here to there with y/foo<cr> (forward search) or y?bar<cr> (backward search)

using text-objects

  1. determine what text-object would map to what you want to yank:

    • inner/outer word, iw/aw

    • inner/outer pair, i'"([{</a'"([{<

    • inner/outer html tag, it/at

    • sentence, s

    • paragraph, p

    • "block", ]

  2. yank that text-object with, say, yip

using other motions

  • yank to end of function: y]}

  • yank to end of file: yG

all of the above solutions with visual mode

  • V'ay

  • V/foo<cr>y

  • V?bar<cr>y

  • Vipy, etc.

  • V]}y

  • VGy


:h motion.txt will hopefully blow your mind, like it did to mine.

Upvotes: 4

daniel gratzer
daniel gratzer

Reputation: 53881

Yep, in normal mode type V[direction] and you will highlight multiple lines. If you don't want whole lines, use v instead of V. To copy it, hit y and move to the area which you want to paste in and hit p. To delete it, instead of y use x.

Alternatively, you can simply use [number of lines]yy to yank some number of lines or [number of lines]dd to cut some number of lines. In this case pasting is the same.

Upvotes: 1

alestanis
alestanis

Reputation: 21863

You can place your cursor in the first line you want to copy and then type nyy where n is the number of lines you want to copy. For example, type 2yy to copy the two lines under the cursor.

Then, you can paste them using p.

You can also select multiple lines by placing your cursor somewhere and keeping Shift pressed. Move your cursor to the end of the desired selection and stop pressing Shift. Then copy using just y (and not yy) and paste with p.

Upvotes: 2

Related Questions