2

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.

0x777C
  • 993
  • 7
  • 21
  • There's nothing built in that will work on the command line. If you can narrow your use case, you might be able to accomplish something with a custom function (for example, [vim-abolish](https://github.com/tpope/vim-abolish) has a custom substitute command that could be modified to ignore range errors). – Jim Stewart Nov 02 '18 at 22:04
  • I just want to display the lines with their line numbers in ex – 0x777C Nov 03 '18 at 00:50

2 Answers2

2

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'
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

My CmdlineSpecialEdits plugin has (among many others) a CTRL-G + mapping that changes relative ranges like .-5,.+5 to absolute line numbers and vice versa. It also corrects addressing out of bounds (<= 0 and larger than the last line number) and backwards ranges.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324