Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way in Vim to operate on relative ranges in visual mode?

Tags:

vim

range

I often delete, yank, and paste using something like this:

:3,6y

Since Vim 7, I've switched to using relative line numbers. I find it's much easier to use relative line numbering with commands like h,j,k,l etc.

Since switching to relative line numbering, I find it difficult to operate on absolute ranges (eg. :3,6y). It takes me too long to determine what absolute line numbers I need to select since Vim is displaying relative line numbers.

What is the best/quickest way to use visual selection on a range if your setup is displaying relative line numbering? Naively, I'm looking for something like:

:-2,+8y 

(yank the lines from 2 lines above my current position to 
8 lines below my current position.)
like image 641
drbunsen Avatar asked Oct 26 '11 13:10

drbunsen


People also ask

How do I use visual mode in Vim?

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim's Normal mode.

How do you copy a range of a line in vi?

The vi yy command "yanks" the current line into the vi general buffer. That is, it copies the line for you to put into the file immediately. The vi p and P commands "put" the line back into the file just after (p) or just before (P) the line on which the cursor is resting.


1 Answers

Did you try your naive thing?

:-2,+8y is equivalent to :.-2,.+8y and should do what you want.

Note that if you don't specify a number, 1 is assumed, so: :,+y means .,.+1 y: yank current and next line.

On :help range it is not well explained. The relevant parts are here:

Line numbers may be specified with:     *:range* *E14* *{address}*
    [...]
    .       the current line              *:.*
    [...]
Each may be followed (several times) by '+' or '-' and an optional number.
This number is added or subtracted from the preceding line number.  If the
number is omitted, 1 is used.

What the doc does not tell is that if the + r - is not preceded with anything, . is assumed.

like image 53
Benoit Avatar answered Oct 05 '22 11:10

Benoit