1

I have the text:

abcd
abcd
abcd
abcd

my cursor is in the first line, and I want to yank the first 2 lines.

Usually I would do it by pressing Shift+v, selecting the second line by going down with j and yanking with y.

How can this be achieved without using visual mode?

6
3

yank is an operator like d and c, which means it works on all motions and text-objects (see the relevant help sections).

That means that yip, y), ygg, yib, y/pattern<CR>, yfc etc., all work (and with registers, too)! They can potentially be multiple lines.

In this case I would probably use yj (and not y2j) because yank will include the current line and the one we land on.

Note that some motions are not line-wise by default. You can use yV{motion} to force a line-wise yank.

4
  • What's the difference between a motion and a text-object - will you give an example? I would consider w, W, e, etc. all motions, since they move the cursor.
    – Shuzheng
    Oct 14 '20 at 9:31
  • @Shuzheng that’s correct. Motions move the cursor. Text objects are a bit harder to explain ; they’re like « targets »â€”you can select them in visual mode or operate on them with operators. See the help on text objects for more. There are a couple examples in the answer
    – D. Ben Knoble
    Oct 14 '20 at 11:58
  • So, would ib, ip, and a" be considered text objects? I've seen these in :h text-objects. I use these often with the d (delete) operator.
    – Shuzheng
    Oct 14 '20 at 12:10
  • @Shuzheng yes, those are text objects. y,c,g~, etc all work with them too (not just d!)
    – D. Ben Knoble
    Oct 14 '20 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.