๐ŸŽš๏ธ relative-toggle.nvim

April 7, 2025 ยท View on GitHub

Automatically toggles smoothly between relative and absolute line numbers on various Neovim events. This is useful when you want to take advantage of both types of line numbers in different situations.

demo

๐Ÿš€ Installation

{
  "cpea2506/relative-toggle.nvim"
}

Requirements

  • Neovim >= 0.8.0

โš™๏ธ Setup

Once installed, this plugin is activated automatically. No setup is required unless you want to customize the options.

Available Options

OptionDescriptionTypeNote
patternPatterns where the plugin should be enabledstring|table:h autocmd-pattern
events.onEvents that turn relative numbers onstring|table:h autocmd-events
events.offEvents that turn relative numbers offstring|table:h autocmd-events

Default Configuration

require("relative-toggle").setup({
    pattern = "*",
    events = {
        on = { "BufEnter", "FocusGained", "InsertLeave", "WinEnter", "CmdlineLeave" },
        off = { "BufLeave", "FocusLost", "InsertEnter", "WinLeave", "CmdlineEnter" },
    },
})

๐Ÿ’ก Tips

  1. The keymap Ctrl-C does not trigger the InsertLeave event (:h i_CTRL-C), so youโ€™ll need to use another keymap that does. For example:

  2. To ensure the numbers actually toggle (lol), when you define an event in the events.on table, you should also define its corresponding "off" event in the events.off table. For example: BufEnter - BufLeave, VimEnter - VimLeave, ...

  3. The relativenumber option is always enabled by default. So, based on the value of vim.opt.number, the line numbers displayed relative to the cursor will change as follows (:h number_relativenumber):

    'nonu'          'nu'
    'rnu'           'rnu'
    
    |  2 apple      |  2 apple
    |  1 pear       |  1 pear
    |0   nobody     |3   nobody
    |  1 there      |  1 there
    

:eyes: Inspiration

:scroll: Contribution

For detailed instructions on how to contribute to this plugin, please see the contributing guidelines.