Master Vim: 12 Essential Tips to Boost Your Coding Efficiency
This guide presents twelve practical Vim techniques—from using built‑in help and saving files with sudo to converting tabs, managing indentation, displaying line numbers, and enabling spell check—helping developers work faster and avoid common editing pitfalls.
1. Use Built‑in Help
Access Vim's documentation with :help followed by a command or keyword, e.g., :help :w for write command help.
2. Edit as Normal User, Save as Root
When a file requires elevated privileges, write changes without leaving Vim using: :w !sudo tee % This prompts for the sudo password and writes the file safely. Prefer sudoedit over sudo vim for such tasks.
3. Convert Spaces ↔ Tabs
3.1 Convert spaces to tabs
:set noexpandtab</code>
<code>:retab!3.2 Convert tabs to spaces
:set expandtab</code>
<code>:set tabstop=4</code>
<code>:set shiftwidth=4</code>
<code>:retabThese commands adjust Vim's indentation behavior and replace the opposite whitespace characters.
4. Indent All Lines
Press gg to go to the top, then = followed by G to re‑indent the entire file automatically.
5. Preserve Indentation When Pasting Code
Add the following to .vimrc to toggle paste mode with F2:
set pastetoggle=<F2>6. Start Writing at Correct Indent Depth
From normal mode, press S at the beginning of a line; Vim moves the cursor to the appropriate indent and enters insert mode.
7. Show Differences Before Saving
Run the following to compare the buffer with the file on disk: :w !diff % - The command writes the unsaved buffer to STDIN, then diff compares it with the current file.
8. Enable Spell Checking
Turn on Vim's spell checker with: :set spell To make it permanent, add the same line to .vimrc.
9. Show Line Numbers
Enable both absolute and relative line numbers by adding to .vimrc: set number relativenumber This displays the current line's absolute number and relative numbers for all other lines.
10. Open a File at a Specific Line
Use the +n option (where n is the line number) when launching Vim:
vim +n <file-name>11. Choose Readable Color Schemes
Select a color scheme that highlights syntax clearly; examples are shown in the accompanying screenshots.
12. Delete Text in Insert Mode
Common shortcuts while in insert mode: Ctrl+W – delete the previous word (equivalent to db in normal mode). Ctrl+H – delete the previous character. Ctrl+U – delete all characters before the cursor on the current line (like d0). Ctrl+K – delete from the cursor to the end of the line (like d$).
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.
