In the world of Linux and Unix, there are three essential text processing tools: <span>awk</span>, <span>sed</span>, and <span>vi</span>. Among them, vi is the most classic text editor, which has remained indispensable for decades. The vi editor is a tool that almost all system administrators and developers cannot avoid; it is lightweight, fast, and does not rely on a graphical interface. It is often pre-installed even in the most minimal systems. For example, when modifying configuration files, vi is often the only tool that can accomplish the task. So, let’s not hesitate any longer and start learning together!
Getting to Know vi: The Mystery of Three Modes
Command Mode
This is the default mode of vi. In this mode, every character you input is interpreted as a command rather than text to be inserted.
Insert Mode
In this mode, you can input text normally, just like using a regular editor.
Last Line Mode
In this mode, all input is used to execute operations such as saving, exiting, and searching or replacing text.
Switching Modes: When you open a file, you enter command mode. Press <span>i</span> to enter insert mode, and press <span>Esc</span> to return to command mode—this is the most basic yet crucial operation in vi. In command mode, press <span>:</span> to enter last line mode, and press <span>Esc</span> to return to command mode. You can also switch modes using the following methods:
- •
<span>i</span>→ Enter insert mode before the cursor - •
<span>I</span>→ Insert at the beginning of the line - •
<span>a</span>→ Enter insert mode after the cursor - •
<span>A</span>→ Append at the end of the line - •
<span>o</span>→ Create a new line below the current line and enter insert mode - •
<span>O</span>→ Create a new line above the current line and enter insert mode - •
<span>Esc</span>→ Return to command mode - •
<span>:</span>→ Enter last line mode from command mode; if currently in insert mode, you must first return to command mode
Core Commands You Must Master
1. Start and Exit
vi filename Open file
vi newfile.txt Create new file
vi -R filename Open in read-only mode
:w Save file
:q Exit without saving; cannot execute if content has changed
:wq Save and exit
:q! Force exit without saving
ZZ Save and exit (shortcut, uppercase Z)
2. Cursor Movement (in Command Mode)
h Move left
j Move down
k Move up
l Move right
w Move to the beginning of the next word
b Move to the beginning of the previous word
e Move to the end of the current word
0 Beginning of the line
$ End of the line
gg Beginning of the file
G End of the file
数字+G Jump to the specified line number
3. Text Editing (in Command Mode)
x Delete the current character
dw Delete from the cursor to the end of the word
d$ Delete from the cursor to the end of the line
dd Delete the current line
yy Copy the current line
数字+yy Copy the specified number of lines below the cursor
p Paste after the cursor
P Paste before the cursor
u Undo the last operation
Ctrl+r Redo
4. Search and Replace (in Command Mode)
/keyword Search forward for the keyword, referred to as a pattern, involving regular expressions, which will not be elaborated here
?keyword Search backward for the keyword
n Find the next occurrence
N Find the previous occurrence
:s/old/new/ Replace the first match in the current line
:s/old/new/g Replace all matches in the current line
:%s/old/new/g Replace all matches in the entire file
:%s/old/new/gc Replace with confirmation for each occurrence
Advanced Techniques: Boost Your Editing Speed
The true power of vi lies in command combinations, for example:
- •
<span>5dd</span>→ Delete 5 lines - •
<span>10yy</span>→ Copy 10 lines - •
<span>10j</span>→ Move down 10 lines
Here are a few more fun examples:
- •
<span>caw</span>→ Change the current word (delete and enter insert mode);<span>c</span>means change, similar to<span>d</span>and<span>y</span>commands
- •
<span>ciw</span>→ Delete the word under the cursor (excluding spaces) and enter insert mode. - •
<span>diw</span>→ Delete the word under the cursor (excluding spaces) but do not enter insert mode. - •
<span>dap</span>→ Delete the entire paragraph under the cursor (including line breaks). - •
<span>yap</span>→ Copy the entire paragraph under the cursor. - •
<span>ci"</span>→ Delete the content inside the quotes and enter insert mode. - •
<span>ci(</span>→ Delete the content inside the parentheses and enter insert mode.
Personalize Your Editing Interface:
<span>:set nu</span> Show line numbers<span>:set nonu</span> Hide line numbers<span>:syntax on</span> Enable syntax highlighting<span>:syntax off</span> Disable syntax highlighting
You can create a <span>~/.vimrc</span> file to configure your vi, so you don’t have to modify settings every time you open a file:
set number # Show line numbers
set tabstop=4 # Width of Tab key
set shiftwidth=4 # Automatic indent width
set expandtab # Convert Tab to spaces
set hlsearch # Highlight search results
set incsearch # Incremental search
syntax on # Syntax highlighting
vi vs vim
In modern systems, vi is usually a link to vim (Vi Improved). Vim retains all the functionalities of vi and adds powerful features such as syntax highlighting, multi-level undo, and a plugin system, making it a super enhanced version of vi.
➜ ~ vi --version
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jul 11 2025 21:28:27)
macOS version - arm64
Included patches: 1-754
| Comparison Item | vi | vim |
| Functionality | Classic basic editor | Enhanced version of vi |
| Cross-platform | Built-in for Unix/Linux | Full support for Unix/Linux/macOS/Windows |
| Syntax Highlighting | ❌ Not supported | ✅ Supports syntax highlighting for multiple programming languages |
| Multi-window/Tabs | ❌ Not supported | ✅ Supported |
| Plugin System | ❌ None | ✅ Rich plugin ecosystem |
| Undo Operations | Can only undo the last action | Can undo/redo infinitely |
| Search and Replace | Basic functionality | More powerful search, replace, and regex support |
| Learning Curve | Basic, limited commands | More powerful but also more complex |
Learning Suggestions
Beginners may feel frustrated with vi, but please do not give up. Here are some suggestions:
- 1. First master the basic mode switching and saving/exiting
- 2. Familiarize yourself with cursor movement and basic editing commands
- 3. Use vi daily to complete some editing tasks
- 4. Gradually learn more advanced features
vi is like a superior martial art that requires diligent practice to master. Once proficient, you can edit text at the speed of thought and feel the joy of your fingertips flying across the keyboard.
Once you master vi, you will have a powerful tool to navigate the terminal world freely. In the future, we will introduce the other two tools of the three text processing tools: awk and sed, so stay tuned!
#Linux #ThreeTextProcessingTools #viEditor #CommandLineSkills
