Vim is like having a Swiss Army knife for text - it might seem intimidating at first (all those buttons!), but once you learn the basics, you'll edit text faster than ever before. The beauty of Vim is that your fingers never need to leave the keyboard, saving you precious time that adds up over your coding career.
Let's break down this powerful tool into bite-sized, easy-to-understand pieces!
Understanding Vim's Modes: The Foundation
When to Use Different Modes
- Command Mode - Use when you want to navigate, delete text, or execute commands
- Insert Mode - Use when you want to add or edit text
- Visual Mode - Use when you want to select blocks of text for copying or manipulation
Common Mode Switching Options
Command |
What It Does |
When to Use It |
i |
Insert before cursor |
When you need to add text at current position |
a |
Insert after cursor |
When you need to append text after current character |
o |
Open new line below |
When you want to start typing on a new line below |
O |
Open new line above |
When you want to start typing on a new line above |
Esc |
Exit to Command mode |
When you're done typing and want to execute commands |
Practical Examples
# Opening a file
vim myfile.txt # Starts in Command mode
# Starting to type in an empty file
i # Press i to enter Insert mode
Hello, world! # Type your text
<Esc> # Press Esc to return to Command mode
# Adding text after existing content
a # Moves cursor one position right and enters Insert mode
<Esc> # Return to Command mode when finished
# Adding a new line below current position
o # Creates new line below and enters Insert mode
This is a new line # Type your text
<Esc> # Return to Command mode
Navigating in Vim: Moving Around Like a Pro
When to Use Navigation Commands
- When you need to move to a specific location in your file
- When you want to jump to the beginning or end of a line
- When you need to go to a specific line number
- When you want to navigate through your document without using the mouse
Common Navigation Options
Command |
What It Does |
When to Use It |
h , j , k , l |
Move left, down, up, right |
For basic character-by-character navigation |
w |
Move to start of next word |
When moving forward by words |
b |
Move to start of previous word |
When moving backward by words |
0 |
Move to beginning of line |
When you need to get to the start of a line |
$ |
Move to end of line |
When you need to get to the end of a line |
gg |
Go to beginning of file |
When you need to jump to the top |
G |
Go to end of file |
When you need to jump to the bottom |
10G |
Go to line 10 |
When you need to jump to a specific line |
Practical Examples
# Moving to the beginning of a file
gg # Takes you to the first line
# Moving down 5 lines
5j # Moves cursor down 5 lines
# Jumping to line 42
42G # Takes you directly to line 42
# Moving to the beginning of current line
0 # Positions cursor at first character
# Moving to the end of current line
$ # Positions cursor after last character
Editing Text: The Power of Vim Commands
When to Use Editing Commands
- When you need to delete, change, or copy text
- When you want to manipulate words, lines, or paragraphs efficiently
- When you need to make repetitive edits
- When you want to undo or redo changes
Common Editing Options
Command |
What It Does |
When to Use It |
x |
Delete character under cursor |
When removing a single character |
dd |
Delete current line |
When removing an entire line |
3dd |
Delete 3 lines |
When removing multiple lines |
dw |
Delete from cursor to end of word |
When removing part of a word |
cw |
Change word (delete and enter Insert mode) |
When replacing a word |
r |
Replace single character |
When changing just one character |
yy |
Yank (copy) current line |
When copying a line |
p |
Paste after cursor |
When pasting copied or deleted text |
u |
Undo last change |
When you make a mistake |
Ctrl+r |
Redo last undone change |
When you undo too much |
Practical Examples
# Replacing a word
cw # Delete word and enter Insert mode
better # Type replacement word
<Esc> # Return to Command mode
# Deleting a paragraph
3dd # Delete three lines at once
# Copying and pasting
yy # Copy current line
p # Paste below current line
5yy # Copy 5 lines
p # Paste all 5 lines below current position
# Fixing a mistake
u # Undo last change
Ctrl+r # Redo if you undo too much
Searching and Replacing: Finding What You Need
When to Use Search Commands
- When you need to find specific text in a file
- When you want to jump to occurrences of a pattern
- When you need to replace text throughout a file
- When locating errors or specific sections of code
Common Search Options
Command |
What It Does |
When to Use It |
/pattern |
Search forward for pattern |
When looking for text ahead in the file |
?pattern |
Search backward for pattern |
When looking for text earlier in the file |
n |
Repeat search in same direction |
When finding next occurrence |
N |
Repeat search in opposite direction |
When finding previous occurrence |
:%s/old/new/g |
Replace all occurrences in file |
When doing global find and replace |
Practical Examples
# Finding all instances of "error"
/error # Search forward for "error"
n # Go to next occurrence
N # Go to previous occurrence
# Replacing "bug" with "issue" throughout file
:%s/bug/issue/g # Replace globally
:%s/bug/issue/gc # Replace globally with confirmation
# Clearing search highlighting
:noh # Turns off highlighting until next search
Working with Multiple Files: Buffers and Split Screens
When to Use Multiple File Features
- When working on related files at the same time
- When comparing different files
- When copying content between files
- When managing a complex project with multiple components
Common Multiple File Options
Command |
What It Does |
When to Use It |
:e filename |
Edit a new file |
When opening another file |
:ls |
List all open buffers |
When checking which files are open |
:buffer n |
Switch to buffer n |
When moving between open files |
:bn |
Go to next buffer |
When cycling through open files |
:bp |
Go to previous buffer |
When cycling through open files |
:bd |
Delete (close) current buffer |
When done with a file |
:split filename |
Open file in horizontal split |
When viewing two files one above the other |
:vsplit filename |
Open file in vertical split |
When viewing two files side by side |
Ctrl+w, arrow key |
Move between splits |
When navigating between split windows |
:only |
Close all splits except current one |
When focusing on just one file again |
Practical Examples
# Opening multiple files at once
vim file1.txt file2.txt # Opens both files in buffers
# Checking open buffers and switching between them
:ls # Lists all open buffers with numbers
:buffer 2 # Switches to buffer number 2
:bn # Goes to next buffer
:bp # Goes to previous buffer
# Working with split screens
:split file2.txt # Opens file2.txt in horizontal split
:vsplit file3.txt # Opens file3.txt in vertical split
Ctrl+w, ↓ # Move to split below
Ctrl+w, → # Move to split on right
:only # Close all splits except current one
Customizing Vim: Making It Your Own
When to Use Customization Commands
- When you want to make Vim more comfortable to use
- When you need to see line numbers
- When you want to change the visual appearance
- When running scripts from within Vim
Common Customization Options
Command |
What It Does |
When to Use It |
:set nu |
Show line numbers |
When you need to reference specific lines |
:set nonu |
Hide line numbers |
When you want a cleaner display |
:colorscheme [name] |
Change color scheme |
When you want a different look |
:!bash % |
Run current bash script |
When testing a script you're editing |
Practical Examples
# Turning on line numbers
:set nu # Shows line numbers along left side
# Changing color schemes
:colorscheme <Ctrl+d> # List available colorschemes
:colorscheme desert # Change to the desert colorscheme
# Running an external command
:!ls -la # Runs ls -la and shows output