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. |
|
Insert mode
| In this mode, you can insert characters. | To switch from the normal mode to this mode, press Note After the Vim editor enters the insert mode, |
Replace mode
| In this mode, you can replace characters. | To switch from the normal mode to this mode, press Note After the Vim editor enters the replace mode, |
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 Note After the Vim editor enters the visual 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, press |
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:Run the
vim example.conf
command to open the file in Vim in normal mode.Press
i
to switch to the insert mode.Enter
Location
.Press the Enter key to move to the next line.
Press the
Esc
key to exit the insert mode.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:Run the
vim example.conf
command to open the file in Vim in normal mode.Enter
:10
to move the cursor to line 10.Press
I
to switch to the insert mode.Enter
#
.Press the
Esc
key to exit the insert mode.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 theInclude conf.modules.d/*.conf
line of the example.conf file, perform the following steps:Run the
vim example.conf
command to open the file in Vim in normal mode.Run the
/Include conf.modules.d/*.conf
command to find the line on which you want to perform the insert operation.Press
o
to switch to the insert mode.Enter
LoadModule rewrite_module modules/mod_rewrite.so
.Press the
Esc
key to exit the insert mode.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:
Run the
vim example.conf
command to open the file in Vim in normal mode.Run the
/AllowOverride None
command to find the line on which you want to perform the replace operation.Move the cursor to the first letter of
None
.Press
R
to switch to the replace mode.Enter
All
and a space.NoteThe word
None
consists of four characters, and the wordAll
consists of three characters. To replace all four characters in None, enter an extra space after the three characters in All.Press the
Esc
key to exit the replace mode.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:Run the vim example.conf command to open the file in Vim in normal mode.
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.Press
x
to delete#
.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:Run the
vim example.conf
command to open the file in Vim in normal mode.Run the
/#Listen 12.34.XX:XX:80
command to find the lines on which you want to perform the delete operation.Press
2dd
to delete the following content:#Listen 12.34.XX:XX:80 Listen 80
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. #