Master Vim: Essential Commands and Tips for Efficient Text Editing
This guide introduces Vim, the powerful vi‑based editor, explains its three modes, and provides detailed examples of common commands such as navigation, line operations, multi‑file handling, visual selection, commenting, case conversion, and advanced features like split windows and auto‑completion, helping users edit code more efficiently.
Vim Text Editor Overview
Linux's vi is a text editor; its upgraded version is Vim. Vim inherits vi's three modes: normal mode, insert mode, and command-line mode.
Basic Navigation Commands
Ctrl+f – page forward
Ctrl+b – page backward
n – move to column n in the current line
Common Command Cases
1. Delete Specified Lines
Command (command-line mode): :m,nd Explanation: m is the start line, n is the end line, d deletes. Press Enter to execute.
2. Copy Specified Lines
Command (command-line mode): :m,ny Explanation: y yanks (copies) the range from line m to n. Press Enter to execute.
3. Select Specified Lines
Command (normal mode): mGVnG Explanation: mG moves to line m, nG moves to line n, V enters visual line mode, after which you can copy ( y) or delete ( d).
Example for selecting all lines:
ggVG4. Multi‑Line Comment / Uncomment
Comment: press Ctrl+v to enter block visual mode, move the cursor to select the column range, press I, type the comment symbol (e.g., //), then Esc to apply the comment to all selected lines.
Uncomment: press v to enter visual mode, select the comment characters, press Esc, then Ctrl+v to enter block visual mode, select the comment symbols, and press d to delete them.
5. Auto‑Completion of Code Members
Use Ctrl+N to search forward and Ctrl+P to search backward for completion suggestions.
Additional Vim Features
1. Block Selection
Note: navigation within the block still uses h j k l or arrow keys.
2. Multiple File Switching
Open several files with vim file1 file2 file3 .... Switch between files to copy, paste, or delete across buffers.
3. Multiple Window Functionality
Display two windows side by side.
Commands: vim -o file1 file2 – open files in horizontal splits vim -O file1 file2 – open files in vertical splits vim -d file1 file2 – vertical split with diff mode qall – quit all windows (prompts if unsaved changes) qall! – force quit without saving wall – write all changed files
4. Case Conversion
Use gu to change to lowercase and gU to change to uppercase.
Examples: guw – lowercase word under cursor gUw – uppercase word under cursor gUG – uppercase from cursor to end of file gUgg – uppercase from cursor to start of file 3gU – uppercase three lines down ggguG – lowercase entire file gggUG – uppercase entire file
Common Small Operations
Undo: u (redo: Ctrl+r)
Count occurrences of a string, e.g.,
:%s/hello//gnSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
