How to Enable Line Numbers in Vim/Vi

Suraj Joshi Feb 02, 2024
  1. Absolute Line Numbering in Vim/Vi
  2. Relative Line Numbering
  3. Hybrid Line Numbering
  4. Permanently Enable Vim Line Numbers
How to Enable Line Numbers in Vim/Vi

Vim and Vi are one of the most used text editors among Linux users. Line numbering is essential for text editors while pair programming, debugging scripts, indicating a specific line, and much more.

The line numbering feature is inactive by default in Vim and Vi editors, but it can be easily turned on configuring the settings. Vim and Vi support three types of line numbering: Absolute, Relative, and Hybrid.

Absolute Line Numbering in Vim/Vi

Absolute line numbering is the numbering method that represents the successive numbering of lines.

Enable Absolute Line Numbering in Vim/Vi

To enable absolute line numbering in Vim/Vi, we need to perform the following steps:

  • Switch to command mode by pressing the Esc key.
  • Now press : and the cursor will appear at the bottom left of the terminal.
  • To enable absolute numbering, type set number or set nu and then hit Enter.

absolute_numbering_vim

Disable Absolute Line Numbering in Vim/Vi

To disable absolute line numbering in Vim/Vi, type set nonumber or set nu! command in the command bar and then hit Enter.

Relative Line Numbering

Relative line numbering is the numbering method that represents line number with respect to the cursor’s position.

Enable Relative Line Numbering in Vim/Vi

To enable relative line numbering in Vim/Vi, we need to perform the following steps:

  • Switch to command mode by pressing the Esc key.
  • Now press : and the cursor will appear at the bottom left of the terminal.
  • To enable absolute numbering, type set reltivenumber or set rnu and then hit Enter.

realtive_numbering_vim

In the figure, the cursor is at line 4 in absolute numbering. So the line numbering here is done with respect to the 4th line.

Disable Relative Line Numbering in Vim/Vi

To disable relative line numbering in Vim/Vi, type set norelativenumber or set nornu command in the command bar and then hit Enter.

Hybrid Line Numbering

Hybrid line numbering is the numbering method that is a combination of both absolute and relative numbering methods.

Enable Hybrid Line Numbering in Vim/Vi

To enable hybrid line numbering in Vim/Vi, we need to perform the following steps:

  • Switch to command mode by pressing the Esc key.
  • Now press : and the cursor will appear at the bottom left of the terminal.
  • To enable absolute numbering, type set number relativenumber and then hit Enter.

hybrid_numbering_vim

In the figure, the cursor is at line 4 in absolute numbering. Hence, the numbering for other lines except line-4 is done with respect to line-4, but the numbering of line-4 is done by absolute numbering.

Disable Hybrid Line Numbering in Vim/Vi

To disable hybrid line numbering in Vim/Vi, type set nonumber norelativenumber command in the command bar and then hit Enter.

Permanently Enable Vim Line Numbers

The methods stated above do not sustain permanently. Once we close the Vim/Vi editor and reopen it, it gets switched back to default mode i.e. line numbers are hidden.

To enable the Vim Line numbers permanently, we need to perform the following steps:

  • Open the Vim configuration file .vimrc with the command:
    vim ~/.vimrc
    
  • Add appropriate command in the .vimrc file to enable line numbering permanently.
    :set [required_mode]
    

    If you don’t have any .vimrc file in the home directory, we can easily create it using vim .vimrc command.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Vim