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
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, enter one of the following characters:
i, I, a, A, o, O .
Note
-- INSERT -- are shown in the lower-left corner of the editor after Vim enters the insert mode.
|
Replace mode
|
In this mode, you can replace characters. | To switch from the normal mode to this mode, enter R .
Note
-- REPLACE -- is shown in the lower-left corner of the editor after Vim enters the replace 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
-- VISUAL -- is shown in the lower-left corner of the editor after Vim enters the visual mode.
|
Command mode
|
In this mode, you can find and replace strings, have line numbers displayed, save file changes, and exit the editor. | To switch from the normal mode to this mode, enter : .
|
Insert
- 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.
# 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
at the first line of the example.conf file. Perform the following operations:- Run the
vim example.conf
command to open the file in normal mode. - Enter
i
to switch to the insert mode. - Enter
Location
. - Press the Enter key to switch to a new line.
- Press the
Esc
key to exit the insert mode. - Enter
:wq
to save the changes to the file and then exit the editor.After the specified content is inserted to 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
- Run the
- Example 2: Insert
#
at the start of line 10 in the example. conf file. Perform the following operations:- Run the
vim example.conf
command to open the file in normal mode. - Enter
:10
to move the cursor to line 10. - Enter
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 then exit the editor.After the specified content is inserted to 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
- Run the
- Example 3: 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 operations:- Run the
vim example.conf
command to open the file in normal mode. - Run the
/Include conf.modules.d/*.conf
command to find the line on which you want to perform the insert operation. - Enter
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 then exit the editor.After the specified content is inserted to 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
- Run the
Replace
Basic commands:
R: replaces the highlighted characters, until you press the Esc
key to exit the replace mode.
# 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 operations:
Delete
- 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.
# 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: Delete
#
at the start of the#Listen 12.34.XX.XX:80
line of the example.conf file. Perform the following operations:- Run the
vim example.conf
command to open the file 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 so that the cursor is on the#
character. - Enter
x
to delete#
. - Enter
:wq
to save the changes to the file and then 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
- Run the
- Example 2: Delete the
#Listen 12.34.XX.XX:80
line and the line below it in the example.conf file. Perform the following operations:- Run the
vim example.conf
command to open the file 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. - Enter
2dd
to delete the following contents.#Listen 12.34.XX.XX:80 Listen 80
- Enter
:wq
to save the changes to the file and then 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. #
- Run the