How to Set Line Numbers in Vim Permanently

How to Set Line Numbers in Vim Permanently

Permanently Show Line Numbers in Vim with This Simple Trick

Play this article

Introduction

In my earliest article on vi/vim, where I wrote extensively on what vi/vim is and how to make use of vi/vim, if you are new to using vi/vim, you can check the article here.

What are Line Numbers in Vim?

Just like any other editor, the Vim editor also has line numbers, the line numbers are simply numbers apportioned to each line starting from the first line carrying the number 1 and as you keep typing and moving to the next line, it keeps increasing showing the line count of the present line you are on.

How to Show Line Numbers in Vim Temporarily

In that same article, about Vim, I wrote about how one can simply set the line number in Vim temporarily, that is once you exit the editor, the line number will disappear, entering the editor, you will have to enter the command again to bring the line number back.

To set the line number in Vim temporarily, simply use the :set number command while in the editor's command mode.

How to Set Line Numbers in Vim Permanently

This is the main focus of this article, to do this, we will need to either create if the file is not available or make an edit if the file is already existing.

We will need to create or edit a file named .vimrc that is in the home directory. To do that, we will be using the vim editor;

vim ~/.vimrc

Once you run the command, it will open the file in the Vim editor, next is to input this in the file, make sure you put it in an entirely new line, that is it should be in a single line, and not together with another thing.

set number

Once you have entered this on a line, save the changes made and exit the editor, now go ahead and try your Vim editor, this time around you will observe that it now opens with line numbers.

Benefits of Using Line Numbers in Vim

There are quite some benefits of using line numbers in Vim, some of them are:

  1. It helps you know how many lines of code or content you have in a file.

  2. It helps you to know the particular number you are having problems with especially if you are debugging codes like C and C++. As the compiler will likely show you the problem as well as the line number, so entering the editor with the line numbers present, you can easily go to the line number and make necessary adjustments to your line of code.

  3. It can help you know which line number you presently are, in a situation you want to either copy, cut or delete multiple lines from the position you have.

How to Disable Line Numbers in Vim

To disable the line numbers in Vim, you can simply use the :set nonumber command or the :set number! command while in the command mode of the editor.

To disable the permanent set number, simply go back to the .vimrc file and remove the line set number.

Conclusion

This is how you can easily set line numbers in the Vim editor, you can also check for other settings that can be applied to the .vimrc file, to make your Vim editor more robust.

Thank you for reading. My name is Gideon Bature, a software engineering student with ALX Software Engineering at the point of writing this article, I will like to connect with you via Twitter and LinkedIn as I share useful tips in my Software Engineering Journey.