Show Line Numbers in Vim

Vim supports different kinds of line numbering. Here's how to enable them.
Warp Terminal

Want to show line numbers in Vim?

Well, there are 3 types of line numbering in Vim:

  • Absolute: Show line numbers starting from 1.
  • Relative: Show line numbers from 0.
  • Hybrid: Uses two columns and shows both absolute and relative numbers.

Here's how you can enable and disable them in Vim (make sure you are in normal mode before using them):

Type Activate Deactivate Description
Absolute :set nu :set nonu Show number starting from 1.
Relative :set rnu :set nornu Your current line will always be noted as the 0th line.
Hybrid :set nu rnu :set nonu nornu Shows the both relative and absolute numbers using two number columns.
πŸ’‘
You can make any of these permanent by editing the vimrc file.

Wanna know how? Here's how you do it.

Enable absolute line numbering in Vim

As I mentioned earlier, absolute line numbers will start from 0 and if you want to have that behavior, follow 2 simple steps to enable it:

  1. Press Esc key to switch to normal mode
  2. Press the colon key (:) and type set nu or set number and hit the Enter key

Confused? Here's how you do it:

enable absolute line numbering in vim

Pretty easy. Right?

But what if you want to disable it?

Well, for that, all you have to do is press the colon key : and type set nonu or set nonumber and it will disable the absolute number in Vim:

Disable absolute line numbering in Vim

Make absolute line numbering permanent

If you liked the absolute line numbering, you can make it permanent by making a few changes to the vimrc file. Let me show you how.

First, open the vimrc file using the following:

vi ~/.vimrc

Now, press the i key to get into the insert mode and type the following:

set number
make absolute line numbering permanent in Vim

Once done, press the colon key : and type wq to save and exit from the Vim editor.

From now on, if you open any file with the Vim, you will see absolute numbering by default.

Enable relative line numbering in Vim

When enabled, it will show the current line a 0th line which is quite helpful as you don't have to do mental math and you can go to a specific line pretty easily.

To enable relative line numbering, you have to follow two simple steps:

  • Press the Esc key to switch to normal mode.
  • Press the colon key : and type set rnu or set relativenumber and then hit the Enter key.

Let me show you how you perform those steps in actual life:

enable relative line numbering in Vim

For some reason, if you want to disable it, all you have to do is press the colon key : and type set nornu or set norelativenumber and it will disable relative numbering:

disable relative line numbering in Vim

Make relative line numbering permanent in Vim

If you want to use relative line numbering every time you use Vim, it is a good idea to make it permanent. For that, you have to make some changes in vimrc file.

First, open the vimrc file using the following command:

vi ~/.vimrc

Now, press the i key to switch to insert mode and enter the following lines in the file:

set relativenumber
Make relative line numbering permanent in Vim

To save changes and exit from Vim, press the colon key : and type wq.

From now on, you will see relative numbering every time you use Vim.

Enable hybrid line numbering in Vim

Hybrid line numbering is a blend of absolute and relative numbering and in my opinion, it gives you the best of both worlds.

To enable hybrid line numbering, you need to follow two simple steps:

  • Press Esc key to switch to the normal mode.
  • Press the colon key : and type set nu rnu or set number relativenumber and then press the Enter key.

Here's how you follow two steps:

Enable hybrid line numbering in Vim

For some reason, if you don't find it useful to your workflow, it can be disabled by simply pressing the colon key : and typing set nonu nornu or set nonumber norelativenumber and press the Enter key:

Disable hybrid line numbering in Vim

That was quick. Isn't it?

Make hybrid line numbering permanent in Vim

To make hybrid line numbering permanent in Vim, first, open the vimrc file using the following:

vi ~/.vimrc

Now, press the i button to switch to insert mode and enter the following:

set number relativenumber
Make hybrid line numbering permanent in Vim

To save changes and exit from Vim, press the colon key : and type wq , and hit Enter.

That's it!

New to Vim? Let me help

I know Vim overwhelms you with the plethora of keybindings and an entirely different workflow than the other text editors but once you get a grip on it, there's no going back.

I will recommend you start by learning what are the Vim modes:

What are Vim Modes? How to Change Them?
Vim has three different modes for handling user input and processing them.

Once done, learn some basic keybindings of Vim:

Basic Vim Commands Every Linux User Must Know [With PDF Cheat Sheet]
A comprehensive guide explaining basic vim commands that will be useful to any Linux user be it a sysadmin or a developer.

In the end, learn some tips and tricks for Vim:

8 Vim Tips And Tricks That Will Make You A Pro User
Brief: In this article, I’ll show you some of my favorites Vim tricks with practical examples. If you don’t use Vim, these tips might not give reasons for using Vim but if you use it already, you’ll definitely become a better Vim user. Even if I recently

Want even more Vim? Opt for this Vim course:

Mastering Vim Quickly

Master Vim like a true professional with this highly rated, premium Vim book and training course.

Chek it out

I hope you will find this guide helpful.

About the author
Sagar Sharma

Sagar Sharma

A software engineer who loves to write about his experience with Linux. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.

It's FOSS

Making You a Better Linux User

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

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

Success! Your billing info has been updated.

Your billing was not updated.