www-gem words

Hybrid mode

Published on

vim 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:

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.


Interact with this post using Mastodon or

Comment on wwwgem's post

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈

More food for thoughts? Check other posts about: #Vim