Reputation: 789
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
Reputation: 196526
A few other ways that don't use visual mode at all:
using marks
leave a mark somewhere with ma
move somewhere else
yank from here to there with y'a
using search motions
localize some unique token at the end of the part you want to yank
yank from here to there with y/foo<cr>
(forward search) or y?bar<cr>
(backward search)
using text-objects
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", ]
…
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
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
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