Development

How to Show Line Numbers in Vim

Captain Salem 3 min read

Vim is one of most popular and extensive text editors in the world of development and sys admins as well. It offers a set of tools and customization that almost every developer would need. Vim enables you to use key combinations to perform all the tasks.

However, Vim does not display line numbers by default. Line numbers are useful especially when working with a large file. It can allow you to jump and navigate to specific lines in a single command, commenting blocks of code, multi-line editing, and more.

Using this tutorial, you will learn how enable Vim to show line numbers using three modes: standard absolute numbering, relative, and hybrid numbering.

Let’s delve in into the world of Vim.

Enable Absolute Line Numbering in Vim

Absolute numbering is one of the most common line numbering format. This mode provides line numbering in the traditional chronological format. Each line number is displayed next to the start of the line.

To activate absolute line numbering in Vim, open the file to edit:

vim node1.py
  • Start by pressing the ESC key to enable Vim Command Mode.
  • Next, press the : followed by the command set number and press Return.

The command is as shown:

:set number

Or:

:set nu

Before:

image-20220930202159260

After:

image-20220930202237203

Vim will enable line numbers on the left side at the start of each line. Keep in mind that an empty line is treated a valid line.

To disable absolute line numbering, run the command:

:set nonumber

Or:

:set nonu

Or:

:set number!

Or:

:set nu!

Vim Enable Relative Line Numbering

In some cases, you may need to enable relative numbering mode in Vim. In relative mode, the current line is assigned the value 0 while other lines are assigned a value based on the distance from the current cursor position.

The format is as shown:

3

2

1

0 -- current line

1

2

3

To enable relative numbering, run the command:

:set relativenumber

Or:

:set rnu

Before:

image-20220930203613343

After:

image-20220930203725883

As you can see in the screenshot above, each line, before and after, the current line is assigned a value denoting the distance from the current cursor position.

To disable relative numbering, use the commands:

:set norelativenumber

Or:

:set nornu

Or:

:set nonumber

Vim Enable Hybrid Numbering Mode

In recent Vim versions, you can enable both hybrid numbering mode which combines both absolute and relative numbering system.

Hybrid mode works in a similar manner to relative numbering. However, instead of the current line being number 0, it shows the absolute (actual) line number.

To enable hybrid mode, run the command:

:set number relativenumber

Or:

:set number
:set relativenumber

This should enable absolute and relative numbering.

Before:

image-20220930204410190

After:

image-20220930204551366

As you can see from the output above, Vim, uses the absolute number in the current line.

To disable hybrid number, disable absolute and relative numbering:

:set nornu
:set nonumber

Vim Enable Numbering by Default

Instead of enabling line numbering when editing a given file, you can enable line numbering in the Vim configuration file. This will enable Vim line numbering by default =.

Edit the vim config file:

vim ~/.vimrc

Add the numbering mode to the config file:

:set number

Save and close the file.

Conclusion

In this post, we discussed how to enable and disable line numbering modes using various commands. Choose the numbering mode you find comfortable and stick with it.

We hope you enjoyed this tutorial. Leave us a comment down below and share.

Vim over Emacs 😁!!

Thanks for reading and see you in the next one!!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.