Show Line Numbers in Vim [Absolute, Relative, Hybrid]


Tips and Tricks, Linux

Author: Omer Cakmak
Reviewer: Deepak Prasad

Vim Editor was written by Bram Moolenaar in 1991, based on the Vi Text editor. At first his name meant Vi Imitation. Renamed "Improved VI" as a result of many improvements found in version 2.0 of Vim.

This text editor, which can be run on many operating systems, has many features that cause it to be preferred. One of them is that it displays the line number in the opened file.

 

How to Show Line Numbers in Vim?

By default, Vim doesn't show line numbers, so you need to enable/turn this setting on. The feature to show line numbers is done in 3 ways:

  • Absolute Line Numbers
  • Relative Line Numbers
  • Hybrid Line Numbers

Now let's explain them in order.

 

Absolute Line Numbers

For this feature to be active, after opening the file with vim, first ESC and then :set number or :set nu must be written.

Vim

 

To disable this feature, just type :set nonumber or :set nonu  after pressing ESC.

Show Line Numbers in Vim [Absolute, Relative, Hybrid]

 

After pressing ESC, typing :set number! or :set nu! also removes the number line.

It can be useful for finding the wrong line in the log result. When you open the file, the problem is solved quickly by going to the wrong line. Open the file, type the line number with the following command:

:11

Show Line Numbers in Vim [Absolute, Relative, Hybrid]

In summary:

  • absolute line numbers active
    :set number
    set :nu
  • absolute line numbers inactive
    :numerous settings
    :set it
  • absolute line numbers inactive(Other option)
    :set the number!
    set :nu!

 

Relative Line Numbers

In this feature, the line number changes according to the line where the cursor is located. Each line in the file is numbered according to the current position of the cursor to show the distance to that line. The current row is marked as 0, above and below as 1.

For this feature, the following commands are used after pressing ESC:

  • relative line numbers active
    :set relativenumber
    :set rnu
  • relative line numbers inactive
    :set norelativenumber
    :set nornu
  • relative line numbers inactive(Other option)
    :set relativenumber!
    :set rnu!

Show Line Numbers in Vim [Absolute, Relative, Hybrid]

 

Hybrid Line Numbers

In Vim 7.4 and later it was possible to enable both absolute and relative line numbers at the same time. It's called hybrid line number mode. While either one is active, the other can also be activated. In other words, when Absolute is active and Relative is activated, or vice versa, hybrid mode occurs.

If none is active, hybrid mode is activated with the following commands after ESC:

  • hybrid line numbers active
    :set number relativenumber
    :set nu rnu

Show Line Numbers in Vim [Absolute, Relative, Hybrid]

For inactive mode:

  • hybrid line numbers inactive
    :set nonumber norelativenumber
    :set nonu nornu
  • hybrid line numbers inactive(Other option)
    :set number! relativenumber!
    :set nu! rnu!

If one of the modes is deactivated, the other mode remains active. If both are disabled, line numbers will not appear.

 

Summary

We talked about a feature of the Vim editor. Vim is a very powerful and advanced editor. Some users develop apps with this editor.
For more information about Vim, you can get help from the manual page:

foc@fedora:/tmp$ man vim

NAME
vim - Vi IMproved, a programmer's text editor

SYNOPSIS
vim [options] [file ..]
vim [options] -
vim [options] -t tag
vim [options] -q [errorfile]
...

and the --help parameter:

foc@fedora:/tmp$ vim --help
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Oct 11 2022 00:00:00)

Usage: vim [arguments] [file ..]       edit specified file(s)
   or: vim [arguments] -               read text from stdin
   or: vim [arguments] -t tag          edit file where tag is defined
   or: vim [arguments] -q [errorfile]  edit file with first error

Arguments:
   --			Only file names after this
   -v			Vi mode (like "vi")
   -e			Ex mode (like "ex")
   -E			Improved Ex mode
...

 

References

stackoverflow.com - Command for toggling line numbers in vi

 

Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment