Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select relative range, ignoring invalid line numbers

Tags:

vim

ex

I was wondering if there was a syntax for specifying a range of relative lines in vim/ex that does not give 'invalid range' and instead gets as many lines as it can.

like image 458
0x777C Avatar asked Dec 05 '25 11:12

0x777C


1 Answers

There is no built-in way, but you can resolve the relative ranges into absolute line numbers yourself, and then limit the range to the available lines with :help min() and :help max(). So, for example, the following relative range:

:.-5,.+5 print

is equivalent to this:

:execute (line('.') - 5) . ',' . (line('.') + 5) 'print'

would be converted into this:

:execute max([1, (line('.') - 5)]) . ',' . min([line('$'), (line('.') + 5)]) 'print'
like image 174
Ingo Karkat Avatar answered Dec 08 '25 10:12

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!