vim line numbers - how to have them on by default?
Categories:
Vim Line Numbers: How to Enable Them by Default

Learn how to permanently display line numbers in Vim, enhancing your editing workflow and navigation.
Line numbers are an invaluable feature for many developers and text editors, providing quick reference points for navigation, debugging, and collaborative editing. While Vim is a powerful and highly configurable editor, line numbers are not enabled by default. This article will guide you through the simple steps to enable line numbers in Vim, both temporarily and permanently, ensuring they are always there when you need them.
Understanding Vim's Configuration
Vim's behavior is controlled by its configuration files, primarily .vimrc
(or _vimrc
on Windows). This file is executed every time Vim starts, allowing you to customize settings, key mappings, and plugin configurations. To make changes persistent, you need to modify this file.
flowchart TD A[Vim Startup] --> B{Check for .vimrc} B -- Yes --> C[Execute .vimrc] B -- No --> D[Load Default Settings] C --> E[Vim Ready with Custom Settings] D --> E
Vim Configuration Loading Process
Enabling Line Numbers Temporarily
Before making permanent changes, it's useful to know how to toggle line numbers on and off within a live Vim session. This is done using Vim's command mode.
:set number
:set nonumber
Toggle line numbers in Vim command mode
The :set number
command will display absolute line numbers, while :set nonumber
will hide them. Vim also offers 'relative' line numbers, which can be incredibly useful for navigation, especially when combined with movement commands like j
and k
.
:set relativenumber
:set norelativenumber
Toggle relative line numbers in Vim command mode
number
and relativenumber
. This will show the current line's absolute number and relative numbers for all other lines.Making Line Numbers Permanent
To ensure line numbers are always visible when you open Vim, you need to add the appropriate set
command to your .vimrc
file. If you don't have a .vimrc
file, you'll need to create one.
1. Locate or Create .vimrc
Open your terminal or command prompt. Your .vimrc
file is typically located in your home directory (~/.vimrc
on Linux/macOS, or C:\Users\YourUsername\_vimrc
on Windows). If it doesn't exist, create it using touch ~/.vimrc
(Linux/macOS) or by simply creating a new file named _vimrc
in your user directory on Windows.
2. Edit .vimrc
Open the .vimrc
file with Vim itself or any other text editor. For example: vim ~/.vimrc
.
3. Add the Line Number Command
Add one of the following lines to your .vimrc
file, depending on your preference:
4. Save and Exit
Save the changes to your .vimrc
file and exit Vim (:wq
). The next time you open Vim, line numbers will be enabled by default.
" For absolute line numbers:
set number
" For relative line numbers:
set relativenumber
" For both (current line absolute, others relative):
set number
set relativenumber
Examples of line number configurations for .vimrc
~/.config/nvim/init.vim
.Advanced Line Number Configuration
For those who prefer relative line numbers for navigation but want absolute line numbers when inserting text, Vim offers a clever trick using autocommands. This allows you to dynamically switch between relativenumber
and number
based on your current mode.
set number
set relativenumber
" Toggle relative numbers off in insert mode, on in normal mode
autocmd InsertEnter * :set norelativenumber
autocmd InsertLeave * :set relativenumber
Dynamic line number switching in .vimrc
This configuration ensures that when you enter insert mode, only absolute line numbers are shown, providing a stable reference. Upon exiting insert mode, relative line numbers return, aiding in efficient movement.