Vim is a popular text editor in Linux, commonly used for tasks such as system operations and maintenance and writing shell scripts. This topic describes the basic commands and how to switch between modes in Vim to help you get started quickly.
Installation
The Vim tool is installed by default on most Linux systems. You do not need to install it. You can run vim --version in the terminal to view the Vim version information. This topic uses Vim 8.0 as an example. The commands and features may differ in other versions.
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Aug 10 2022 11:26:47)
Included patches: 1-1763
Modified by OpenAnolis Community
Compiled by OpenAnolis Community
Enormous version without GUI. Features included (+) or not (-):
+acl +farsi +mouse_sgr -tag_any_white
......
Switching modes
Run the vim filename command to open a file and enter Normal mode. If the file does not exist, Vim creates a new file. The following table describes the modes in Vim and how to switch between them.
|
Mode |
Function |
Mode switching |
|
Normal mode (Normal Mode) |
In this mode, you can copy, paste, and delete characters or lines. |
|
|
Insert mode (Insert Mode) |
In this mode, you can insert characters. |
In Normal mode, press any of the following keys to enter Insert mode: Note
After you enter Insert mode, |
|
Replace mode (Replace Mode) |
In this mode, you can replace characters. |
In Normal mode, press Note
After you enter Replace mode, |
|
Visual mode (Visual Mode) |
In this mode, you can select text. Commands, such as copy, replace, and delete, apply only to the selected text. |
In Normal mode, press Note
After you enter Visual mode, |
|
Command mode (Command Mode) |
In this mode, you can find strings, replace strings, show line numbers, save changes, and exit the editor. |
In Normal mode, press |
Basic commands
Opening files
-
vim filename: Opens a single file. You enter Normal mode. If the file does not exist, Vim creates a new file. -
vim filename1 filename2: Opens multiple files.-
By default, Vim opens `filename1`. After you edit the file, run
:wto save it. Then, run:bnto switch to the next file, `filename2`. After you edit `filename2`, run:wto save it. -
Run
:bpto switch to the previous file, `filename1`. -
Run
:lsto view the list of files that are being edited.
-
-
:open filename3: In Command mode, you can run this command to open a new file for editing. Before you run this command, run:wto save the current file.
Moving the cursor
-
In Normal mode
-
Up arrow/k: Move the cursor up.
-
Down arrow/j: Move the cursor down.
-
Left arrow/h: Move the cursor left.
-
Right arrow/l: Move the cursor right.
-
-
In Insert mode
-
You can only use the arrow keys to move the cursor.
-
Inserting content
In Normal mode, press any of the following keys to enter Insert mode: i,I,a,A,o,O.
-
i: Insert text to the left of the cursor. -
I: Insert text at the beginning of the current line. -
a: Insert text to the right of the cursor. -
A: Insert text at the end of the current line. -
o: Insert a new line below the current line. -
O: Insert a new line above the current line.
Copying and pasting
These commands are similar to Ctrl+C and Ctrl+V in a word processor. In Normal mode:
-
yy: Copies the current line. You can then usepto paste it. -
nyy: `n` is a number. For example,2yycopies the current line and the line below it, for a total of two lines. -
p: Pastes the copied content to the line below the cursor. -
P: Pastes the copied content to the line above the cursor.
Delete
-
In Normal mode
-
Delete a single character: Press the
xkey to delete the character at the cursor's position. -
Delete an entire line: Press
ddto delete the current line. This is similar to Ctrl+X in a word processor. You can then usepto paste the deleted line. -
To delete the current line and the line above it, press
dk. -
To delete the current line and the line below it, press
dj. -
dG: Deletes from the current line to the end of the document. -
nx: `n` is a number. Deletes the character at the cursor and the next `n-1` characters. -
ndd: `n` is a number. Deletes the current line and the `n-1` lines below it. This is similar to Ctrl+X in a word processor. You can then usepto paste the deleted lines.
-
-
In Insert mode
-
Use the
Deletekey to delete characters.
-
Searching
In Normal mode
-
/text: Searches for `text`. By default, the search is an exact match. Press Enter to highlight the matching characters.-
To perform a case-insensitive search, first run
:set ignorecase. To return to exact matching, run:set noignorecase.
-
-
n: Goes to the next match. -
N: Goes to the previous match.
Replacing
-
In Normal mode
-
r: Replaces the single character at the cursor. -
R: Enters Replace mode to continuously replace characters. Press theEsckey to exit. -
cc: Deletes the current line and enters Insert mode. -
:%s/oldtext/newtext/g: Finds all instances of `oldtext` and replaces them with `newtext`. The/gflag means that all instances on a line are replaced. Without this flag, only the first match on each line is replaced.
-
-
In Insert mode
-
Replace content by deleting the old content and inserting the new content.
-
Undoing and redoing
In Normal mode
-
u: Undoes an insertion or modification. This is similar toCtrl+Zin a word processor. -
U: Undoes all recent changes on the current line. -
Ctrl+r: Redoes the last undone change. This is equivalent toCtrl+Yin a word processor.
Indenting and formatting
-
In Normal mode
-
>>: Indents the current line to the right. The default indent is 8 spaces, which is the width of a tab character. -
<<: Indents the current line to the left.
-
-
In Command mode
-
:ce: Center-aligns the current line. -
:le: Left-aligns the current line. -
:ri: Right-aligns the current line.
-
Commenting code
-
Back up the file: Before you perform a large-scale replacement, back up the file or use the undo command (
u) to prevent mistakes. For example, runsudo cp /etc/text.txt /etc/text.txt.bakto back up a file. -
Regular expressions: Make sure you understand Vim's regular expression syntax to avoid accidentally deleting content. In Vim regular expressions, the
/character must be escaped as\/. If you are not familiar with regular expressions, you can use tools such as Regex101 to practice and test. -
Comment styles for different languages: Adjust the comment symbol in the replacement command based on the programming language you are using.
-
In Visual mode
-
Comment out multiple consecutive lines of code
-
Move the cursor up or down to select the lines to comment out.
-
Press
:to enter Command mode. Vim automatically enters:'<,'>, which indicates that the operation applies to the selected range. -
Enter the replacement command. For example, to add
#to the beginning of each line, enters/^/#/and pressEnter.
-
-
Comment out all code: Run the replacement command
:%s/^/#/gto comment out the entire file using#.
-
-
In Insert mode
Manually insert comment symbols to comment out code.
Saving and exiting
-
Save the file: In Normal mode, enter
:wand press Enter. -
Exit Vim: Enter
:qand press Enter. -
Save and exit: Enter
:wqor:xand press Enter, or use theZZcommand. -
Force exit without saving: Enter
:q!and press Enter. -
Force save and exit: Enter
:wq!and press Enter.
Encrypting documents
-
Encrypt a document:
vim -x filename. After you set a password, Vim enters Normal mode. You must save the file for the encryption to take effect. When you open the file again, you are prompted to enter the password to authenticate. -
Cancel document encryption.
-
In Vim, run the command
:set key=to cancel encryption. This command clears the encryption key for the current file, which removes the encryption setting. -
Use the
:wqcommand to save and exit. -
Run
vim filenameagain to open the file. You are no longer prompted for a password.
-
Executing commands
-
!pwd: Shows the current working directory without exiting Vim. -
!ls: Lists the files and folders in the current directory without exiting Vim.
Multi-window editing
-
vim -o filename1 filename2: Opens two files in separate windows at the same time. To exit, you must run the exit command in each window. -
:n: Switches to the next file window. -
:N: Switches to the previous file window.
Help documents
-
In the terminal, run
vim --helpto view help information about Vim command syntax. -
In Normal mode
-
:help: Views the Vim help document. The help document is read-only. Run:qto exit the help document. -
:help i: Shows the help document fori. -
:help yy: Shows the help document foryy. -
:set nu: Shows line numbers.
-
Examples
Modify a configuration file
This example shows how to insert Location on the first line of the example.conf configuration file. The steps are as follows:
-
Run the
vim example.confcommand to open the file and enter Normal mode. -
With the cursor on the first character of the file, press the
ikey to enter Insert mode. -
Enter
Locationand press Enter to start a new line. -
Press the
Esckey to return to Normal mode. -
Enter
:wqto save the file and exit.
Insert content on a target line
This example shows how to insert # at the beginning of line 10 in the example.conf configuration file. The steps are as follows:
-
Run the
vim example.confcommand to open the file and enter Normal mode. -
Run
:10to move the cursor to line 10. -
Press the
ikey to enter Insert mode. -
Enter
#. -
Press the
Esckey to return to Normal mode. -
Enter
:wqto save the file and exit.
Find and insert content
In the example.conf configuration file, insert LoadModule rewrite_module modules/mod_rewrite.so after the Include conf.modules.d/*.conf line. The steps are as follows:
-
Run the
vim example.confcommand to open the file and enter Normal mode. -
Run
/Include conf.modules.d/*.confto find the target line. -
Press
ito enter insert mode. -
Enter
LoadModule rewrite_module modules/mod_rewrite.so. -
Press the
Esckey to return to Normal mode. -
Run
:wqto save the file and exit.
Delete content
In the example.conf configuration file, delete the # from the beginning of the #Listen 12.34.XX:XX:80 line and delete the Listen 80 line. The steps are as follows:
-
Run the
vim example.confcommand to open the file and enter Normal mode. -
Run
/#Listen 12.34.XX:XX:80to find the target line. The cursor is positioned on the#character. -
Press the
xkey to delete#. -
Move the cursor to the Listen 80 line and press
ddto delete the line. -
Enter
:wqto save the file and exit.
Edit Docker.yaml
Create and edit the docker-compose.yaml file. The following is an example.

Remove multi-line comments
-
Assume the comment character
#is at the beginning of a line, and may be preceded by spaces and indents:# This is a comment # Another comment # Yet another comment -
Run the following command:
:%s/^\s*#\s\?//Description:
-
^: Matches the beginning of a line. -
\s*: Matches any number of whitespace characters (for handling indentation). -
#: The comment symbol. -
\s\?: Matches one optional space after the comment symbol. -
//: Replaces the matched content with nothing, which deletes it.
This command traverses the entire file. After the command is run, the file looks like this:
This is a comment Another comment Yet another comment -
Upgrade Vim
If the version of Vim on your operating system does not meet your needs, you can run the following commands to upgrade it.
Alibaba Cloud Linux 3 and 2
sudo yum update vim
CentOS 7 and 8
sudo yum update vim
Fedora
sudo yum update vim
Ubuntu and Debian
sudo apt upgrade vim
openSUSE
sudo zypper update vim
Common errors
-
The "No write since last change" prompt appears on exit:
-
Cause: The file was modified but not saved.
-
Solution: Run
:wqto save and exit, or run:q!to discard the changes and exit.
-
-
Cannot enter text:
-
Cause: You may be in a mode other than Insert mode, such as Visual mode.
-
Solution: Press the
Esckey to return to Normal mode, and then enter Insert mode.
-
-
Cannot save the file:
-
Cause: You may not have the required permissions. The current user does not have write permissions for the target file or its directory. For example, system configuration files, such as
/etc/hostsand/etc/nginx/nginx.conf, usually require superuser permissions to modify. -
Solution 1: Use the
sudocommand to start Vim with superuser permissions. For example,sudo vim example.conf. -
Solution 2: Use
:w !sudo tee %to save the current file with administrative permissions. To change the permissions or ownership of the configuration file, you can also use commands such assudo chownorsudo chmod.
-
-
A file with the ".swp" suffix exists:
-
The file is open in another Vim session.
-
Solution: Confirm that no other terminal is editing the file. If not, you can use
ll -ato find and delete the.swpfile, and then reopen the file for editing. Alternatively, you can use the:recovercommand to recover the file from the swap file before editing.
-