Powerful Vim Tips to Boost Your Coding Efficiency
This guide explains Vim’s role as a pure editor, introduces its three modes, and provides concrete command examples for inserting, navigating, deleting, copying, searching, replacing, saving, customizing shortcuts, and configuring .vimrc to make Vim feel like a modern IDE.
First, the article clarifies the distinction between an editor, a compiler, and a debugger: Vim is only an editor (similar to Windows Notepad), gcc is a compiler, and gdb is a debugger; Visual Studio integrates all three functions.
Vim Basics
Vim is a powerful multi‑mode text editor widely used on Linux/UNIX. It operates without menus, relying on commands. The default mode is command mode , where any typed characters are interpreted as commands. Other modes include insert mode and low‑line mode .
Common Commands in Command Mode
Insertion Commands
a– insert after the cursor. i – insert before the cursor. o – open a new line below the cursor. A – insert at the end of the current line. I – insert at the beginning of the current line. O – open a new line above the current line.
Navigation Commands
Enter low‑line mode with Shift+: then set nu to show line numbers, set nonu to hide them. GG – jump to the start of the last line; gg – jump to the start of the first line. nG or :n – jump to line n . $ – move to the end of the current line; 0 – move to the beginning. b – move backward by one word; w – move forward by one word. h j k l – left, down, up, right (arrow keys work as well). L – move to the last line of the visible screen.
Deletion Commands
x– delete the character under the cursor. nx – delete n characters after the cursor; nX – delete n characters before the cursor. dd – delete the current line. D – delete from the cursor to the end of the line. ndd – delete n consecutive lines starting from the current line. dG – delete from the cursor to the end of the file. :n1,n2d – delete a specific range of lines. u – undo the last change (similar to Ctrl+Z).
Copy and Paste
yycopies the current line; nyy copies the current line plus the next n‑1 lines. p pastes after the cursor, P pastes before. dd cuts the current line; ndd cuts n lines. The same p / P commands paste the cut text.
Search and Replace
:%s/old/new– replace all occurrences of old with new in the whole file. :n1,n2s/old/new/g – replace in a line range; add c to confirm each replacement. r – replace the character under the cursor. R – enter replace mode to overwrite characters until Esc is pressed. u – cancel the previous operation.
Save and Quit
:w!– force write (save) changes. :q! – quit without saving. :wq! or x! – save and quit. :w filename – write the buffer to a new file (useful for backup).
Advanced Tips
Import Command Output
Use :r !date to insert the current date directly at the cursor, avoiding manual copy‑paste.
Commenting Multiple Lines
To comment a range of lines, execute :n1,n2s/^/#/g. The caret ^ anchors the substitution at the line start, inserting #. To remove the comment, use :n1,n2s/^#//g.
Defining Custom Mappings
Example: :map ^P I# maps Ctrl+P to insert a # at the beginning of the line and return to command mode.
Another example: :map ^B 0x maps Ctrl+B to move to the start of the line and delete the first character.
Abbreviations
Define a shortcut for frequently typed text with :ab mymail [email protected]. Typing mymail followed by Enter expands to the full email address.
These mappings are temporary; to make them permanent, add them to .vimrc.
Basic Configuration
All settings are stored in .vimrc. For a regular user, edit the file with vim .vimrc and add desired options.
An example of installing a ready‑made configuration:
curl -sLf https://gitee.com/HGtz2222/VimForCpp/raw/master/install.sh -o ./install.sh && bash ./install.shAfter the script finishes, run source ~/.bashrc to apply the changes.
To uninstall, execute bash ~/.VimForCpp/uninstall.sh.
Original link: https://blog.csdn.net/m0_61933976/article/details/124653524
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.
Linux Tech Enthusiast
Focused on sharing practical Linux technology content, covering Linux fundamentals, applications, tools, as well as databases, operating systems, network security, and other technical knowledge.
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.
