Vim How to Show Line Numbers
Line numbers are an essential aspect of code editing in Vim. They allow programmers to navigate through code, debug scripts, and review code changes. In this article, we’ll explain how to show line numbers in Vim, including absolute, relative, and hybrid modes, and how to set them to be displayed by default in new Vim editors. We will also provide tips and best practices for working with line numbers in Vim.
If you’re new to Vim, it’s essential to understand the basics of Vim’s command mode and insert mode. In command mode, you can move the cursor around the screen, search for text, and perform other operations. In insert mode, you can type text into the document. To switch between these modes, press the Esc
key to enter command mode from insert mode, or press the i
key to enter insert mode from command mode. Now, let’s dive into how to show line numbers in Vim.
Absolute Line Numbering
According to Linuxize, absolute line numbers provide a reference for the current line and the position of the cursor in the document. Here’s how to show absolute line numbers in Vim:
- Open Vim and enter command mode by pressing the
Esc
key. - Type
:set number
and press theEnter
key. - The absolute line numbers will now be displayed on the left-hand side of the screen.
To disable absolute line numbers, repeat the steps above but replace :set number
with :set nonumber
.
Making Absolute Line Numbers Permanent
To make the absolute line number setting permanent in the Vim configuration file, follow these steps:
- Open the Vim configuration file by typing
vim ~/.vimrc
in the terminal and pressing theEnter
key. - Add the line
set number
to the configuration file. - Save and close the file by typing
:wq
and pressing theEnter
key.
Now, every time you open Vim, absolute line numbers will be displayed by default.
Relative Line Numbering
PhoenixNap explains that relative line numbers are useful when you want to jump between lines relative to the current line. Here’s how to show relative line numbers in Vim:
- Open Vim and enter command mode by pressing the
Esc
key. - Type
:set relativenumber
and press theEnter
key. - The relative line numbers will now be displayed on the left-hand side of the screen.
To disable relative line numbers, repeat the steps above but replace :set relativenumber
with :set norelativenumber
.
Making Relative Line Numbers Permanent
To make the relative line number setting permanent in the Vim configuration file, follow these steps:
- Open the Vim configuration file by typing
vim ~/.vimrc
in the terminal and pressing theEnter
key. - Add the line
set relativenumber
to the configuration file. - Save and close the file by typing
:wq
and pressing theEnter
key.
Now, every time you open Vim, relative line numbers will be displayed by default.
Hybrid Line Numbering
LINUX Handbook explains that hybrid line numbers display the current line as an absolute number and the other lines as relative numbers. Here’s how to show hybrid line numbers in Vim:
- Open Vim and enter command mode by pressing the
Esc
key. - Type
:set relativenumber
and press theEnter
key. - Type
:set number
and press theEnter
key. - The hybrid line numbers will now be displayed on the left-hand side of the screen.
To disable hybrid line numbers, repeat the steps above but replace :set number
with :set nonumber
.
Making Hybrid Line Numbers Permanent
To make the hybrid line number setting permanent in the Vim configuration file, follow these steps:
- Open the Vim configuration file by typing
vim ~/.vimrc
in the terminal and pressing theEnter
key. - Add the line
set number
andset relativenumber
to the configuration file. - Save and close the file by typing
:wq
and pressing theEnter
key.
Now, every time you open Vim, hybrid line numbers will be displayed by default.
Toggling Line Numbers On and Off
Warp.dev explains that you can toggle line numbers on and off in Vim easily. Here’s how to do it:
- Open Vim and enter command mode by pressing the
Esc
key. - Type
:set number
and press theEnter
key to enable line numbers. - Type
:set nonumber
and press theEnter
key to disable line numbers. - Alternatively, you can toggle line numbers on and off by typing
:set invnumber
and pressing theEnter
key. This will enable line numbers if they’re disabled, or disable them if they’re enabled.
Setting Line Numbers to Be Displayed by Default
To set line numbers to be displayed by default when you open a new Vim editor, follow these steps:
- Open the Vim configuration file by typing
vim ~/.vimrc
in the terminal and pressing theEnter
key. - Add the line
set number
to the configuration file. - Save and close the file by typing
:wq
and pressing theEnter
key.
Now, every time you open a new Vim editor, line numbers will be displayed by default.
Opening Vim with Line Numbers
LINUXize explains that when you open a file in Vim, it will not display line numbers by default. However, you can open Vim with the line numbers enabled by adding a command-line option to the vim
command.
Opening a File with Absolute Line Numbers
To open a file with line numbers enabled in Vim using absolute line numbering, simply run the following command:
vim +set number filename
Replace filename
with the name of the file you want to open.
Opening a File with Relative Line Numbers
To open a file with line numbers enabled in Vim using relative line numbering, simply run the following command:
vim +set relativenumber filename
Replace filename
with the name of the file you want to open.
Opening a File with Hybrid Line Numbers
To open a file with line numbers enabled in Vim using hybrid line numbering, simply run the following command:
vim +set number +set relativenumber filename
Replace filename
with the name of the file you want to open.
Now, every time you open a file with Vim using these commands, line numbers will be displayed by default.
Customizing Line Number Appearance
PhoenixNAP explains that you can customize the appearance of line numbers in Vim. Here’s how:
Changing the Color of Line Numbers
To change the color of line numbers in Vim, you can use the :highlight
command. For example, to change the color of line numbers to yellow, type the following command in Vim:
:highlight LineNr ctermfg=yellow
Changing the Background Color of Line Numbers
To change the background color of line numbers in Vim, you can use the :highlight
command with the ctermbg
option. For example, to change the background color of line numbers to blue, type the following command in Vim:
:highlight LineNr ctermfg=yellow ctermbg=blue
Changing the Width of Line Numbers
To change the width of line numbers in Vim, you can use the :set
command with the numberwidth
option. For example, to set the width of line numbers to 3, type the following command in Vim:
:set numberwidth=3
Changing the Format of Line Numbers
To change the format of line numbers in Vim, you can use the :set
command with the formatoptions
option. For example, to display line numbers with leading zeros, type the following command in Vim:
:set formatoptions+=2
Now, you can customize the appearance of line numbers in Vim to suit your preferences.
Opening Vim as the Correct User
Linux Handbook explains that when you open Vim, it will use the configuration file of the current user. However, if you open a file with Vim as a different user, it will use that user’s configuration file instead. This can cause discrepancies in the settings, including line numbers. Here’s how to open Vim as the correct user:
Using the sudoedit
Command
To open a file with Vim as the correct user, you can use the sudoedit
command. For example, to open the /etc/fstab
file as the root user, type the following command in the terminal:
sudoedit /etc/fstab
This will open the file in Vim as the root user, ensuring that the correct configuration file is used.
Using the sudo -E
Command
Alternatively, you can use the sudo -E
command to preserve the current user’s environment variables when opening a file with Vim as a different user. For example, to open the /etc/fstab
file as the root user while preserving the current user’s environment variables, type the following command in the terminal:
sudo -E vim /etc/fstab
This will open the file in Vim as the root user, using the correct configuration file for the current user.
Now, you can ensure that Vim is using the correct configuration file and displaying line numbers correctly by opening files as the correct user.
Wrapping Up
Congratulations! You now know how to show line numbers in Vim, customize their appearance, and open Vim as the correct user to ensure preferences are reflected.
Adding line numbers to your Vim editor is a great way to navigate code, debug scripts, and perform code reviews. By customizing the appearance of line numbers, you can make them even more useful and aesthetically pleasing.
Thank you for reading this article and we hope you found it helpful. Be sure to check out our other great content for more useful tips and tricks for Linux and Vim.
Answers To Common Questions
Who needs to show line numbers in Vim?
Developers, sysadmins, and anyone who works with code can benefit from showing line numbers in Vim.
What are the benefits of showing line numbers in Vim?
Line numbers can help navigate code, debug scripts, and perform code reviews more efficiently.
How do I show line numbers in Vim?
Use the :set number
command to show absolute line numbers or :set relativenumber
command to show relative line numbers.
How do I make line numbers appear by default in new Vim editors?
Use the :set number
or :set relativenumber
command in your Vim configuration file to make line numbers appear by default.
What is the difference between absolute and relative line numbers?
Absolute line numbers show the exact line number, while relative line numbers show the distance between the current line and the line with the cursor.
How do I customize the appearance of line numbers in Vim?
Use commands like :highlight
to change the color or width of line numbers and :set formatoptions
to change the format.