Skip to main content
  1. Posts/

Line Number Settings for More Efficient Movement in Neovim

··468 words·3 mins·
Nvim Windows CentOS Tmux Mac
Table of Contents

In this post, I would like to share line number settings in Neovim to move the cursor more efficiently.

Absolute number and relative number in Neovim
#

Show absolute line number
#

To know where we are in a file, it is useful to show the line number on the leftmost column of the window. To show line numbers in neovim, we can set the number option:

set number

The absolute line number will be shown at the leftmost column of current window.

Combine absolute number and relative number
#

While the number option is useful, it is not convenient for us to move the cursor to other lines. Because cursor movement in Neovim is based on relative distance. For example, to go to 3 lines above or below the current line, we use 3k or 3j. If we want to go to a specific line, we have to manually calculate the distance between current cursor line and the destination line, which is cumbersome and error-prone.

To alleviate this issue, Neovim also support showing relative number:

set relativenumber

This option can be combined with the number option. The end result is that only the cursor line shows absolute line number, and all the other lines show relative number( or distance) relative to current line.

Automatic toggle of relative line number
#

In some situations, it is more convenient to see absolute line numbers, e.g., when we want to debug a file and run to specific lines. We can the use plugin vim-numbertoggle to automatically toggle relative number based on several events. e.g., when we enter insert mode or lost focus of a window.

Automatic number toggle does not work inside Tmux?
#

I have found that the number toggle function does not work inside tmux sessions by default1. If I open two tmux panes side by side and open a file in Neovim in one pane and then switch to another tmux pane, the relative line number in Neovim does not change to absolute number.

I opened an issue on the Neovim GitHub repo and get the right answer from the developer. We need to turn on the focus-events option for tmux. From the Tmux documentaion:

focus-events [on | off]
        When enabled, focus events are requested from the terminal if
        supported and passed through to applications running in tmux. Attached
        clients should be detached and attached again after changing this
        option.

We need to edit the tmux config file ~/.tmux.conf and add the following setting:

set -g focus-events on

Refresh the tmux session or start a new tmux session and automatic relative number toggle should work as expected now.

References
#


  1. I am using Tmux 2.8-rc ↩︎

Related

Tmux Plugin Install and Management
··426 words·2 mins
Linux Tmux Mac CentOS
Nvim-qt Settings on Windows 10
··732 words·2 mins
Nvim Windows Font 字体
How Is Newline Handled in Python and Various Editors?
··1035 words·5 mins
Technique Nvim Windows Mac Sublime-Text