How to Show Line Numbers in vi or vim Editor

In the vi or vim editor, you might find it useful to display line numbers alongside the text in the buffer. This can help you navigate large files more easily and quickly locate specific lines of code or text.

In this short guide, we’ll show you how to enable line numbers in vi or vim.

Step 1: Open a File in vi or vim

First, open a file in the vi or vim editor by running the following command:

vi filename.txt

Replace filename.txt with the name of the file you want to edit. If the file does not exist, the editor will create a new one with the specified name.

For example, open httpd.conf:

[root@server ~]# 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.

Step 2: Enter Command Mode

To enable line numbers in vi or vim, you need to enter command mode. By default, the editor opens in normal mode. To switch to command mode, press the : (colon) key.

See also  How to Install Cockpit on Linux CentOS?

Step 3: Enable Line Numbers

With the command line prompt (the colon) displayed at the bottom of the editor, enter the following command:

set number

or

set nu

Press Enter to execute the command. The editor will now display line numbers along the left side of the buffer.

      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

Step 4: Disable Line Numbers (Optional)

If you want to disable line numbers, enter command mode again by pressing the : (colon) key, and then enter the following command:

set nonumber

or

set nonu

Press Enter to execute the command. The line numbers will be removed from the buffer.

See also  How to Check and Patch Meltdown CPU Vulnerability in Linux?

Commands and Their Functions:

  • vi – Opens a file in the vi or vim text editor
  • :set number – Enables line numbers in vi or vim
  • :set nonumber – Disables line numbers in vi or vim

The Outcome

By following these steps, you have successfully enabled line numbers in the vi or vim editor using the :set number command. You can now easily navigate and locate specific lines in your text files. To disable line numbers, use the :set nonumber command.

See also  How to Install tcpdump

We hope this guide has been helpful in showing you how to enable line numbers in vi or vim. If you have any suggestions or improvements, please feel free to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *