Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to yank relative line?

Tags:

vim

I have relative line numbers turned on.

I can yank line 10 using :10y.

But how to I yank say the 5th line below the current line without jumping to said line, yanking and jumping back (i.e. 5jY5k).

If I had this file:

2   describe 'foobar' do
1     it 'should be cool' do
46      # do stuff
1     end
2  end

I am on line 46 and I want to yank relative line 1 or 2, either above or below.

like image 305
Kris Avatar asked Nov 17 '16 11:11

Kris


1 Answers

You can use +n and -n for relative addresses:

:+2y    " Two lines after the current line
:-2y    " Two lines before the current line

And you can also combine this:

:-2,+2y   "  Two lines before the cursor and two lines after

Also see this answer for some more examples and :help [range] for the Vim documentation.

like image 62
Martin Tournoij Avatar answered Sep 24 '22 09:09

Martin Tournoij