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 indentation, displaying line numbers, and configuring spell checking—empowering developers to work faster and avoid common pitfalls.
1. Use Built‑in Help
Vim’s internal help can be accessed with :help followed by a command or keyword, e.g., :help :w for the write command.
2. Edit as Normal User, Save as Root
When a file requires elevated privileges, type: :w !sudo tee % The command writes the buffer to stdout, pipes it to sudo tee, which writes to the original file ( %).
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>:retab4. Indent All Lines
Press gg to go to the top, then = and G to re‑indent the whole file, or use the command :gg=G.
5. Preserve Indentation When Pasting
Add the following to .vimrc: set pastetoggle=<F2> Press F2 before pasting to keep original indentation.
6. Start Writing at Correct Indent Depth
In normal mode, press S (uppercase) at the beginning of a line; Vim moves the cursor to the proper indent and enters insert mode.
7. Show Diff Before Saving
Use: :w !diff % - This writes the unsaved buffer to stdin and compares it with the file on disk.
8. Enable Spell Checking
Turn on Vim’s spell checker with: :set spell Misspelled words are highlighted according to the color scheme.
9. Display Line Numbers
Add to .vimrc: set number relativenumber This shows absolute numbers on the current line and relative numbers on others.
10. Open File at a Specific Line
Launch Vim with +n where n is the line number:
vim +n <file-name>11. Use Readable Color Schemes
Select a color scheme that highlights syntax clearly; examples are shown in the accompanying screenshots.
12. Delete Text in Insert Mode
Ctrl+w– delete the previous word. Ctrl+h – delete the previous character. Ctrl+u – delete everything before the cursor on the current line. Ctrl+k – delete everything after the cursor on the current line.
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 Cloud Computing Practice
Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!
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.
