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

last updated in Categories , , , , , , , ,

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.

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.

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

  1. 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

    Have a question? Post it on our forum!