
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!
Getting Started with Vim
Before diving into specific commands, let’s understand how Vim operates. Unlike most text editors, Vim uses different modes:
- Normal mode: The default mode for navigation and simple commands
- Insert mode: For typing and editing text
- Visual mode: For selecting text blocks
- Command mode: For executing commands
Opening Files with Vim
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)
Essential Mode Switching Commands
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!
Navigation Commands
Moving efficiently is where Vim truly shines:
Basic Movement
h # Move cursor left
j # Move cursor down
k # Move cursor up
l # Move cursor right
Code language: PHP (php)
Faster Movement
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)
Page Navigation
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)
Search and Replace
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)
Editing Commands
These commands make text manipulation much faster:
Deleting Text
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)
Copying and Pasting (Yank and Put)
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)
Text Selection
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:
- Press
d
to delete selected text - Press
y
to copy selected text - Press
p
to replace selected text with clipboard contents
Working with Multiple Files
Vim 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)
Saving and Exiting
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 and Navigation
Line numbers make navigation much easier:
:set number # Show line numbers
:set nonumber # Hide line numbers
Code language: JavaScript (javascript)
Advanced Text Manipulation
These commands help you manipulate text more efficiently:
Indentation
>> # Indent line
<< # Unindent line
=G # Auto-indent entire file
Code language: PHP (php)
Case Manipulation
~ # Change case of character under cursor
gUw # Change word to uppercase
guw # Change word to lowercase
Code language: PHP (php)
Text Deletion
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)
Vim Productivity Boosters
Repeating Commands
. # Repeat last command
Code language: PHP (php)
Macros
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)
Marks
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)
Customizing Vim
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)
Common Problems and Solutions
Stuck in a Mode?
If you’re stuck in a mode and can’t get out, try:
- Press
Esc
multiple times - Press
Ctrl+c
- Type
:q!
to force quit
File Permission Issues?
If you can’t save due to permission issues:
:w !sudo tee % > /dev/null
Code language: JavaScript (javascript)
Terminal Issues?
If your terminal gets messed up after using Vim:
:!reset
Final Thoughts
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!
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
Leave a Reply