Introduction
During the use of Linux, we often need to edit configuration files, write scripts, or modify log files. The most commonly used, classic, and also the most “love-hate” tool for beginners is the Vim editor.
Vim is one of the most important text editors in the Linux world. It is lightweight, powerful, and almost universally available across all systems. In this chapter, we will systematically introduce it from three aspects:
- Basic concepts of text editors
- The three modes of Vim
- Common commands and operations in Vim
1. Basic Concepts of Text Editors
In Linux, the vast majority of configuration files and code are plain text files. Therefore, mastering an efficient text editor is almost an essential skill.
Common text editors in the Linux environment include:
| Editor | Features |
|---|---|
<span>nano</span> |
Simple and easy to use, suitable for beginners (few commands, limited functionality) |
<span>vi</span> |
Early Unix built-in editor, lightweight but not easy to master |
<span>vim</span> |
“Vi IMproved” – an enhanced version of vi, powerful, supports syntax highlighting, plugins, and multi-mode editing |
<span>gedit</span> |
Graphical editor, similar to Notepad |
<span>emacs</span> |
Highly customizable editor, extremely powerful, but with a steep learning curve |
Tip: Vim has become the “default editor” in Linux server environments. When you execute commands like
<span>crontab -e</span>or<span>git commit</span>, Vim is what opens!
Practical Case: Opening a Configuration File
Suppose we want to modify the system’s network configuration file <span>/etc/hosts</span>:
sudo vim /etc/hosts # Use vim to open the hosts file
After opening, you may be surprised to find that keyboard input is ineffective, the arrow keys are jumping around, and nothing seems to respond.
Don’t panic – this is not a system freeze, but because you haven’t yet understood the three modes of Vim.
2. The Three Modes of Vim
The most unique design of Vim is its multi-mode editing. The meaning of key presses is completely different in different modes. Understanding mode switching is key to learning Vim.
| Mode | Function Description | How to Enter | How to Exit |
|---|---|---|---|
| Command Mode | The default mode, used for moving the cursor, deleting, copying, pasting, etc. | Default entry upon starting vim | Press <span>i</span>, <span>a</span>, or <span>o</span> to enter Insert Mode |
| Insert Mode | Allows normal text input | Press <span>i</span>, <span>a</span>, or <span>o</span> in Command Mode |
Press <span>Esc</span> to return to Command Mode |
| Last Line Mode | Used for saving, exiting, searching, replacing, etc. | Enter <span>:</span> in Command Mode |
Automatically returns to Command Mode after executing the command |
Practical Case: Editing and Saving a File
Let’s go through the steps to experience Vim’s “multi-mode logic”:
vim test.txt # Open or create a text file
i # Enter Insert Mode (can input text)
Hello Linux! # Input content
Esc # Return to Command Mode
:wq # Save and exit the file
Explanation:
- i: Enter Insert Mode
- Esc: Return to Command Mode
- :wq: Save (write) and exit (quit)
If you only want to save without exiting, use :w
If you want to exit without saving, use :q!
3. Common Commands and Keys in Vim
Familiarity with common commands is key to moving from “being able to use Vim” to “mastering Vim”.
1️⃣ File Operation Commands
| Command | Function |
|---|---|
<span>:w</span> |
Save the file |
<span>:q</span> |
Exit Vim |
<span>:wq</span> |
Save and exit |
<span>:q!</span> |
Force exit without saving |
<span>:w new.txt</span> |
Save as a new file |
<span>:e filename</span> |
Open a new file |
<span>:r filename</span> |
Read the contents of another file into the current cursor position |
2️⃣ Cursor Movement Commands
| Command | Function |
|---|---|
<span>h / l</span> |
Move left / right |
<span>j / k</span> |
Move down / up |
<span>0</span> |
Move to the beginning of the line |
<span>$</span> |
Move to the end of the line |
<span>gg</span> |
Jump to the beginning of the file |
<span>G</span> |
Jump to the end of the file |
<span>/keyword</span> |
Search for a keyword |
<span>n / N</span> |
Repeat search down / up |
3️⃣ Text Editing Commands
| Command | Function |
|---|---|
<span>x</span> |
Delete the character under the cursor |
<span>dd</span> |
Delete the entire line |
<span>yy</span> |
Copy the entire line |
<span>p</span> |
Paste below the cursor |
<span>u</span> |
Undo the last operation |
<span>Ctrl + r</span> |
Redo (restore) |
<span>:set number</span> |
Show line numbers |
<span>:set nonumber</span> |
Turn off line numbers |
Practical Case: Quickly Modifying a Configuration File
Suppose we want to modify a configuration in <span>/etc/ssh/sshd_config</span>:
sudo vim /etc/ssh/sshd_config # Open the SSH configuration file
/Port # Search for the keyword "Port"
n # Jump to the next match
i # Enter Insert Mode
# Modify the port number (e.g., change to 2222)
Esc # Return to Command Mode
:wq # Save and exit
Explanation:
<span>/Port</span>: Find the configuration item
<span>n</span>: Continue searching for the next
<span>i</span>: Modify content
<span>:wq</span>: Save changes
With these steps, you have completed a secure modification of the SSH configuration file!
Conclusion
Vim may seem complex, but its logic is very clear – left hand for commands, right hand for thinking; once mastered, you will never want to part with it.