How to display line numbers in vim using .vimrc file

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

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


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one ↓


CategoryList of Unix and Linux commands
Disk space analyzersdf β€’ duf β€’ ncdu β€’ pydf
File Managementcat β€’ cp β€’ mkdir β€’ tree
FirewallAlpine Awall β€’ CentOS 8 β€’ OpenSUSE β€’ RHEL 8 β€’ Ubuntu 16.04 β€’ Ubuntu 18.04 β€’ Ubuntu 20.04
Modern utilitiesbat β€’ exa
Network UtilitiesNetHogs β€’ dig β€’ host β€’ ip β€’ nmap
OpenVPNCentOS 7 β€’ CentOS 8 β€’ Debian 10 β€’ Debian 8/9 β€’ Ubuntu 18.04 β€’ Ubuntu 20.04
Package Managerapk β€’ apt
Processes Managementbg β€’ chroot β€’ cron β€’ disown β€’ fg β€’ glances β€’ gtop β€’ jobs β€’ killall β€’ kill β€’ pidof β€’ pstree β€’ pwdx β€’ time β€’ vtop
Searchingag β€’ grep β€’ whereis β€’ which
User Informationgroups β€’ id β€’ lastcomm β€’ last β€’ lid/libuser-lid β€’ logname β€’ members β€’ users β€’ whoami β€’ who β€’ w
WireGuard VPNAlpine β€’ CentOS 8 β€’ Debian 10 β€’ Firewall β€’ Ubuntu 20.04

Comments on this entry are closed.

  • 🐧 Vivek Gite Jan 19, 2019 @ 19:36

    If you need further regarding vim text editor help please use our forum.

Comments are closed. Still have questions? Post it on our forum