How to display line numbers in vim using .vimrc file

last updated in Categories , ,

I am a new Linux or Unix system user. How do I make the vi or vim text editor to display line numbers permanently? How to configure vim text editor to show line numbers on the screen by default? How do I configure ~/.vimrc to show line numbers on Linux or Unix-like systems?

Introduction – You type :set number command within vi or vim text editor session when you are editing the file. However, it is possible to configure vim to display line numbers by default.

Display line numbers in vim using .vimrc

To force vi/vim display line numbers, you need to set the number flag. To do so, edit a file named ~/.vimrc. If you are using old good vi text editor edit a file named ~/.exrc:
vi ~/.vimrc
OR
vim ~/.vimrc
Press the i key to activate insert mode. Enter the following command:
set number
Save and close the file in vim.

Display line numbers on startup

Here is my sample file displayed using the cat command:
cat ~/.vimrc
Sample outputs:

call plug#begin('~/.vim/plugged')
Plug 'pearofducks/ansible-vim'
Plug 'sheerun/vim-polyglot'
Plug 'trevordmiller/nova-vim'
Plug 'ambv/black'
call plug#end()
colorscheme nova
set number

Please note that the minimum value of N is 1, the maximum value of N is 10.

Make the vim editor display line numbers by default

Just test it by editing any file:
vi ~/bin/script.sh
vim foo.c

Display line numbers in vim using .vimrc file
Configured my ~/.vimrc to show line numbers in vim

How to chage gutter column width for numbering in vim

The syntax is:
:set numberwidth=4
OR in ~/.vimrc
set numberwidth=4

How to configure text wrapping for the number column in vim

Again use the following syntax within vim:
:set cpoptions+=n
Add it to ~/.vimrc:
set cpoptions+=n

Conclusion

You learned how to display or print the line number in front of each line in vim/vi. When the ‘n’ option is excluded from ‘cpoptions’ a wrapped line will not use the column of line numbers. The ‘numberwidth‘ option can be used to set the room used for the line number. To read help, type:
:help number

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!