Vim show line numbers by default on Linux

ADVERTISEMENTS



Vim show line numbers by default

How do I show line numbers in Vim by default on Linux?Vim (Vi IMproved) is not just free text editor, but it is the number one editor for Linux sysadmin and software development work. By default, Vim doesn’t show line numbers on Linux and Unix-like systems, however, we can turn it on using the following instructions. My experience shows that line numbers are useful for debugging shell scripts, program code, and configuration files. Let us see how to display the line number in vim permanently.

Turn on absolute line numbering by default in vim:

Open vim configuration file ~/.vimrc by typing the following command:

vim ~/.vimrc Append set number

Press the Esc key To save the config file, type :w and hit Enter key You can temporarily disable the absolute line numbers within vim session, type: :set nonumber Want to enable disabled the absolute line numbers within vim session? Try:

:set number We can see vim line numbers on the left side.

Relative line numbers

Apart from regular absolute line numbers, Vim supports relative and hybrid line numbers too to help navigate around text files. The ‘relativenumber’ vim option displays the line number relative to the line with the cursor in front of each line. Relative line numbers help you use the count you can precede some vertical motion commands with, without having to calculate it yourself. Once again edit the ~/vimrc, run:

vim ~/vimrc

Finally, turn relative line numbers on:

set relativenumber

Save and close the file in vim text editor.



How to show “Hybrid” line numbers in Vim by default

What happens when you put the following two config directives in ~/.vimrc ?

set number

set relativenumber

That is right. We can enable both absolute and relative line numbers at the same time to get “Hybrid” line numbers.

Conclusion

Today we learned about permanent line number settings for the vim text editor. By adding the “set number” config directive in Vim configuration file named ~/.vimrc, we forced vim to show line numbers each time vim started. See vim docs here for more info and following tutorials too: