sev
sev

Reputation: 1822

Vim yank from current to nth line including nth line

In vim nyy will yank n lines. This means it will yank all the lines including the current line and the (n-1)th line. Is there a command with which I can yank all the lines inlcuding the current and the nth line -> yanking n+1 lines?

If so could one override the nyy command to this command in vimrc?

Background: I have my editor set up to show me the line number relative to the one I am at. This is convenient for navigation e.g. 2j sets the cursor to the line with the 2. When I use 2yy however it yanks all the lines excluding the one with the 2 which trips me up every time.

enter image description here

Upvotes: 2

Views: 232

Answers (1)

romainl
romainl

Reputation: 196856

Note that your screenshot is not a Vim screenshot, which means that whatever advice you may get regarding Vim may or may not work in the Vim emulator you use.

Anyway, the 2 in 2j and in 2yy is a count, which can have varying effects depending on what kind of command you use it with.

In 2j, it literally means "do j two times", which tells Vim to move the cursor two lines downwards. 2j is a motion.

In 2yy, it literally means "do yy on this line and the next one", which tells Vim how many lines to to yank, including the current one. It means that the behaviour that "trips you up every time" is perfectly normal, consistent, and expected.

That's not what you want.

Vim yank from current to nth line including nth line

What you actually want is to use the downwards motion you already have, 2j, with the y operator: y2j.

See :help operator.

Upvotes: 4

Related Questions