How do I show lines in vi or vim text editor?

I am using a Linux based system. In, vi or vim text editor, how do I show or hide line numbers? How do I show lines in vi?

Introduction: One can make the vi or vim text editor display or hide line numbers using set number command. This page shows how to make the vi editor show or hide line numbers when using under Linux or Unix-like systems.

ADVERTISEMENTS

How to show lines in vi or vim text editor

To force vim or vi text editor display line numbers

  1. Press the ESC key
  2. Type : (the colon)
  3. Execute the following command to show lines in vi/vim:
    set number
  4. Now you can see line numbers on the left side of the vi/vim text editor screen
Show lines in vi or vim text editor

How to hide lines in vi or vim text editor

  1. Press the ESC key
  2. Type : (the colon)
  3. Execute the following command to hide lines in vi/vim:
    set nonumber

How do I make line number vi / vim editor settings permanent?

Usually, your vi or vim editor forget the setting as soon as you left the editor. You can make the setting take effect automatically whenever you use vi. To do this, edit a file named ~/.exrc for vi text editor and ~/.vimrc for vim text editor.

How to show line numbers in the vim editor automatically

You need to edit your personal Vim/vi text editor initializations and config file. The file located at $HOME/.vimrc or ~/.vimrc. To change default settings of Vim/vi editor such as setting up line number, run:
vi ~/.vimrc
OR
vi $HOME/.vimrc
Append the following line:

set number

However, one can only display line number on files with py (Python) extension:

autocmd BufReadPost *.py set number

Save and close the file.

Conclusion

You learned how to set, hide/display line number in VI or Vim text editor running on a Linux, macOS or Unix-like operating systems.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • JJJJ Mar 13, 2014 @ 11:50

    Yes, to see the line number in vi I’m typing like below:
    :set nu
    To hide it:
    :set nonu
    I would add the appropriate set statement in a file in my home directory named .exrc aka ~/.exrc

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.