Interact with this post using Mastodon or
Hybrid mode
Published on
While relative numbers are handy, the “hybrid” mode is real power.
The table below comes from the :help number_relativenumber
.
# nonu : don't print the line number in front of each line
# nu : print the line number in front of each line
# rnu : show the line number relative to the line
# with the cursor in front of each line.
# nornu: don't show the line number relative to the line
# with the cursor in front of each line.
| nonu / nornu | nu / nornu | nonu / rnu | nu / 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 |
In this table, your cursor is on the “nobody” line.
Looking at this, you will notice that:
-
Column 1 gives you no clues about where you are in the file, nor about the lines below and above your cursor.
-
Column 2 shows the number of each line, which helps you to know where you are in the file.
-
Column 3 gives you clues about the lines below and above your cursor relatively to your cursor line.
-
Column 4 gives you clues about the lines below and above your cursor relatively to your cursor line, but also lets you know where you are in the file by using the absolute number for your cursor line. That’s the “hybrid” mode.
This feature has been introduced in vim 7.1
If you think about a real file with dozens of lines, you can imagine how difficult it can be to work on a range of lines if you have to count how far from your cursor line the first/last line you want to select, copy, or delete is. With relative line numbers, you can easily identify this line. Hence, you can for example select, copy, or delete the 10 lines above your cursor line with v10k
, y10k
, or d10k
.
How cool is that?
Additionally, if you have multiple files open, it may be painful to have the “hybrid” mode turned on everywhere. If it’s handy for the file you are editing, you may prefer the use of absolute numbers for the other files. This can be achieved by adding the code below in your ~/.vimrc
:
set number
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,WinEnter * if &nu | set rnu | endif
autocmd BufLeave,FocusLost,WinLeave * if &nu | set nornu | endif
augroup end
This post has been published on lazybear blog as well
Thanks for your read. Hope it's been useful to you.
✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈
More food for thoughts? Check other posts about: #(Neo)vim