www-gem words

Hybrid mode

Published on

While relative numbers are handy, the “hybrid” mode is real power.

The table below comes from the :help number_relativenumber.

In this table, your cursor is on the “nobody” line.

nonu (1) / nornu (2)nu (3) / nornunonu / rnu (4)nu / rnu
apple1 apple2 apple2 apple
pear2 pear1 pear1 pear
nobody3 nobody0 nobody3 nobody
there4 there1 there1 there

(1) nonu : NO line NUmber (2) nornu: NO Relative NUmber (3) nu : show NUmber (4) rnu : show Relative NUmber

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:

bash
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



More food for thoughts? Check other posts about: #(Neo)vim


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.

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