Display relative and absolute line numbers simultaneously in Vim

Vim

Vim Problem Overview


Is there any way to display both relative and absolute line numbers simultaneously in Vim? Either side-by-side, or some other interesting presentation are fine.

Vim Solutions


Solution 1 - Vim

Yes, you can. Use RltvNmbr.vim to display the relative numbers, and use Vim to display the absolute ones:

enter image description here

Solution 2 - Vim

Vim 7.4 (Patch 7.3.787) will show the absolute line number instead of "0", when both "relativenumber" and "number" are set.

From :h relativenumber, 7.3

>When setting this option, 'number' is reset.

From :h relativenumber, 7.4

> The number in front of the cursor line also depends on the value of 'number', see |number_relativenumber| for all combinations of the two options.

Solution 3 - Vim

Reasonably speaking, you can't. Line numbering is one-per-buffer, controlled solely by the 'number' option.

One crazy notion I tried: :vertical diffsplit plus :setlocal relativenumber. This gets two copies of the buffer next to one another, linked in scroll position because they're in diff mode. However, it really doesn't work properly. The relative numbers aren't updated successfully (on 7.3.154, bug potentially fixed later, but I doubt it—it's scarcely worth it), and even if they did, you'd be stuck with a great eyesore and harder management and navigation.

You could do it with a plugin writing to a special buffer which you would keep on the left, but the implementation would be ugly, and it would still make navigation hard (because it's another buffer). I for one am certainly not going to spend time writing such a plugin, though I must confess I'd like to show both forms of line numbering.

Solution 4 - Vim

Many years later and this feature is now part of Vim and known as hybrid numbers.

You can turn this feature on by entering:

:set number relativenumber

No speed penalty either. As far as I can tell.

Solution 5 - Vim

Combining plnx and Macario answers with vim 7.4.

autocmd WinEnter,FocusGained * :setlocal number relativenumber
autocmd WinLeave,FocusLost   * :setlocal number norelativenumber

This displays Relative Numbering (with current line in Absolute) in the focused window, and Absolute in the non-focused window.

Solution 6 - Vim

This are my mappings related to relative and absolute line numbers toggles between relative and absolute.

Entering a window sets the window to relative numbers, while leaving it set it to absolute. Then if you do a vertical split of the window you would se the same buffer with relative and absoulte numbers.

" setglobal relativenumber
autocmd WinEnter * :setlocal relativenumber
autocmd WinLeave,FocusLost * :setlocal number
autocmd InsertEnter * :setlocal number
autocmd InsertLeave * :setlocal relativenumber

function! g:ToggleNuMode()
  if(&relativenumber == 1)
    set number
  else
    set relativenumber
  endif
endfunc

map <C-l> :call g:ToggleNuMode()<CR>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmt3View Question on Stackoverflow
Solution 1 - VimOrangenhainView Answer on Stackoverflow
Solution 2 - VimplnxView Answer on Stackoverflow
Solution 3 - VimChris MorganView Answer on Stackoverflow
Solution 4 - VimallesklarView Answer on Stackoverflow
Solution 5 - Vimgo2nullView Answer on Stackoverflow
Solution 6 - VimMacarioView Answer on Stackoverflow