How to Show Line Number in vi or vim Editor

Share this Article :

A VI or VIM editor is a simple editor used by system administrators or programmers in Linux and Unix server environment. vi or vim is actually the same thing as is has same usage and same purpose. Vim is a text editor that is upwards compatible to Vi. Both can be used to edit all kinds of plain text and programs. By default, Vi and vim does not show the line number and this is very inefficient. However, there is the step to show line number in vi or vim editor. Follow this example:

1. Select any file to view in vi editor. For example httpd.conf

    [[email protected] ~]# vi /etc/httpd/conf/httpd.conf
    
    #
    # This is the main Apache server configuration file.  It contains the
    # configuration directives that give the server its instructions.
    # See  for detailed information.
    # In particular, see
    #
    # for a discussion of each configuration directive.
    #
    #
    # Do NOT simply read the instructions in here without understanding
    # what they do.  They're here only as hints or reminders.  If you are unsure
    # consult the online docs. You have been warned.
    #
    # The configuration directives are grouped into three basic sections:
    #  1. Directives that control the operation of the Apache server process as a
    #     whole (the 'global environment').
    #  2. Directives that define the parameters of the 'main' or 'default' server,
    #     which responds to requests that aren't handled by a virtual host.
    #     These directives also provide default values for the settings
    #     of all virtual hosts.
    #  3. Settings for virtual hosts, which allow Web requests to be sent to
    #     different IP addresses or hostnames and have them handled by the
    #     same Apache server process.
    

2. Once you are in vi or vim editor, makesure you press ESC.
3. In vi or vim editor type the command below:-

    :set number
    

or

    :set nu
    

Once entered, you will see the line number showing at the left side.

          1 #
          2 # This is the main Apache server configuration file.  It contains the
          3 # configuration directives that give the server its instructions.
          4 # See  for detailed information.
          5 # In particular, see
          6 #
          7 # for a discussion of each configuration directive.
          8 #
          9 #
         10 # Do NOT simply read the instructions in here without understanding
         11 # what they do.  They're here only as hints or reminders.  If you are unsure
         12 # consult the online docs. You have been warned.
         13 #
         14 # The configuration directives are grouped into three basic sections:
         15 #  1. Directives that control the operation of the Apache server process as a
         16 #     whole (the 'global environment').
         17 #  2. Directives that define the parameters of the 'main' or 'default' server        ,
         18 #     which responds to requests that aren't handled by a virtual host.
         19 #     These directives also provide default values for the settings
         20 #     of all virtual hosts.
         21 #  3. Settings for virtual hosts, which allow Web requests to be sent to
         22 #     different IP addresses or hostnames and have them handled by the
    

4. If you want to hide the line number, you can use the command below:-

    :set nonumber
    

or

    :set nonu
    


Leave a Reply