Vim has a reputation for being difficult to learn. There's even a famous joke about developers not knowing how to exit it. But here's the truth: once you understand Vim's logic, it becomes one of the most powerful and efficient text editors you'll ever use.
This guide teaches you the essential Vim commands you need to be productive. No overwhelming feature lists or obscure shortcuts. Just the core skills that will make you comfortable editing files in the terminal.
Whether you're SSH'd into a server, working on a remote machine, or just want to level up your terminal skills, this guide has you covered.
Why learn Vim?
Before diving into commands, let's address the obvious question: why bother learning Vim when modern editors like VS Code exist?
Vim is everywhere. Every Linux server, Mac, and Unix system has Vim or Vi installed by default. When you SSH into a production server to fix a critical bug at 2 AM, you won't have VS Code. You'll have Vim.
Vim is fast. Not just in performance, but in how quickly you can edit once you learn the commands. Your hands never leave the keyboard. No reaching for the mouse, no waiting for a GUI to load.
Vim is powerful. The modal editing approach might seem strange at first, but it's designed around how we actually work with text: navigating, selecting, editing, and repeating actions.
Let's start with the basics.
Opening files in Vim
Launch Vim from your terminal with:
vi text.txt
Or use the vim command if you have Vim installed (most systems alias vi to vim anyway):
vim text.txt
Replace text.txt with any filename. If the file doesn't exist, Vim creates it when you save.
You're now staring at Vim's interface. It looks minimal because it is. No toolbars, no menus, just your text and a command line at the bottom.
Understanding Vim's modes
This is where Vim differs from every other editor you've used. Vim operates in different modes, and each mode serves a specific purpose. Understanding modes is the key to understanding Vim.
Normal mode
This is where you start when you open Vim. In Normal mode, keys don't insert text. Instead, they execute commands. Think of it as command mode.
Press Esc any time to return to Normal mode from any other mode. When in doubt, hit Esc a couple times.
Insert mode
This is where you actually type and edit text like a traditional editor. To enter Insert mode from Normal mode, press i.
Your cursor changes (usually to a line or block), and you'll see -- INSERT -- at the bottom of the screen. Now you can type normally.
Press Esc to return to Normal mode.
Visual mode
Visual mode is for selecting text. Press v from Normal mode to enter Visual mode.
Your cursor becomes the start of a selection. Move the cursor to highlight text, then execute commands like copy or delete on the selection.
Press Esc to return to Normal mode.
The mode system seems weird initially, but it's incredibly efficient. You spend most of your time in Normal mode, navigating and executing commands. You briefly enter Insert mode to add text, then return to Normal mode. This workflow keeps your hands on the home row and eliminates repetitive mouse movements.
Navigating in Normal mode
Forget the arrow keys. Vim's navigation keys are faster because they keep your fingers on the home row.
Basic movement
These four keys are your primary navigation:
h- Move leftl- Move right (lowercase L)j- Move downk- Move up
At first, this feels unnatural. Your muscle memory screams for arrow keys. Resist the urge. After a few hours of practice, hjkl becomes second nature and you'll find yourself trying to use them in other applications.
Why these keys? They're on the home row, right under your right hand's fingers. Touch typists never need to move their hands.
Line navigation
Moving character by character is slow. These commands jump to key positions on a line:
0- Move to the beginning of the line (that's a zero)$- Move to the end of the line^- Move to the first non-whitespace character on the line
The $ key makes sense if you remember regular expressions, where $ represents the end of a line.
Word navigation
Jump between words instead of individual characters:
w- Move forward to the beginning of the next worde- Move forward to the end of the next wordb- Move backward to the beginning of the previous word
These commands are incredibly useful when editing code or prose. Combined with other commands, they become even more powerful.
File navigation
Jump to the top or bottom of a file instantly:
gg- Jump to the top of the fileG- Jump to the bottom of the file (capital G)
Combine G with a number to jump to a specific line. For example, 50G jumps to line 50.
You can also type :50 and press Enter to jump to line 50, which brings us to command mode navigation.
Advanced navigation tips
Here are some less obvious but incredibly useful navigation commands:
{- Jump to the previous paragraph or code block}- Jump to the next paragraph or code block%- Jump to matching bracket/parenthesis/braceCtrl + u- Scroll up half a pageCtrl + d- Scroll down half a pageCtrl + b- Scroll up a full pageCtrl + f- Scroll forward a full page
The % command is particularly useful when working with code. Place your cursor on an opening brace and press % to jump to its matching closing brace.
Editing in Normal mode
Navigation is only half the story. Vim's real power comes from editing commands in Normal mode.
Deleting text
x- Delete the character under the cursordd- Delete the entire current linedw- Delete from cursor to the end of the wordd$- Delete from cursor to the end of the lined0- Delete from cursor to the beginning of the line
Notice a pattern? The d key starts a delete command, and the second key specifies what to delete. This composability is central to Vim's design.
Copying and pasting
Vim calls copying "yanking," which explains the key choices:
yy- Yank (copy) the current lineyw- Yank from cursor to the end of the wordy$- Yank from cursor to the end of the linep- Paste after the cursorP- Paste before the cursor (capital P)
When you delete with dd, the deleted text goes into Vim's clipboard. So dd followed by p cuts and pastes a line.
Undo and redo
Everyone makes mistakes. Vim makes them easy to fix:
u- Undo the last changeCtrl + r- Redo (undo the undo)
You can press u multiple times to walk backward through your changes. Vim maintains a comprehensive undo tree, so you can undo dozens or even hundreds of changes.
Repeating commands
This is where Vim gets magical:
.- Repeat the last command
Made a change you want to apply elsewhere? Navigate to the new location and press . to repeat it. This works for deletions, changes, insertions, everything.
For example, if you delete a word with dw, move to another word, and press ., that word gets deleted too. This simple command saves countless keystrokes.
Working in Visual mode
Visual mode gives you precise control over text selection. Enter Visual mode by pressing v in Normal mode.
Selection types
Vim offers three types of visual selection:
v- Character-wise visual mode (select character by character)V- Line-wise visual mode (select entire lines)Ctrl + v- Block visual mode (select rectangular blocks)
In Visual mode, use the same navigation keys (hjkl, w, e, b, $, 0, etc.) to extend your selection.
Actions on selections
Once you've selected text, execute commands on the entire selection:
y- Yank (copy) the selected textd- Delete (cut) the selected textc- Change the selected text (deletes and enters Insert mode)>- Indent the selected lines<- Unindent the selected lines~- Toggle case of selected text
For example, to copy three lines: press V to enter line-wise visual mode, press jj to select two more lines (three total), then press y to yank them. Navigate elsewhere and press p to paste.
Searching and finding
Finding text is essential when working with large files.
Basic search
From Normal mode:
/search_term
Press /, type your search term, and press Enter. Vim highlights all matches and jumps to the first one.
n- Jump to the next matchN- Jump to the previous match (capital N)
To search backward through the file, use ? instead of /:
?search_term
Search tips
/\cpattern- Case-insensitive search (the\cflag)/pattern\>- Match only whole words*- Search for the word under the cursor#- Search backward for the word under the cursor
The * command is particularly handy. Place your cursor on a variable name and press * to find all occurrences.
Jumping to specific lines
Navigate to a line number by typing : followed by the line number:
:42
This jumps to line 42. Press Enter to execute.
You can also use 42G (capital G) to achieve the same result without entering command mode.
Saving and exiting
This is the famous part. How do you exit Vim? More importantly, how do you save your work?
All these commands start with : which enters Command-line mode. Type the command and press Enter.
Basic exit commands
:q- Quit (only works if no unsaved changes):q!- Quit without saving (force quit, discards changes):w- Write (save) the file:wq- Write and quit (save and exit):x- Write and quit (same as:wq, but only writes if changes were made)
The ! symbol forces a command. Use it carefully, especially with :q! since it discards all your unsaved work.
Save as a new file
:w newfile.txt
This saves the current buffer to a new file without closing the original.
Saving specific lines
:10,20w partial.txt
This saves lines 10 through 20 to a new file called partial.txt.
Combining commands for power
The real magic of Vim comes from combining commands. Most Vim commands follow a pattern:
[count] [operator] [motion]
Count is optional and specifies repetition. Operator is an action like delete or yank. Motion specifies what to operate on.
Examples of command composition
d2w- Delete two wordsy3j- Yank current line plus three lines belowc$- Change from cursor to end of lined4j- Delete four lines (current line plus three below)3dd- Delete three lines starting from current
Once you understand this pattern, you can construct powerful commands on the fly. Need to delete five words? d5w. Need to yank to the end of the paragraph? y}.
Essential settings and configuration
Vim is highly customizable through its configuration file. Create or edit ~/.vimrc to add these helpful settings:
# Enable line numbers set number # Enable syntax highlighting syntax on # Set tab width to 4 spaces set tabstop=4 set shiftwidth=4 set expandtab # Enable auto-indentation set autoindent set smartindent # Highlight search results set hlsearch # Enable incremental search set incsearch # Show matching brackets set showmatch # Enable mouse support set mouse=a
Save this file, and these settings apply every time you open Vim.
Common tasks and workflows
Let's put everything together with real-world examples.
Editing a configuration file
vim ~/.bashrc
- Navigate to the line you want to edit using
jandk - Press
ito enter Insert mode - Make your changes
- Press
Escto return to Normal mode - Type
:wqand press Enter to save and exit
Finding and replacing text
To replace all occurrences of "foo" with "bar":
:%s/foo/bar/g
Breaking this down:
:enters Command-line mode%means the entire filesis the substitute command/foo/bar/means replace "foo" with "bar"gmeans globally (all occurrences on each line)
Add c at the end for confirmation on each replacement:
:%s/foo/bar/gc
Deleting multiple lines
To delete lines 10 through 20:
:10,20d
Or navigate to line 10, press V to enter line-wise visual mode, press 10j to select 11 lines total, then press d to delete.
Copying content from another file
:r otherfile.txt
This reads the contents of otherfile.txt and inserts it below the current cursor position.
Common mistakes and how to avoid them
Typing commands in Insert mode. If you're in Insert mode and press j, you'll see the letter "j" appear instead of moving down. Always press Esc to ensure you're in Normal mode before executing commands.
Forgetting to save. Unlike modern editors that auto-save, Vim requires explicit saving. Get in the habit of typing :w frequently.
Using arrow keys. While arrow keys work in Vim, using them defeats the purpose of efficient, home-row-based editing. Force yourself to use hjkl for at least a week. It becomes natural.
Not understanding modes. Half of Vim confusion comes from being in the wrong mode. When something unexpected happens, press Esc a few times to reset to Normal mode.
Panicking when you can't exit. Take a breath, press Esc, type :q!, press Enter. You're out. Try again.
Practice exercises
The only way to get comfortable with Vim is to practice. Try these exercises:
Exercise 1: Basic navigation
- Open a file with at least 20 lines
- Jump to the top with
gg - Jump to the bottom with
G - Navigate to line 10 with
:10 - Move to the end of the line with
$ - Move to the beginning with
0
Exercise 2: Editing
- Delete three lines with
3dd - Undo with
u - Delete a word with
dw - Paste it elsewhere with
p - Copy a line with
yy - Paste it five times with
5p
Exercise 3: Visual mode
- Enter visual mode with
v - Select a paragraph using
}to jump to the end - Copy it with
y - Navigate somewhere else
- Paste with
p
Exercise 4: Search and replace
- Search for a common word with
/word - Jump through matches with
n - Replace all occurrences with
:%s/word/replacement/g
Vim resources for deeper learning
This guide covers the essentials, but Vim has much more to offer. When you're ready to go deeper:
Built-in tutorial: Run vimtutor in your terminal for an interactive 30-minute tutorial that comes with Vim.
Vim documentation: Type :help inside Vim to access comprehensive documentation. Try :help navigation or :help modes for specific topics.
Vim Adventures: A web-based game that teaches Vim commands through gameplay (vim-adventures.com).
Practice regularly: Use Vim for all your terminal-based editing. The more you use it, the more natural it becomes.
Wrapping up
Vim intimidates beginners because it works differently than every other editor. But that difference is exactly what makes it powerful. Modal editing, command composition, and keyboard-driven navigation enable a speed and efficiency that mouse-based editors can't match.
You don't need to master Vim overnight. Start with the basics: navigating with hjkl, entering Insert mode with i, saving with :w, and exiting with :q. As these become muscle memory, gradually add more commands to your repertoire.
Before long, you'll find yourself reaching for Vim even when you have other options. Because once you experience the flow of modal editing, everything else feels slow.
Keep this guide bookmarked, practice daily, and soon you'll be editing at the speed of thought.