Why Learn Vim?#
Vim is installed on virtually every Linux server, container, and Unix-like system. When you SSH into a production box, edit a crontab, or resolve a git merge conflict — Vim (or its predecessor vi) is there.
You don’t need to make it your primary editor to benefit from it. Knowing the basics means you’re never stuck on a remote machine without a way to edit files.
Opening and Closing Files#
# Open a file
vim file.txt
# Open at a specific line
vim +42 file.txt
# Open multiple files
vim file1.txt file2.txtQuitting Vim#
The commands everyone searches for first:
| Command | Action |
|---|---|
:q | Quit (fails if unsaved changes) |
:q! | Quit without saving |
:w | Save (write) |
:wq | Save and quit |
ZZ | Save and quit (shortcut) |
ZQ | Quit without saving (shortcut) |
Understanding Modes#
Vim is a modal editor — the same keys do different things depending on which mode you’re in. This is what makes it confusing at first and powerful once it clicks.
| Mode | Purpose | How to enter |
|---|---|---|
| Normal | Navigate and manipulate text | Esc (default mode) |
| Insert | Type text | i, a, o, etc. |
| Visual | Select text | v, V, Ctrl-v |
| Command | Run commands | : |
The key rule: Press Esc to go back to Normal mode. When in doubt, hit Esc.
Normal Mode Movement#
In Normal mode, you navigate without touching the mouse. Start with these:
Character and line movement#
| Key | Movement |
|---|---|
h | Left |
j | Down |
k | Up |
l | Right |
These replace arrow keys. Your fingers stay on the home row.
Word movement#
| Key | Movement |
|---|---|
w | Forward to start of next word |
b | Back to start of current/previous word |
e | Forward to end of current/next word |
Line movement#
| Key | Movement |
|---|---|
0 | Beginning of line |
^ | First non-blank character |
$ | End of line |
File movement#
| Key | Movement |
|---|---|
gg | Top of file |
G | Bottom of file |
42G | Go to line 42 |
Ctrl-d | Scroll down half a screen |
Ctrl-u | Scroll up half a screen |
Entering Insert Mode#
From Normal mode, these keys put you into Insert mode at different positions:
| Key | Inserts at |
|---|---|
i | Before the cursor |
a | After the cursor |
I | Beginning of the line |
A | End of the line |
o | New line below |
O | New line above |
Type your text, then press Esc to return to Normal mode.
Basic Editing in Normal Mode#
You don’t need to enter Insert mode for every edit. These commands work directly in Normal mode:
Deleting#
| Command | Action |
|---|---|
x | Delete character under cursor |
dd | Delete entire line |
dw | Delete from cursor to start of next word |
d$ or D | Delete from cursor to end of line |
Copying (yanking) and pasting#
| Command | Action |
|---|---|
yy | Yank (copy) entire line |
yw | Yank from cursor to start of next word |
p | Paste after cursor |
P | Paste before cursor |
Undo and redo#
| Command | Action |
|---|---|
u | Undo last change |
Ctrl-r | Redo |
Other essentials#
| Command | Action |
|---|---|
r | Replace single character under cursor |
J | Join current line with the line below |
~ | Toggle case of character |
. | Repeat last change |
Visual Mode (Selecting Text)#
Visual mode lets you select text and then act on the selection:
| Key | Selection type |
|---|---|
v | Character-wise selection |
V | Line-wise selection |
Ctrl-v | Block (column) selection |
Once text is selected, you can:
d— delete the selectiony— yank the selection>— indent<— unindent
# Example workflow: delete 3 lines
V (enter visual line mode)
jj (select two more lines)
d (delete selection)Putting It Together#
Here are some common tasks using what you’ve learned:
Delete a word and type a new one#
dw (delete word)
i (enter insert mode)
...type replacement...
Esc (back to normal mode)Copy a line and paste it below#
yy (yank line)
p (paste below)Delete everything from the cursor to the end of the file#
dGMove to line 10 and delete it#
10G (go to line 10)
dd (delete line)Open a file, add a line at the end, save and quit#
vim file.txtG (go to end of file)
o (new line below, enter insert mode)
...type your text...
Esc (back to normal mode)
:wq (save and quit)Best Practices#
- Stay in Normal mode by default — only enter Insert mode when you’re actively typing new text
- Use
uliberally — Vim has unlimited undo, so experiment without fear - Practice
hjklinstead of arrow keys — it’s awkward for a day, then faster forever - Learn the dot command (
.) early — it repeats your last edit and saves enormous time - Run
vimtutorfrom your terminal for an interactive 30-minute tutorial built into Vim

