Fundamentals 11 min read

12 Essential Vim Tricks Every Developer Should Know

This guide compiles twelve practical Vim techniques—from accessing built‑in help and saving files with sudo to converting tabs and spaces, managing indentation, preserving paste formatting, enabling spell checking, displaying mixed line numbers, previewing diffs, opening files at a specific line, and using efficient delete shortcuts—each illustrated with commands and screenshots.

Liangxu Linux
Liangxu Linux
Liangxu Linux
12 Essential Vim Tricks Every Developer Should Know

1. Use Vim's built‑in help

Access detailed documentation directly in Vim with the :help command. For example, :help :w shows help for the write command, while :help j explains the normal‑mode j movement.

2. Edit as a normal user, save as root

When a file requires elevated privileges, open it normally and write it back using sudo: :w !sudo tee % This pipes the buffer to sudo tee, which writes the file with root rights. The % placeholder expands to the current filename.

3. Convert spaces and tabs

3.1 Convert all spaces to tabs

:set noexpandtab</code>
<code>:retab!

Disables automatic expansion of tabs to spaces and rewrites existing spaces as tabs.

3.2 Convert all tabs to spaces

:set expandtab</code>
<code>:set tabstop=4</code>
<code>:set shiftwidth=4</code>
<code>:retab

Enables tab‑to‑space conversion, sets the number of spaces per tab, and rewrites tabs accordingly.

4. Indent every line

Press gg to go to the top of the file, then = followed by G to re‑indent all lines automatically.

5. Preserve indentation when pasting code

Add the following line to .vimrc to toggle paste mode with F2:

set pastetoggle=<F2>

6. Start writing at the correct indent depth

From normal mode, press Esc then S (uppercase). Vim moves the cursor to the appropriate indent level and switches to insert mode.

7. Show differences before saving

Run the following command to compare the buffer with the file on disk: :w !diff % - The command writes the unsaved buffer to STDIN ( -) and runs diff against the original file ( %).

8. Enable spell checking

Turn on Vim's built‑in spell checker: :set spell Misspelled words are highlighted (often with an underline). To make this permanent, add the same line to .vimrc.

9. Display line numbers

Show both absolute and relative line numbers by adding to .vimrc: set number relativenumber The current line shows its absolute number, while other lines show their distance from the cursor.

10. Open a file at a specific line

Use the +n argument (where n is the line number) when launching Vim:

vim +n <file-name>

11. Delete text while in insert mode

Common shortcuts that work in insert mode: Ctrl+W – delete the previous word (equivalent to db in normal mode). Ctrl+H – delete the previous character. Ctrl+U – delete everything before the cursor on the current line (like d0). Ctrl+K – delete from the cursor to the end of the line (like d$).

12. Choose a readable colorscheme

Select a colorscheme that highlights syntax clearly, helping you spot keywords, variables, and identifiers more easily. Screenshots of several popular schemes are shown below:

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Configurationcommand-linetext editor
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.