Vim Motions and Tricks - Plain Text Guide
Essential Vim Motions & Tricks
Master these lesser-known, yet powerful, Vim commands to dramatically speed up your text editing workflow.
1. Quick Exits
Save precious seconds when closing files.
- Save and Quit: Type
ZZ(Two capital Zs). This is a quick way to write changes and exit. - Quit without saving: Type
ZQ(Capital Z, then Q). Quits without saving, similar to using:q!.
2. Smart Text Selection & Modification (Inner Blocks)
Use the “inner block” motion (i) combined with the block type to quickly target content within delimiters.
- Select inside parentheses
(): Usevib(visual inner block or ‘b’ for balanced symbols). - Select inside curly braces
{}: UseviB(visual inner BRACE, capital B). - Change inside quotes
"": Useci"(change inner quote). This works with''and\`too. - Change inside parentheses
(): Usecib. This quickly deletes the content and puts you in insert mode.
3. Visual Block Mode for Multi-Line Edits
Perform operations on blocks of text across multiple lines.
- Enter Block Mode: Press
Ctrl+vbefore selecting lines. - Insert at Start of Block: While in Visual Block mode, press capital I (
I). Type your text, then pressEsc; the text will appear on every selected line. - Append at End of Block: After selecting the block and moving to the end of the line (
$), press capital A (A). Type your text, then pressEsc; the text will appear appended to every selected line. - Re-select Last Block: Use
gvto jump back into Visual Block mode with the last block selected.
4. Navigation & Structure
Fluidly move around and clean up your code.
- Re-indent the whole file: Use
gg=G. This combines go to top (gg), apply auto-indent (=), and go to bottom (G). - Jump between matching pairs: Use
%. Place the cursor on a(,{,[, or similar symbol and jump directly to its match. - Jump to specific line number: Type the line number, then capital G (e.g.,
42Ggoes to line 42). - Join lines with a space: Use capital J (
J). This joins the current line and the next with one space between them. - Join lines without a space: Use
gJ. This joins the lines without inserting a space.
5. Toggle Case
Quickly switch the case of text.
- Toggle character case: Use
~(Tilde) in Normal mode to toggle the case of the character under the cursor. - Toggle word case: Use
g~wto toggle the case for the current word. - Toggle case in a tag: Use
g~itto toggle the case for text inner tag (useful for HTML/XML).
6. Sessions, URLs, and Files
Quickly interact with your environment.
- Suspend Vim to terminal: Press
Ctrl+zto temporarily pause Vim and return to the shell. - Return Vim to foreground: Type
fgin your terminal to resume your Vim session. - Open URL under cursor: Use
gx. This opens the web link under the cursor in your default browser. - Open file path under cursor: Use
gf. This opens the file path under the cursor (if it exists in the Vim path).
7. Advanced Bookmarks (Marks)
Marks allow you to quickly jump between locations within and across files.
- Set Mark (Current File): Use
mfollowed by a lowercase letter (e.g.,ma). This mark is only valid within the current file. - Jump to Mark (Current File): Use
'(single quote) followed by the lowercase letter (e.g.,'a). Jumps to the marked line and column. - Set Mark (Across Files): Use
mfollowed by an uppercase letter (e.g.,mA). This mark is valid across files and persists until Vim closes. - Jump to Mark (Across Files): Use
'(single quote) followed by the uppercase letter (e.g.,'A). Jumps to the correct file and line.