Master Vim: Essential Modes, Commands, and Tips for Linux Editing
This guide introduces Vim on Linux, covering its built‑in help, core editing modes, common commands for inserting text, moving the cursor, deleting, searching, copying, replacing, exiting, and handling swap‑file warnings, providing practical examples and command syntax for beginners.
Introduction
This chapter shares a concise tutorial on using the Vim editor in Linux, focusing on essential commands and workflow without unnecessary preamble.
Accessing Vim Help
Vim includes comprehensive documentation; simply start Vim and type :help to open the help system.
Vim Modes Overview
Vim operates in distinct modes, each defining how keystrokes are interpreted. The main modes are:
Normal mode : default editing mode; most commands are entered here. Press Esc to return from any other mode.
Command mode : invoked by :, /, or ? from Normal mode to execute longer commands or searches; finish with Enter.
Insert mode : for entering text; enter with i, a, or other insertion commands.
Visual mode : select text blocks; v for characterwise, V for linewise, Ctrl‑V for blockwise selection.
Select mode : similar to typical Windows editors; rarely used on Linux and omitted from further discussion.
Insert Mode Variants
i– insert at cursor position I – insert at beginning of the line o – open a new line below the cursor O – open a new line above the cursor a – append after the cursor A – append at the end of the line
Basic Vim Workflow
1. Starting Vim
[root@localhost myfolder]# ll [root@localhost myfolder]# vim file1When a file is opened, the bottom status line shows the filename, line count, character count, and cursor position. A tilde (~) indicates lines beyond the file’s content.
2. Inserting Text
Vim defaults to Normal mode; press i to switch to Insert mode and type normally. Press Esc to return to Normal mode. The command :set showmode displays the current mode (e.g., INSERT ) at the bottom; :set noshowmode hides it.
:set showmode :set noshowmode3. Moving the Cursor
In Normal mode, use the following keys: h – left j – down k – up l – right
Arrow keys work as well but are slower because they require hand movement away from the home row.
4. Deleting Text
x– delete the character under the cursor dd – delete the entire current line u – undo the last change Ctrl‑R – redo (reverse undo) U – undo all changes on the current line
5. Other Editing Commands
o– open a new line below and enter Insert mode O – open a new line above and enter Insert mode
6. Exiting Vim
ZZ– save and quit :q! – quit without saving :e! – reload the file, discarding changes :wq or :wq! – write (save) and quit
7. Jumping to Specific Lines
G– go to the last line 33G – go to line 33 gg or 1G – go to the first line
8. Simple Search
/pattern– search forward for pattern ?pattern – search backward n – repeat search in the same direction; N – repeat in opposite direction :set ignorecase / :set noignorecase – toggle case‑insensitive search :set hlsearch / :set nohlsearch – enable/disable highlight of matches
:set ignorecase :set noignorecase :set hlsearch :set nohlsearch9. Copy and Paste
Use the yank ( y) and put ( p) commands: yw – yank (copy) a word y2w – yank two words yy – yank a whole line 3yy – yank three consecutive lines dd followed by p – delete a line then paste it elsewhere
10. Replacing Text
:s/UNIX/linux/– replace first occurrence of UNIX on the current line :s/UNIX/linux/g – replace all occurrences on the current line :%s/UNIX/linux/g – replace throughout the entire file
Vim’s Protection Mechanism
If Vim is terminated abruptly (e.g., closing the terminal), a swap file .file.swp remains. On the next start you may see a warning like:
E325: 注意
发现交换文件 ".file.swp"
所有者: root 日期: Sun Jul 28 19:43:04 2024
文件名: ~root/myfolder/file
修改过: 是
用户名: root 主机名: localhost.localdomain
进程 ID: 3884
正在打开文件 "file"
日期: Sun Jul 28 17:25:26 2024
(1) Another program may be editing the same file.
(2) An edit session for this file crashed.The swap file protects against data loss and concurrent edits. You can choose to:
Open read‑only (O)
Continue editing (E)
Recover changes ( :recover or vim -r file)
Delete the swap file (D)
Quit (Q)
Abort (A)
After recovery, delete the swap file to avoid future warnings.
Permissions Note
Vim requires appropriate read/write permissions on the target file; lacking them will prevent editing.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
