All Products
Search
Document Center

Elastic Compute Service:Use the Vim editor

Last Updated:Mar 27, 2024

The Vim editor is a text editor that is developed as an optimized version of the vi editor. The Vim editor can display text that has additional format details, such as font color and underline. The Vim editor is an essential tool in Linux and can be used to edit configuration files of web applications. This topic describes the basic Vim commands and the common operations that you can perform on the Vim editor.

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.

  • When you run the vim <File name> command to open a file, the Vim editor enters the normal mode.

  • To switch from a different mode 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, press i, I, a, A, o, or O.

Note

After the Vim editor enters the insert mode, -- INSERT -- is shown in the lower-left corner of the editor.

Replace mode

In this mode, you can replace characters.

To switch from the normal mode to this mode, press R.

Note

After the Vim editor enters the replace mode, -- REPLACE -- is shown in the lower-left corner of the editor.

Visual mode

In this mode, you can select a block of text. Before you run commands such as copy, replace, and delete commands on text, you must select a block of text.

To switch from the normal mode to this mode, press v.

Note

After the Vim editor enters the visual mode, -- VISUAL -- is shown in the lower-left corner of the editor.

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, press :.

Vim supports the following types of commands:

Insert

Basic commands:

  • i: inserts to the left of the current character.

  • I: inserts at the beginning of the current line.

  • a: inserts to the right of the current character.

  • A: inserts 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.

For example, you want to edit the 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: To insert Location as the first line of the example.conf file, perform the following steps:

    1. Run the vim example.conf command to open the file in Vim in normal mode.

    2. Press i to switch to the insert mode.

    3. Enter Location.

    4. Press the Enter key to move to the next line.

    5. Press the Esc key to exit the insert mode.

    6. Enter :wq to save the changes to the file and exit the editor.

      After the specified content is inserted into the example.conf file, the 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: To insert # at the beginning of line 10 in the example. conf file, perform the following steps:

    1. Run the vim example.conf command to open the file in Vim in normal mode.

    2. Enter :10 to move the cursor to line 10.

    3. Press 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 exit the editor.

      After the specified content is inserted into the example.conf file, the 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: To 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 steps:

    1. Run the vim example.conf command to open the file in Vim in normal mode.

    2. Run the /Include conf.modules.d/*.conf command to find the line on which you want to perform the insert operation.

    3. Press 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 exit the editor.

      After the specified content is inserted into the example.conf file, the 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.

For example, you want to edit the 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                

Example: To replace AllowOverride None with AllowOverride All in the example.conf file, perform the following steps:

  1. Run the vim example.conf command to open the file in Vim in normal mode.

  2. Run the /AllowOverride None command to find the line on which you want to perform the replace operation.

  3. Move the cursor to the first letter of None.

  4. Press R to switch to the replace mode.

  5. Enter All and a space.

    Note

    The word None consists of four characters, and the word All consists of 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 exit the editor.

    After the specified characters are replaced, 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 the character.

  • 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 the line.

For example, you want to edit the example.conf file that 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.XX:XX:80
Listen 80                
  • Example 1: To delete # at the beginning of the #Listen 12.34.XX:XX:80 line of the example.conf file, perform the following steps:

    1. Run the vim example.conf command to open the file in Vim in normal mode.

    2. Run the /#Listen 12.34.XX:XX:80 command to find the line on which you want to perform the delete operation. This way, the cursor is on the # character.

    3. Press x to delete #.

    4. Enter :wq to save the changes to the file and exit the editor.

      After the specified content is deleted from the example.conf file, the 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.XX:XX:80  
      Listen 80                        
  • Example 2: To delete the #Listen 12.34.XX:XX:80 line and the line below it from the example.conf file, perform the following steps:

    1. Run the vim example.conf command to open the file in Vim in normal mode.

    2. Run the /#Listen 12.34.XX:XX:80 command to find the lines on which you want to perform the delete operation.

    3. Press 2dd to delete the following content:

      #Listen 12.34.XX:XX:80
      Listen 80
    4. Enter :wq to save the changes to the file and exit the editor.

      After the specified content is deleted from the example.conf file, the 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.
      #