3 Easy Ways to Show Line Number in Vim

Written by

Reviewed by

Last updated: May 29, 2024

Expert verified

SVG Image

TL;DR

To show line number in Vim by default, follow these steps:

  1. Launch your terminal.
  2. Open your Vimrc file by typing vim ~/.vimrc and pressing Enter.
  3. Add the line set number to the Vimrc file.
  4. Save and exit by typing :wq and pressing Enter.

Continue reading the guide below to learn to show line numbers in Vim and common errors with possible solutions.

When you’re coding in Vim, having line numbers can make a big difference in navigating and editing your files. If you’re struggling to see line numbers, don’t worry—I’ve got you covered. In this post, I’ll show you practical solutions to display line numbers in Vim, enhancing your overall editing experience. You’ll learn how to set up line numbers permanently using your Vimrc file, enable them temporarily for quick tasks, and even use the vim-airline plugin for a visually appealing interface.

I’ll also explore different styles like absolute and relative line numbers, and I’ll share tips for advanced customization. By the end of this guide, you’ll know how to efficiently manage line numbers in Vim to fit your needs.

How to Show Line Number in Vim

To show line numbers in Vim, you can use several methods. To enable line numbers permanently, open your Vimrc file with vim ~/.vimrc, add set number, and save the file. For temporary line numbers, simply type :set number in command mode while editing a file.

For a more enhanced visual experience, you can install the vim-airline plugin by adding Plug 'vim-airline/vim-airline' to your Vimrc file, running :PlugInstall, and then restarting Vim. These methods will help you navigate and edit your files more efficiently in Vim.

That was the quick answer. Here is the detailed step-by-step guide for each method to Vim show line numbers:

1. Configuring Vimrc (Show Line Numbers Permanently)

If you prefer to have line numbers displayed by default whenever you open Vim, you can configure your Vimrc settings. This method ensures line numbers persist across multiple Vim sessions and provides a seamless experience for regular Vim users. To enable line numbers permanently, follow these steps:

  1. Launch your Terminal.
opening terminal 14
  1. Enter the following command to open your Vimrc file: 
vim ~/.vimrc
  1. The command will open vim configuration file.

opening-vim-configuration-file

  1. Add the following line to your vimrc file to enable line numbers:
set number
  1. Save and exit the Vimrc file by typing :wq and pressing Enter.

editing-configuration-file-to-view-line-numbers

  1. Now open the text file in vim by typing the following command:
vim filename.txt

Replace filename with the filename in which you made changes.

opening file in vim
  1. The output will be:
displaying line numbers by default

2. Enabling Line Numbers Temporarily

Temporarily enabling line numbers in Vim / Vi is useful when you only need them for the current editing session. This method doesn’t alter your configuration files, so the changes will not persist the next time you open Vim / Vi.

  1. Open a file in Vim / Vi. To enable line numbers for the current session, type:
:set number

Line numbers should now be visible at the beginning of each line for this session.

displaying line numbers in vim editor

3. vim-airline Plugin

vim-airline is a powerful status bar plugin for Vim that provides valuable information and enhancements to your editing environment. Among its features, vim-airline offers the ability to display line numbers in a visually appealing way. Here’s how to install and utilize vim-airline for line numbering:

  1. Open your vim configuration file.
  2. Add the following line to your configuration file to install vim-airline:
call plug#begin('~/.vim/plugged')
Plug 'vim-airline/vim-airline'
call plug#end()
  1. Save the configuration file and exit.
adding plugin to vim
  1. Restart Vim to apply the changes.
  2. Open Vim, and you will see the status bar at the bottom of the editor window with line numbers displayed.
viewing line numbers in vim using airline plugin

How to Show Line Numbers in Different Styles

1. Absolute Line Numbers

Absolute line numbers are used to display the exact line number for each line in a file. This method is particularly useful when you need to reference specific lines accurately, such as when debugging code, reviewing documents, or collaborating with others.

  1. Open a file in Vim / Vi by typing vim filename or vi filename and pressing Enter.
  2. If you are in insert mode (where you can type text), press Esc to return to command mode.
:set number

You should now see line numbers displayed at the beginning of each line.

displaying line numbers in vim editor 1

2. Activating Relative Line Numbers

Relative line numbers display the distance from the current line to other lines, rather than their absolute position. This makes navigation and editing in Vim / Vi more intuitive and efficient, especially for operations that involve moving around the text. To enable relative line numbers in Vim, follow these steps:

  1. Open the file in which you want to add the relative number.
opening file
  1. Enter the following command in Vim’s command mode: 
:set relativenumber
  1. Press Enter to activate relative line numbers.
show relative line numbers

3. Hybrid Line Numbers

Hybrid line numbers combine the benefits of both absolute and relative line numbering. In this mode, the current line is shown with an absolute number, while all other lines display their relative distance from the current line. This setup is advantageous for navigation and editing, making it easier to see your position in the file and quickly move around.

  1. Open Vim / Vi and press Esc to switch to command mode.
:set number relativenumber

You should see the current line with an absolute number and the other lines with relative numbers.

viewing hybrid line numbers of a file in vim

How to Customize the Number Column Width?

Adjusting the number column width helps you manage how much space the line numbers take up. This is useful if you have very long files and want to ensure the numbers fit well without overlapping your text.

  1. Run the following command to change the column width:
:set numberwidth=5

This command sets the number column width to 4 characters. You can change the number to whatever fits your needs.

increasing column width to 5
  1. If you set the width to 2 (:set numberwidth=2), you might see overlap if your file has more than 99 lines.
setting column width to 2

How to Hide Line Numbers in Certain Modes?

Sometimes, you might not want to see line numbers in all modes. For example, while writing or editing, line numbers might distract you, but you still want them for navigation.

  1. To hide the line numbers, execute the command:
:set nonumber

This command hides the line numbers.

hiding numbers of lines in vim editor

Common Errors When Showing Line Numbers in Vim

Showing line numbers in Vim can greatly improve your productivity, but it’s not without its challenges. Understanding these errors will help you troubleshoot and overcome any obstacles that may arise. Here are four common errors that you may encounter:

  • Line Numbers Not Visible: Sometimes, line numbers don’t appear in Vim. This can happen due to incorrect settings or plugin conflicts. Check your Vimrc file to ensure set number is enabled. Verify no plugins or settings are interfering. Restart Vim to apply any changes.
  • 🔄 Line Numbers Out of Sync: Line numbers might not align with actual lines, especially with hidden or folded lines. Use :set relativenumber to enable relative numbering, which adjusts dynamically. If the issue persists, disable conflicting plugins or settings.
  • 🧩 Line Numbers Overlapping Text: Overlapping line numbers and text can occur due to font or spacing issues. Experiment with different font sizes or types for better alignment. Adjust the number column width with :set numberwidth=4 in your Vimrc file to create enough space.
  • ❗️Line Numbers Misaligned with Cursor: Misalignment of line numbers and cursor position may result from certain plugins or conflicting settings. Temporarily disable recent plugins and check if the issue persists. Review your Vimrc file for any conflicting settings and adjust accordingly.

Vim Show Line Number: Summing Up

In this guide, I’ve explored various methods to Vim set line numbers, including enabling them permanently through the Vimrc file, temporarily via command mode, and using plugins like vim-airline for enhanced visual appeal. I also discussed displaying line numbers in different styles such as absolute, relative, and hybrid, and how to customize the number column width or hide line numbers in certain modes to suit your editing needs.

If you’re eager to learn more, I recommend checking out these articles:

  • For efficient navigation, explore using Vim Page Down and Up controls to help you scroll through long files quickly.
  • Find out ways to display hidden special characters in Vim, which can greatly improve your text formatting and debugging.
  • To ensure you never lose your work, master seven effective ways to save and exit files in Vim.

Frequently Asked Questions

How can I customize the appearance of line numbers in Vim?

Vim allows you to customize the appearance of line numbers through various settings and color schemes. To modify the appearance of line numbers, you can explore Vim’s configuration options. You can change the font, size, color, and position of the line numbers to suit your preferences. Vim also supports different color schemes, which can further enhance the visual presentation of line numbers. By experimenting with these settings and color schemes, you can create a personalized and visually appealing display of line numbers in Vim.

Can I display line numbers only for specific file types?

Yes, Vim allows you to configure line numbers on a per-file type basis. This means you can define specific settings in your Vimrc file to show line numbers in Vim only for certain file types. For example, you can enable line numbers for programming files like Python or JavaScript while hiding them for Markdown or plain text files. By leveraging Vim’s filetype detection mechanism and configuring the appropriate settings, you can have fine-grained control over when line numbers should be displayed, tailoring the experience to your specific needs.

Is there a way to toggle line numbers on and off dynamically in Vim?

Yes, Vim provides the flexibility to dynamically toggle line numbers on and off. You can do this by using the :set number command to enable line numbers or :set nonumber to disable them. These commands can be executed in Vim’s command mode. You can assign these commands to custom key mappings to enhance the convenience of toggling line numbers. By creating a custom mapping, you can easily switch between displaying line numbers for reference or turning them off for a distraction-free editing environment, based on your workflow and preferences.

Ojash

Author

Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

Akshat

Reviewer

Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

Share this article
Shareable URL
Prev Post

How to Check If a Port Is Open on a Remote Linux PC [ 5 Best Methods]

Next Post

Apt Install Specific Version of a Package: How to Do It? [9 Easy Steps]

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next