Let me be honest—I’m not a hardcore Vim enthusiast who lives and breathes in terminal editors. But I’ve come to absolutely appreciate this powerful little tool for what it does best: editing files on remote servers through SSH connections. There’s nothing quite like the feeling of efficiently manipulating text without ever touching your mouse!
The biggest challenge? I don’t use Vim every day, which means I constantly forget commands and have to Google them. It’s frustrating, time-consuming, and breaks my workflow. That’s why I created this comprehensive guide—initially as a personal reference, but now I’m sharing it with you, too!
This isn’t just another Vim cheatsheet. It’s a carefully curated collection of the most practical Vim commands that developers actually use daily. Whether you’re a complete beginner or looking to level up your Vim skills, you’ll find exactly what you need here.
Tip 💡: Working with files that should be version controlled? Consider mastering the essential git commands as well!
Before diving into specific commands, let’s understand how Vim operates. Unlike most text editors, Vim uses different modes:
To open a file in Vim:
vim filename.txt # Open or create a file
vim +143 filename.txt # Open file at line 143
vim -R filename.txt # Open in read-only mode
Code language: PHP (php)
These are the absolute foundation of working with Vim:
i # Enter insert mode at cursor position
I # Enter insert mode at beginning of line
a # Enter insert mode after cursor position
A # Enter insert mode at end of line
Esc # Exit insert mode (return to normal mode)
Code language: PHP (php)
Remember—most commands only work in normal mode, so hitting Esc
before trying other commands is crucial if things aren’t working!
Moving efficiently is where Vim truly shines:
h # Move cursor left
j # Move cursor down
k # Move cursor up
l # Move cursor right
Code language: PHP (php)
w # Jump to start of next word
b # Jump to start of previous word
e # Jump to end of word
0 # Jump to start of line
$ # Jump to end of line
^ # Jump to first non-blank character in line
Code language: PHP (php)
gg # Go to first line of document
G # Go to last line of document
:n # Jump to line n (replace n with line number)
Ctrl+f # Page down (forward)
Ctrl+b # Page up (backward)
Code language: PHP (php)
Finding and replacing text is incredibly powerful in Vim:
/pattern # Search forward for pattern
?pattern # Search backward for pattern
n # Repeat search in same direction
N # Repeat search in opposite direction
:%s/old/new/g # Replace all old with new throughout file
:%s/old/new/gc # Replace all old with new throughout file with confirmations
Code language: PHP (php)
These commands make text manipulation much faster:
x # Delete character under cursor
dw # Delete word from cursor position
d$ # Delete to end of line
dd # Delete entire line
5dd # Delete 5 lines
Code language: PHP (php)
yy # Yank (copy) a line
5yy # Yank 5 lines
y$ # Yank to end of line
p # Put (paste) after cursor
P # Put (paste) before cursor
Code language: PHP (php)
v # Start visual mode for character selection
V # Start visual mode for line selection
Ctrl+v # Start visual block mode (column selection)
Code language: PHP (php)
After selecting text with visual mode, you can:
d
to delete selected texty
to copy selected textp
to replace selected text with clipboard contentsVim can handle multiple files efficiently:
:e filename # Edit a file in a new buffer
:bn # Next buffer
:bp # Previous buffer
:bd # Delete buffer (close file)
:sp filename # Open file in horizontal split
:vsp filename # Open file in vertical split
Ctrl+ws # Split window horizontally
Ctrl+wv # Split window vertically
Ctrl+ww # Switch between windows
Ctrl+wq # Quit a window
Code language: PHP (php)
Don’t get stuck in Vim! Here’s how to get out:
:w # Write (save) the file
:w filename # Write to specified filename
:wq or :x # Write and quit
:q # Quit (fails if unsaved changes)
:q! # Quit and discard unsaved changes
:w !sudo tee % # Save file with sudo privileges
Code language: CSS (css)
That last command is an absolute lifesaver when you forget to open a file with sudo!
Line numbers make navigation much easier:
:set number # Show line numbers
:set nonumber # Hide line numbers
Code language: JavaScript (javascript)
These commands help you manipulate text more efficiently:
>> # Indent line
<< # Unindent line
=G # Auto-indent entire file
Code language: PHP (php)
~ # Change case of character under cursor
gUw # Change word to uppercase
guw # Change word to lowercase
Code language: PHP (php)
To delete all content in a file:
gg # Go to beginning of file
dG # Delete to end of file
Code language: PHP (php)
Or select all and delete:
ggVGd # Go to top, visual select to bottom, delete
Code language: PHP (php)
. # Repeat last command
Code language: PHP (php)
q{letter} # Start recording macro into register {letter}
q # Stop recording macro
@{letter} # Execute macro in register {letter}
@@ # Repeat last executed macro
Code language: PHP (php)
m{letter} # Set mark at current position (a-z for local marks)
'{letter} # Jump to line of mark
`{letter} # Jump to position of mark
Code language: PHP (php)
You can customize your Vim experience by editing the .vimrc
file in your home directory:
:syntax on # Enable syntax highlighting
:set tabstop=4 # Set tab width to 4 spaces
:set autoindent # Enable auto-indentation
:set hlsearch # Highlight search results
Code language: PHP (php)
If you’re stuck in a mode and can’t get out, try:
Esc
multiple timesCtrl+c
:q!
to force quitIf you can’t save due to permission issues:
:w !sudo tee % > /dev/null
Code language: JavaScript (javascript)
If your terminal gets messed up after using Vim:
:!reset
Vim has an incredibly steep learning curve, but the productivity gains are enormous once you master it. Start by memorizing just a few essential commands, then gradually add more to your toolkit as you become more comfortable.
Remember, the key to mastering Vim is consistency—try to use it regularly, even for small editing tasks, to build muscle memory. Before you know it, you’ll be editing text at the speed of thought!
What are your favorite Vim commands? Did I miss any crucial ones that save you time? Let me know in the comments!
Unlock the full potential of service workers with advanced features like push notifications, background sync, and performance optimization techniques that transform your web app into…
Learn how to integrate service workers in React, Next.js, Vue, and Angular with practical code examples and production-ready implementations for modern web applications.
Master the essential service worker caching strategies that transform web performance. Learn Cache-First, Network-First, and Stale-While-Revalidate patterns with practical examples that'll make your apps blazingly…
This website uses cookies.