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

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?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements vim
Time Less than a minute
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.

ADVERTISEMENTS

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

Vim can display line numbers in the left margin:

  1. Press ESC key
  2. At the : prompt type the following command to run on line numbers: set number
  3. 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:

Show or Hide Line Numbers in Vim/Vi

Fig.01: vi/vim in action with line numbers

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

How to turn off line numbering in vim/vi

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:

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 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 cron 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
42 comments… add one
  • yoander May 12, 2008 @ 14:27

    Inside vim editor in command mode you can go directly to specific line pressing line number (ex: 50) and shift g.

  • billyduc May 13, 2008 @ 10:15

    THat’s really kool

  • Planet Lowyat May 17, 2008 @ 16:20

    Can VI display 3 desire lines such as line x(10), line y(25) and line z(50) only?

  • Dean Nguyen Jan 16, 2009 @ 16:16

    Hi,

    The “:set number or :set nu” is working great in VI.

    Thank you!

    Dean

  • neeraj sinha Jun 7, 2009 @ 9:41

    Thanks you so much!
    it works!

  • hilmy Oct 11, 2009 @ 1:44

    its working.

    Thanks!
    hilmy

  • woof Oct 21, 2009 @ 21:44

    Inside vim editor in command mode you can go directly to specific line pressing line number (ex: 50) and shift g.

    Or colon, line number, and Enter (e.g. :50 [enter]). It does the same thing, but is my personal preference over the 50G option.

  • Phil Nov 6, 2009 @ 20:30

    Does not work in gvim with vim6.3 in centOS.

    • vlayne Jan 12, 2013 @ 18:32

      Works with vi & vim in Centos 6.3

      noarch:printing-4.0-amd64:printing-4.0-noarch
      Distributor ID: CentOS
      Description: CentOS release 6.3 (Final)
      Release: 6.3
      Codename: Final

  • Tapas May 8, 2010 @ 19:04

    I use Debian you need to create .vimrc in your home directory for that.
    Also syntax on will help

  • myrabelle cabriana Mar 8, 2011 @ 5:02

    thank uuuuuuuuuuuuu.hehe

  • Venu Mar 9, 2011 @ 6:54

    Previous post not properly added

    Hi,
    vi “file name” +
    vi + will open the file and cursor will be at specified line.

    If the format is
    vi “file name” :

    vi : – How can I get the same behavior.

    thnx
    -Venu

  • rojah May 24, 2011 @ 20:50

    exactly what i was looking for. thank you.

  • sachin Jun 1, 2011 @ 10:41

    Thanks

  • toto Aug 2, 2011 @ 6:33

    this is great tutorial. I can see line number in my vim editor. thanks

  • senthil Aug 18, 2011 @ 9:25

    its working .thx

  • Ramesh Sep 13, 2011 @ 17:48

    thanks

  • Sandip Sep 15, 2011 @ 10:30

    You can also add:
    set number
    ~/.vimrc

  • toto Mar 20, 2012 @ 16:50

    thank you for this info. My vim is look more beautiful :)

  • Malek Mar 21, 2012 @ 12:32

    this is why I like vim

  • w May 30, 2012 @ 8:50

    Thanks!

  • Mike W Jun 8, 2012 @ 19:36

    This works in Poderosa too. Anyone know how to just turn on a display at the bottom of the screen that shows which line you’re on, as an alternative to having all the rows numbered? Poderosa does this on my Dreamhost account but not on another site I use, don’t know why.

  • Melanie Jul 16, 2012 @ 18:52

    I have been to your site before and was back again searching for displaying the line numbers. Thank you for this quick bit of help.

  • Scarlet Jul 18, 2012 @ 3:26

    it’s helpful for beginners,thx

  • sh Oct 30, 2012 @ 7:25

    Thanks

  • Debabrata Banik Nov 14, 2012 @ 6:01

    Thanks, Its working.

  • rajagopal Nov 26, 2012 @ 13:13

    How can i get current cursor number in vi editor???

    • vlayne Jan 12, 2013 @ 18:35

      launch vi –> vi ENTER
      type –> :set number ENTER
      Now as you press down or up, you will see the number value change on the bottom right.

    • jrrs Apr 4, 2014 @ 22:58

      :set ruler

  • newbee Feb 22, 2013 @ 4:10

    .vimrc not available at ~/
    now where should I add “set number”

  • sengseng Feb 23, 2013 @ 21:35

    @newbee: Are you using a Debian-based system? If so, .vimrc is not included in your home dir. You can create the file by doing this at your command prompt:

    $ vi ~/.vimrc

    You’ll then be inside the vi editor. Press “i” for insert mode and type “set number” into the first line of the editor.

    Once you are done, press ESC, then type ZZ and press Enter. You’ll automatically write the file and exit to your command prompt. You’ll now see .vimrc in your home dir.

    You should now be able to see line numbers when you vi a file.

    Last edited by Admin; 24th Feb 2013 at 9:59 AM. Reason: Correction.

  • soreana Apr 7, 2013 @ 14:45

    You solve my problem in shortest waythat possible.
    thank you

  • preet May 23, 2013 @ 9:06

    tnks really useful ……….

  • Van Jun 9, 2013 @ 7:27

    Thanks for the tips, it works for me

  • mark Jun 11, 2013 @ 21:40

    Every time I enter to my vim file I have to put “:syntax on” to show the colors. Is there I way I can set it up as default like “set numers”, (in fact I’m still trying to undertand how to make it with the numbers). Thanks.

    • 🐧 nixCraft Jun 12, 2013 @ 10:06

      Edit ~/.vimrc file and append the following:

      set number
       syntax on
       set ruler

      Save and close the file. It should now work.

      • mark Jun 13, 2013 @ 14:51

        Thank you!! I done it! When I put ./vimrc it show a message of permission denied so I try with sudo, but doesn’t work either. So I created new one: “vim ~/.vimrc” and I put the commands in the file!

        set nu
        syntax on
        

        And it works!
        Thanks!!

  • RB Jun 18, 2014 @ 9:12

    Beautiful. I was missing that (just started using Vim :3). The lines and columns are displayed at the bottom right but this is much nice and similar to all decent code editors.

  • Aldi Firmansyah Jan 27, 2015 @ 7:34

    it’s working very nice. thanks

  • ashmantak Feb 17, 2015 @ 5:57

    Thanks, it works.

  • ramya Mar 27, 2015 @ 13:24

    I want to indent and set numbers for each and every program by default. Is there anyway to do this

  • Lionel Sep 12, 2016 @ 19:18

    You should take out the : in front of your command line it is a little confusing. Otherwise, excellent tutorial. God bless

Leave a Reply

Your email address will not be published.

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