vim
vim#
Concepts#
Why vim?#
vim (Vi Improved) is the most widely available text editor on Unix/Linux systems. Some form of vi exists on virtually every Linux installation, even minimal server images where nano is not installed. vim is extremely powerful once learned, but has a steep learning curve because it works fundamentally differently from editors you may be used to.
Learning vim is not mandatory for this course — nano is sufficient for editing files. But understanding vim basics will serve you well when you encounter a system where it is the only available editor.
The Key Concept: Modes#
Unlike nano (where you just type), vim has modes. Each mode interprets your keystrokes differently:
| Mode | Purpose | How to Enter |
|---|---|---|
| Normal | Navigate, delete, copy, paste, run commands | Esc (always returns here) |
| Insert | Type text (like a normal editor) | i, a, o, I, A, O |
| Visual | Select text | v (character), V (line), Ctrl+V (block) |
| Command | Run commands (save, quit, search, replace) | : from Normal mode |
Normal mode is the default. When you open vim, you are in Normal mode. Your keystrokes are commands, not text. This is the #1 source of confusion for beginners.
The most important rule: When in doubt, press Esc — it always takes you back to Normal mode.
Starting and Quitting vim#
vim filename.txt # open a file
vim +15 filename.txt # open and jump to line 15
vim -R filename.txt # open in read-only mode
How to Quit#
You don’t. Once vim is opened it will extract a toll from your soul.
From Normal mode (press Esc first):
| Command | Action |
|---|---|
:q |
Quit (fails if there are unsaved changes) |
:q! |
Quit without saving (force quit) |
:w |
Save (write) |
:wq |
Save and quit |
:x |
Save and quit (same as :wq, slightly shorter) |
ZZ |
Save and quit (shortcut, no : needed) |
ZQ |
Quit without saving (shortcut) |
Normal Mode — Navigation#
In Normal mode, you navigate without arrow keys (though arrow keys also work):
Basic Movement#
| Key | Action |
|---|---|
h |
Left |
j |
Down |
k |
Up |
l |
Right |
w |
Jump forward to start of next word |
b |
Jump backward to start of previous word |
e |
Jump to end of current word |
0 |
Go to beginning of line |
$ |
Go to end of line |
^ |
Go to first non-blank character of line |
Larger Movements#
| Key | Action |
|---|---|
gg |
Go to first line of file |
G |
Go to last line of file |
42G or :42 |
Go to line 42 |
Ctrl + f |
Page forward (down) |
Ctrl + b |
Page backward (up) |
Ctrl + d |
Half page down |
Ctrl + u |
Half page up |
{ |
Previous paragraph (empty line) |
} |
Next paragraph |
Entering Insert Mode#
From Normal mode:
| Key | Action |
|---|---|
i |
Insert before cursor |
I |
Insert at beginning of line |
a |
Append after cursor |
A |
Append at end of line |
o |
Open new line below and insert |
O |
Open new line above and insert |
Press Esc to return to Normal mode when done typing.
Normal Mode — Editing#
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 |
d0 |
Delete from cursor to beginning of line |
3dd |
Delete 3 lines |
Copying (Yanking) and Pasting#
| Command | Action |
|---|---|
yy |
Yank (copy) entire line |
yw |
Yank word |
y$ |
Yank to end of line |
3yy |
Yank 3 lines |
p |
Paste after cursor |
P |
Paste before cursor |
Undo and Redo#
| Command | Action |
|---|---|
u |
Undo |
Ctrl + r |
Redo |
. |
Repeat last command |
vim’s Language: Verb + Count + Motion#
vim commands follow a grammar: verb + count (optional) + motion.
| Verb | Meaning |
|---|---|
d |
Delete |
y |
Yank (copy) |
c |
Change (delete and enter insert mode) |
| Motion | Meaning |
|---|---|
w |
Word |
$ |
End of line |
0 |
Beginning of line |
G |
End of file |
gg |
Beginning of file |
} |
Next paragraph |
Examples:
d3w— delete 3 wordsy$— yank to end of linec2w— change (replace) 2 wordsdG— delete from cursor to end of filed5j— delete 5 lines down
Search and Replace#
Search#
| Command | Action |
|---|---|
/pattern |
Search forward for “pattern” |
?pattern |
Search backward |
n |
Next match |
N |
Previous match |
* |
Search for word under cursor (forward) |
# |
Search for word under cursor (backward) |
Search and Replace#
:s/old/new/ " Replace first occurrence on current line
:s/old/new/g " Replace all occurrences on current line
:%s/old/new/g " Replace all occurrences in file
:%s/old/new/gc " Replace all with confirmation (y/n each time)
:10,20s/old/new/g " Replace in lines 10-20
Visual Mode — Selecting Text#
From Normal mode:
| Key | Action |
|---|---|
v |
Character-wise visual selection |
V |
Line-wise visual selection |
Ctrl + v |
Block (column) visual selection |
After selecting, apply an action:
d— delete selectiony— yank selection>— indent selection<— unindent selection:— apply a command to the selection
Useful Commands#
| Command | Action |
|---|---|
:set number |
Show line numbers |
:set nonumber |
Hide line numbers |
:set paste |
Paste mode (disables auto-indent) |
:set nopaste |
Exit paste mode |
:!command |
Run a shell command without leaving vim |
:r filename |
Insert the contents of a file |
:r !command |
Insert the output of a command |
Basic .vimrc Configuration#
Create ~/.vimrc to customize vim:
" Show line numbers
set number
" Highlight search results
set hlsearch
" Incremental search (highlight as you type)
set incsearch
" Ignore case in search (unless uppercase is used)
set ignorecase
set smartcase
" Tab settings
set tabstop=4
set shiftwidth=4
set expandtab
" Auto-indentation
set autoindent
set smartindent
" Show current mode
set showmode
" Show matching brackets
set showmatch
" Enable syntax highlighting
syntax on
" Enable mouse
set mouse=a
Lab#
Exercise 1: Open, Insert, Save, Quit#
# Open (or create) a file
vim ~/vim_practice.txt
# You are in Normal mode. Press i to enter Insert mode.
# Type: "This is my first vim edit."
# Press Esc to return to Normal mode.
# Type :wq and press Enter to save and quit.
# Verify
cat ~/vim_practice.txt
Exercise 2: Navigation#
# Open a large file
vim /etc/services
# Practice Normal mode navigation:
# gg — go to top of file
# G — go to bottom of file
# :50 — go to line 50
# w — move forward by word (do it several times)
# b — move backward by word
# 0 — go to beginning of line
# $ — go to end of line
# Ctrl+f — page down
# Ctrl+b — page up
# Quit without saving: :q!
Exercise 3: Editing#
vim ~/vim_practice.txt
# Go to the end of the file: G
# Open a new line below: o
# Type: "Line added with o"
# Esc
# Go to the beginning: gg
# Open a line above: O
# Type: "Line added at the top"
# Esc
# Go to any word, type dw to delete it
# Type u to undo
# Type dd to delete the whole line
# Type u to undo
# Type yy to yank (copy) a line
# Type p to paste it below
# Save and quit: :wq
Exercise 4: Search and Replace#
vim ~/vim_practice.txt
# Add some content: press o for each new line, Esc between them
# Add these lines:
# The quick brown fox
# jumps over the lazy dog
# The fox is quick
# Search for "fox": /fox then Enter
# Press n to go to next occurrence
# Press N to go back
# Replace "fox" with "cat" everywhere:
# :%s/fox/cat/gc
# Press y or n for each occurrence
# Save: :w
# Quit: :q
Exercise 5: Visual Mode#
vim ~/vim_practice.txt
# Move to a line
# Press V to select the whole line (visual line mode)
# Move down with j to select more lines
# Press d to delete the selection
# Press u to undo
# Try character visual mode:
# Press v, move with w or arrow keys to select characters
# Press y to yank (copy) the selection
# Move somewhere and press p to paste
# :wq to save and quit
Exercise 6: Create Your .vimrc#
vim ~/.vimrc
# Press i for insert mode, then type:
set number
set hlsearch
set incsearch
set ignorecase
set smartcase
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set showmode
set showmatch
syntax on
# Esc, then :wq
# Now open any file — you should see line numbers and syntax highlighting
vim ~/vim_practice.txt
# :q to exit
Exercise 7: vim Tutor#
vim includes a built-in tutorial:
vimtutor
This is a 30-minute interactive tutorial. It is the single best way to learn vim. Work through it at your own pace.
Clean Up#
rm -f ~/vim_practice.txt
Review#
1. What are vim's main modes?
Normal (navigation and commands), Insert (typing text), Visual (selecting text), and Command (: commands for saving, quitting, searching).
2. How do you exit vim without saving?
Press Esc (to ensure you are in Normal mode), then type :q! and press Enter.
3. How do you enter Insert mode?
From Normal mode, press i (insert before cursor), a (append after cursor), or o (open new line below). There are several others (I, A, O). Press Esc to return to Normal mode.
4. How do you delete a line in vim?
In Normal mode, press dd. To delete 5 lines: 5dd.
5. How do you search and replace all occurrences in a file?
:%s/old/new/g — this replaces all occurrences. Add c for confirmation: :%s/old/new/gc.
6. How do you undo and redo in vim?
u to undo, Ctrl + r to redo. Both work in Normal mode.
7. What is the "vim language" for commands?
Commands follow a verb + count + motion pattern. For example: d3w = delete 3 words, y$ = yank to end of line, c2w = change 2 words. This composable grammar makes vim very powerful.
Previous: nano | Next: Streams, Redirection, and Pipes