How To Show or Hide Line Numbers In vi / vim Text Editor

Tutorial details Difficulty Easy (rss) Root privileges No Requirements vim Time Less than a minute

ADVERTISEMENTS



Make the vi/vim text editor show or hide line numbers

How do I display vi / vim text editor line numbers while writing a shell script or c program? How can I turn on or off this feature on a Linux, Apple OS X/macOS, or Unix-like operating systems? How cah I make the vi text editor display or hide line numbers? How can I enable line numbers on vim startup by editing the ~/.vimrc config file?Displaying line numbers under vi / vim can be very useful for debugging program errors and to improve overall readability of a program or scripts. Vim is very popular among software developers, system administrators and Linux/Unix/macOS users but lines are not displayed by default. In this tutorial, you will learn how to display or hide line numbers in Vim / Vi text editor running on Linux/Unix based systems.

Vim can display line numbers in the left margin:

Press ESC key At the : prompt type the following command to run on line numbers: set number To turn off line numbering, type the following command at the : prompt set nonumber

Let us see all commands in detailed to display line number in vim.

Vi or Vim show line number command

To display line numbers along the left side of a window, type any one of the following command while using text editor. First press the Esc key. Press : and type any one of the following command at a : prompt:

set number

or

set nu

Sample outputs:



:set relativenumber

:set rnu

How to turn off line numbering in vim/vi

Want to display relative line numbers in vim or vi? Try:OR

To turn off (or hide) line number again enter the same command at the : prompt:

set nu!

OR

set nonumber

To turn off relative line numbers, try:

:set norelativenumber

OR

:set nornu

How to enable line number setting permanently while using vim

If you need number every time you start vi/vim, append the following line to your ~/.vimrc file:

$ vi ~/.vimrc

Append the following line:

set number

Save and close the file in vim.

How to open a file at particular location/line number

Jump to particular line number from a shell prompt, enter:

$ vi +linenumber file.c

$ vi +300 initlib.c

Jump to particular line that contains main() function from a shell prompt, enter:

$ vi +/searchTermHere file.c

$ vi +/main initlib.c

### note shell escape done with \ ###

$ vi +/addUser\( initlib.c

$ vi +/addUser\(arg1\) initlib.c

Minimal number of columns to use for the vim line number

Set following option:

:set numberwidth=N

:set numberwidth=1

:set numberwidth=4

:set numberwidth=10

The minimum value of N is 1, the maximum value N is 10. See this page for more info.

How do I list of all current vim settings?

At the : prompt, enter:

set all

To see a list of everything that you have set so far in your vim config file or runtime, type the following command at the : prompt:

set

Conclusion

This page explained how to show or hide line numbers in vim / vi text editors using commands. For more info see the official vim website here and the following pages: