Vim is a text editor that is developed as an improved version of the vi editor. It can display text with extra format details, such as font color and underline. Vim is an essential tool in Linux. For example, you can use this tool to edit configuration files of web applications. This topic describes the modes and commonly used commands of Vim.

Background information

The following table describes the different modes of Vim.
Mode Description Mode switching method
Normal mode

In this mode, you can copy, paste, and delete characters or lines.
  • The tool enters the normal mode when you run the vim <File name> command to open a file.
  • To switch from other modes to this mode, press the Esc key.
Insert mode

In this mode, you can insert characters. To switch from the normal mode to this mode, enter any of the following characters: i, I, a, A, o, O.
Note You will see -- INSERT -- in the lower-left corner of the editor after you switch to this mode.
Replace mode

In this mode, you can replace characters. To switch from the normal mode to this mode, enter R.
Note You will see -- REPLACE -- in the lower-left corner of the editor after you switch to this mode.
Visual mode

In this mode, you can select a range of text. You must select a range of text before you run commands such as copy, replace, and delete on the selected text. To switch from the normal mode to this mode, enter v.
Note You will see -- VISUAL -- in the lower-left corner of the editor after you switch to this mode.
Command mode

In this mode, you can find and replace strings, display line numbers, save file changes, and exit the editor. To switch from the normal mode to this mode, enter :.
Vim supports three types of commands:

Insert

Basic commands:
  • i: inserts a character to the left of the current character.
  • I: inserts a character at the start of the current line.
  • a: inserts a character to the right of the current character.
  • A: inserts a character at the end of the current line.
  • o: inserts a new line below the current line.
  • O: inserts a new line above the current line.
Assume that you want to edit an example.conf file that contains the following content:
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
Include conf.modules.d/*.conf            
  • Example 1: Insert Location as the first line of the example.conf file. Perform the following operations:
    1. Run the vim example.conf command to open the file in normal mode.
    2. Enter i to switch to the insert mode.
    3. Enter Location.
    4. Press the Enter key to switch to a new line.
    5. Press the Esc key to exit the insert mode.
    6. Enter :wq to save the changes to the file and then exit the editor.
      After you perform the insertion, the example.conf file contains the following content:
      Location
      # To be able to use the functionality of a module which was built as a DSO you
      # have to place corresponding `LoadModule' lines at this location so the
      # directives contained in it are actually available _before_ they are used.
      # Statically compiled modules (those listed by `httpd -l') do not need
      # to be loaded here.
      #
      # Example:
      # LoadModule foo_module modules/mod_foo.so
      #
      Include conf.modules.d/*.conf            
  • Example 2: Insert # at the start of line 10 in the example. conf file. Perform the following operations:
    1. Run the vim example.conf command to open the file in normal mode.
    2. Enter :10 to move the cursor to line 10.
    3. Enter I to switch to the insert mode.
    4. Enter #.
    5. Press the Esc key to exit the insert mode.
    6. Enter :wq to save the changes to the file and then exit the editor.
      After you perform the insertion, the example.conf file contains the following content:
      # To be able to use the functionality of a module which was built as a DSO you
      # have to place corresponding `LoadModule' lines at this location so the
      # directives contained in it are actually available _before_ they are used.
      # Statically compiled modules (those listed by `httpd -l') do not need
      # to be loaded here.
      #
      # Example:
      # LoadModule foo_module modules/mod_foo.so
      #
      #Include conf.modules.d/*.conf        
  • Example 3: Insert LoadModule rewrite_module modules/mod_rewrite.so in the line below the Include conf.modules.d/*.conf line of the example.conf file. Perform the following operations:
    1. Run the vim example.conf command to open the file in normal mode.
    2. Run the /Include conf.modules.d/*.conf command to find the target line.
    3. Enter o to switch to the insert mode.
    4. Enter LoadModule rewrite_module modules/mod_rewrite.so.
    5. Press the Esc key to exit the insert mode.
    6. Enter :wq to save the changes to the file and then exit the editor.
      After you perform the insertion, the example.conf file contains the following content:
      # To be able to use the functionality of a module which was built as a DSO you
      # have to place corresponding `LoadModule' lines at this location so the
      # directives contained in it are actually available _before_ they are used.
      # Statically compiled modules (those listed by `httpd -l') do not need
      # to be loaded here.
      #
      # Example:
      # LoadModule foo_module modules/mod_foo.so
      #
      Include conf.modules.d/*.conf
      LoadModule rewrite_module modules/mod_rewrite.so                            

Replace

Basic commands:

R: replaces the highlighted characters, until you press the Esc key to exit the replace mode.

Assume that you want to edit an example.conf file that contains the following content:
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
AllowOverride None                

For example, to replace AllowOverride None with AllowOverride All in the example.conf file, perform the following operations:

  1. Run the vim example.conf command to open the file in normal mode.
  2. Run the /AllowOverride None command to find the target line.
  3. Move the cursor to the first letter of None.
  4. Enter R to switch to the replace mode.
  5. Enter All and a space.
    Note The word None has four characters, but the word All has three characters. To replace all four characters in None, enter an extra space after the three characters in All.
  6. Press the Esc key to exit the replace mode.
  7. Enter :wq to save the changes to the file and then exit the editor.
    After you perform the replacement, the example.conf file contains the following content:
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All                            

Delete

Basic commands:
  • x: deletes the highlighted character.
  • nx (n represents a number): deletes the highlighted character and the n-1 characters after it.
  • dd: deletes the line in which the cursor is located.
  • ndd (n represents a number): deletes the line in which the cursor is located and the n-1 lines below it.
Assume that you want to edit an example.conf file that contains the following contents:
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80                
  • Example 1: Delete # at the start of the #Listen 12.34.56.78:80 line of the example.conf file. Perform the following operations:
    1. Run the vim example.conf command to open the file in normal mode.
    2. Run the /#Listen 12.34.56.78:80 command to find the target line so that the cursor is on the # character.
    3. Enter x to delete #.
    4. Enter :wq to save the changes to the file and then exit the editor.
      After you perform the deletion, the example.conf file contains the following content:
      # Listen: Allows you to bind Apache to specific IP addresses and/or
      # ports, instead of the default. See also the <VirtualHost>
      # directive.
      #
      # Change this to Listen on specific IP addresses as shown below to
      # prevent Apache from glomming onto all bound IP addresses.
      #
      Listen 12.34.56.78:80  
      Listen 80                        
  • Example 2: Delete the #Listen 12.34.56.78:80 line and the line below it in the example.conf file. Perform the following operations:
    1. Run the vim example.conf command to open the file in normal mode.
    2. Run the /#Listen 12.34.56.78:80 command to find the target lines.
    3. Enter 2dd to delete the following contents.
      #Listen 12.34.56.78:80
      Listen 80
    4. Enter :wq to save the changes to the file and then exit the editor.
      After you perform the deletion, the example.conf file contains the following content:
      # Listen: Allows you to bind Apache to specific IP addresses and/or
      # ports, instead of the default. See also the <VirtualHost>
      # directive.
      #
      # Change this to Listen on specific IP addresses as shown below to
      # prevent Apache from glomming onto all bound IP addresses.
      #