Vim is a powerful text editor that is highly customizable. One of the many things you can customize in Vim is the display of line numbers. In this guide, I will show you how to show line numbers in Vim and explain how you can display them.
How to Enable Line Numbers in Vim?
To enable line numbers in Vim, you will need to add a single line of code to your .vimrc file. The .vimrc file is a configuration file for Vim that allows you to customize various settings.
- Open your .vimrc file in Vim by typing the following command:
vim ~/.vimrc
- Press the
i
key to enter insert mode. - Add the following line of code to the file:
set number
- Press the
Esc
key to exit insert mode. - Save the file by typing
:wq
and pressingEnter
. - You should now see line numbers on the Vim editor’s left side.
How to Display Line Numbers in Different Ways?
There are several different ways you can display line numbers in Vim. The most common way is to display them on the left side of the editor, but you can also display them relative to the current line or as absolute line numbers.
- Displaying Line Numbers on the Left Side: This is the default way line numbers are displayed in Vim. As mentioned earlier, you can enable this by adding the
set number
the line to your .vimrc file.
- Displaying Line Numbers Relative to the Current Line: To display line numbers relative to the current line, you will need to add the following line of code to your .vimrc file:
set relativenumber
- Displaying Absolute Line Numbers: To display absolute line numbers, you will need to add the following line of code to your .vimrc file:
set number
You can change the color to whatever you prefer by changing the ctermfg value.
- Hiding line numbers: To hide line numbers, you can remove the
set number
line from your .vimrc file or type the following command:
:set nonumber
How to Display Line Numbers on the Left Side?
This is the default way line numbers are displayed in Vim. As mentioned earlier, you can enable this by adding the set number
line to your .vimrc file. But there are more options you can add to customize the way line numbers are displayed on the left side of the editor.
- Setting the width of line numbers: By default, the width of line numbers is set to the number of digits in the highest line number. This means that if your file has 100 lines, the width of the line numbers will be 3 (since 100 has 3 digits). However, you can set a specific width for the line numbers by adding the following line to your .vimrc file:
set numberwidth=4
This will set the width of the line numbers to 4, regardless of the number of lines in your file. You can set the width to any number you like.
- Changing the color of line numbers: By default, line numbers are the same as the color of the rest of the text. However, you can change the color of line numbers to make them stand out more. To change the color of line numbers, you will need to add the following line to your .vimrc file:
highlight LineNr cterm=bold ctermfg=DarkGrey
This will set the color of the line numbers to dark gray. You can change the color to whatever you prefer by changing the ctermfg value.
- Line numbers with specific font: By default, line numbers are shown with the same font as the rest of the text. However, you can also change the font of line numbers to make them stand out more. To change the font of line numbers, you will need to add the following line to your .vimrc file:
set guifont=Consolas:h12
This will set the font of the line numbers to Consolas with a size of 12. You can change the font and size to whatever you prefer.
How to Display Line Numbers Relative to the Current Line?
When you enable relative line numbers in Vim, the line numbers are displayed relative to the current line rather than as absolute line numbers. This can be helpful when navigating through a file, as it gives you a better sense of your location in the file.
To enable relative line numbers, you need to add the following line of code to your .vimrc file:
set relativenumber
This will display the line numbers relative to the current line.
- Setting the width of relative line numbers: Similar to the absolute line numbers, you can also set the width. The default width for relative line numbers is the number of digits in the highest relative line number. However, you can set a specific width for the relative line numbers by adding the following line to your .vimrc file:
set numberwidth=4
This will set the width of the relative line numbers to 4.
- Changing the color of relative line numbers: You can also change the color of relative line numbers to make them stand out more. To change the color of relative line numbers, you will need to add the following line to your .vimrc file:
highlight CursorLineNr cterm=bold ctermfg=DarkGrey
This will set the color of the relative line numbers to dark gray. You can change the color to whatever you prefer by changing the ctermfg value.
How to Display Absolute Line Numbers?
When you enable absolute line numbers in Vim, the line numbers are displayed as the actual line number in the file. This can be helpful when working with large files, and you need to reference specific line numbers.
To enable absolute line numbers, you need to add the following line of code to your .vimrc file:
set number
This will display the line numbers as absolute numbers.
- Setting the width of absolute line numbers: Similar to relative line numbers, you can also set the width of absolute line numbers. The default width for absolute line numbers is the number of digits in the highest line number. However, you can set a specific width for the absolute line numbers by adding the following line to your .vimrc file:
set numberwidth=4
This will set the width of the absolute line numbers to 4.
- Changing the color of absolute line numbers: You can also change their color to make them stand out more. To change the color of absolute line numbers, you will need to add the following line to your .vimrc file:
highlight LineNr cterm=bold ctermfg=DarkGrey
This will set the color of the absolute line numbers to dark gray. You can change the color to whatever you prefer by changing the ctermfg value.
Conclusion
In this guide, I have shown you how to enable and display line numbers in Vim. You can customize how line numbers are displayed in Vim by adding different lines of code to your .vimrc file. With the knowledge of how to customize line numbers in Vim, you can make your text editing experience more efficient and streamlined.