Master Vim: Essential Commands Every Developer Should Know
This article introduces the long‑standing vi/vim editor, explains why it remains essential for developers, and provides concise, practical instructions on navigation, editing, visual and block modes, macro recording, searching, and exiting, helping readers become efficient with Vim in production environments.
Introduction
The most widely used editor in online development is vi. Whether you need to quickly view a file or edit it, vi is handy. It is a long‑lived tool; this article focuses on the most common functions used in production, assuming plugins and scripts are handled on development machines.
vim is an enhanced version of vi . Modern Linux distributions include the enhanced version by default, so the examples below use vim .
Develop Good Habits
The biggest contribution of vim is its key system . Editors like Chrome, IDEA, and Atom provide a vim mode because of this. Experienced programmers often use arrow keys, which works but abandons the core efficiency of vim. Persistently using h, j, k, l builds muscle memory. vim also offers a mode‑based workflow with four modes; you only need examples to understand them.
Avoid Pitfalls
Do not open very large files with vim, as it reads the entire file into memory and may cause host memory overflow. Before opening, use du -h to check size; generally keep files under 100MB.
Common Operations
Navigation in Normal Mode
Wandering
j down 30j down 30 lines k up h left l right 0 start of line ^ first non‑blank character $ end of line gg go to file top G go to file bottom 100G jump to line 100
Do not move the cursor in Insert mode; it is inefficient.
Copy: y
yy copy a line 10yy copy 10 lines downwards yw copy a word from the cursor y$ copy to end of line yfB copy up to the first uppercase B y2fB copy up to the second uppercase B
Cut: x
x cut a character (or cut backward at line end) 3x cut three characters xp swap two characters, e.g., bs → sb
Delete: d
Deleted content goes to the clipboard; press p to paste elsewhere.
dd delete a line 200dd delete 200 lines dw delete a word df" delete up to the first double quote
Paste: p
p paste the copied or cut text 3p paste three times
Visual Mode
v Line Mode
Press v in Normal mode to enter visual mode. Use h, j, k, l to move and select content.
Ctrl+v Block Mode
Demo: add each line of a file to an ArrayList (image below).
Steps:
In Command mode, execute %s/$/\");/g to append data at line ends.
Press ESC, then gg to return to the top.
Press Ctrl+v to enter visual block mode, then G to reach the file bottom.
Press I to enter Insert mode and type list.add(".
Press ESC, then $ to return to line end.
Press j to move to the next line, then ^ to go to line start.
Press q to stop recording.
Execute @a to test the macro, or 100@a to repeat it 100 times.
Block mode can also swap columns, a technique seen in UE.
Command Mode
In Normal mode, type : to enter Command mode.
%s/$/sth/ append sth at line ends %s/\^M//g remove DOS carriage returns (enter Ctrl+v + Enter for \^M ) :g/^\s*$/d delete empty or whitespace‑only lines %s/#.*//g delete comments after #
Command mode uses regular expressions; these tricks are universal.
These commands resemble the sed utility for stream editing.
Search Strings
Press / in Normal mode to start a search, type the pattern, and confirm.
n find next match N find previous match 2n find the second next match
If navigation feels confusing, enable line numbers with set nu in Command mode.
Macro Recording
Macro recording is a powerful Vim feature. Using the earlier example, you can record a macro that adds each line to an ArrayList and replay it many times.
Steps:
Press gg to go to the top.
Start recording with qa (register a).
Enter Insert mode ( I) and type list.add(".
Return to Normal mode ( ESC) and jump to line end ( $).
Move to the next line ( j) and back to line start ( ^).
Stop recording with q.
Play the macro with @a or repeat 100@a for 100 lines.
You can record multiple macros for batch operations.
Other Useful Commands
r replace a character ggVG select all u undo changes J join the next line gU uppercase at cursor ggguG lowercase entire file % jump to matching tag (e.g., <div> ↔ </div> ) :e /tmp/a open file /tmp/a in the same editor (buffers share clipboard) bp previous buffer bn next buffer
Exit Editor
wq write and quit wqa write all and quit q! quit without saving qa! quit all without saving
This article focuses on the most frequently used Vim features to help readers handle text quickly in production environments. Many more advanced capabilities exist for you to explore.
Although Vim has a steep learning curve, frequent use makes it indispensable.
Signed-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.
