Skip to content

How to display line numbers in Vi editor?

Vi or Vim is one of the most popular text editors among Linux users. The program offers a bunch of great features for a wide range of audiences, from developers to regular users looking to edit or create documents.

One such feature in Vi is the ability to show or hide line numbers. In this article, we’re going over how you can enable line numbers in Vi.

Also read: How to create Bash aliases?


See Absolute line numbers in Vi

Absolute line numbers are exactly what they sound like. Each line is numbered as you’d expect, starting from going all the way until the file ends. All you have to do to enable them is enter command mode using the Esc key and type the following command. 

:set number
How to display line numbers in Vi editor? | Candid.Technology

To disable the line numbers, use this command.

:set nonumber

See Relative line numbers in Vi

When you enable relative line numbers, the line your cursor is on becomes line zero, and any lines above or below are incrementally numbered. This comes in handy if you’re looking to base your code around a certain reference or are working with someone to whom you need to point out specific lines relative to a particular line. 

To enable relative line numbers, enter command mode using the Esc key and use either of the following commands.

:set relativenumber

Or

:set rnu
How to display line numbers in Vi editor? | Candid.Technology

If you want to toggle relative line numbering, add an exclamation mark after the commands. You can disable relative line numbering using these two commands.

:set norelativenumber

Or

:set nornu

Also read: How to save and exit in Vim?


See Hybrid line numbers in Vi

Starting from Vim 7.4 and later, you can enable hybrid line numbers. Under this system, you can see absolute and relative line numbers side by side, making it easy to refer to just about any line in your file.

To enable this, enter command mode by pressing the Esc key and type in the commands for enabling absolute and relative numbering. 

:set number relativenumber
How to display line numbers in Vi editor? | Candid.Technology

Disabling the numbering also works in the same way. Just disable both absolute and relative numbering as you usually would.


How to permanently enable line numbers in Vi?

By default, line numbers are disabled in Vi. To permanently enable them, you’re going to have to append the command for the line numbering you want in the .vimrc file, which manages all Vim configurations. 

Just type vim ~/.vimrc to open the file and type in your command as you would in the command mode. 

Also read: How to delete lines in Vim?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

We may earn a commission if you buy something from a link on this page. Thanks for your support.







>