Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative number in vim

Tags:

vim

I noticed a strange behavior in vim regarding the relative numbers:

:set nu

  1
  2
  3 _
  4
  5
  6
  ~

:set relativenumber

  2
  1
3   _
  1
  2
  3
  ~

:set nonu

  2
  1
  0 _
  1
  2
  3
  ~

I was asking myself why sometime I see the absolute current line number and sometime I see 0 which is pretty useless.

Is this behavior normal and how to correctly number and relativenumber?

like image 544
nowox Avatar asked Mar 02 '26 18:03

nowox


2 Answers

It's the expected behavior. From :help number_relativenumber

The 'relativenumber' option changes the displayed number to be
relative to the cursor.  Together with 'number' there are these
four combinations (cursor in line 3):

        'nonu'          'nu'            'nonu'          'nu'
        'nornu'         'nornu'         'rnu'           'rnu'

    |apple          |  1 apple      |  2 apple      |  2 apple
    |pear           |  2 pear       |  1 pear       |  1 pear
    |nobody         |  3 nobody     |  0 nobody     |3   nobody
    |there          |  4 there      |  1 there      |  1 there
like image 92
Stefan Avatar answered Mar 05 '26 08:03

Stefan


That behavior depends on how number and relativenumber are combined.

Everything is explained in :help number_relativenumber, a subsection of :help 'number' that you would have found if you had tried a little harder.

like image 35
romainl Avatar answered Mar 05 '26 09:03

romainl



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!