VI Essential Commands - The 80/20 Rule
Welcome to Efficiency
Now that you know the basics, it's time to learn the commands that will make you truly productive. The Pareto Principle applies perfectly to VI: 20% of the commands handle 80% of your editing tasks.
This tutorial focuses on those high-value commands that you'll use constantly. Master these, and you'll be faster than most GUI editor users.
The VI Philosophy: Keep your fingers on the home row. Every moment spent reaching for the mouse or arrow keys is wasted time. VI's power comes from never leaving the keyboard.
The Power of Combining Operators and Motions
This is where VI becomes truly powerful. Commands follow a simple grammar:
[number] [operator] [motion]
Operators (Actions)
| Operator |
Action |
d |
Delete |
c |
Change (delete and enter Insert Mode) |
y |
Yank (copy) |
> |
Indent right |
<:244
|
Indent left |
Motions (Where to Apply the Action)
| Motion |
Meaning |
w |
Word forward |
b |
Word backward |
e |
End of word |
$ |
End of line |
0 |
Beginning of line |
^ |
First non-blank character |
G |
End of file |
gg |
Beginning of file |
Powerful Combinations
dw # Delete word
d3w # Delete 3 words
d$ # Delete to end of line
d0 # Delete to beginning of line
dG # Delete to end of file
dgg # Delete to beginning of file
cw # Change word
c$ # Change to end of line
c3w # Change 3 words
y$ # Yank to end of line
yG # Yank to end of file
y3j # Yank current line and 3 below
<3j # Indent current line and 3 below
>4k # Unindent current line and 4 above
Think in VI Grammar: "I want to delete 3 words" = d3w
"I want to change to the end of the line" = c$
"I want to yank 5 lines down" = y5j
TRY IT NOW: Create a file with several lines. Practice: d3w to delete 3 words, c$ to change to end of line, y2j to yank current and 2 lines below, then p to paste.
Text Objects - The Secret Weapon
Text objects let you operate on logical units like words, sentences, paragraphs, and blocks. This is one of VI's most powerful features.
Text Object Syntax
[operator] [i or a] [object]
i means "inner" (excludes delimiters), a means "around" (includes delimiters)
Common Text Objects
| Object |
Meaning |
w |
Word |
s |
Sentence |
p |
Paragraph |
" ' ` |
Quoted strings |
( ) { } [ ] |
Blocks enclosed by these characters |
< > |
Tag blocks (HTML/XML) |
t |
Tag (HTML/XML) |
Powerful Text Object Commands
diw # Delete inner word (cursor anywhere in word)
daw # Delete around word (includes trailing space)
ciw # Change inner word
caw # Change around word
di" # Delete inside quotes
da" # Delete around quotes (includes quotes)
ci" # Change inside quotes
di( # Delete inside parentheses
da( # Delete around parentheses (includes parens)
ci{ # Change inside braces
da{ # Delete around braces
dis # Delete inner sentence
das # Delete around sentence
dip # Delete inner paragraph
dap # Delete around paragraph
dit # Delete inner tag (HTML)
dat # Delete around tag (includes tags)
cit # Change inner tag content
❌ The Slow Way:
Position cursor at start of word, v for visual, move to end, d to delete
✅ The Fast Way:
Cursor anywhere in word, diw
❌ The Slow Way:
Find opening quote, move cursor, delete to closing quote
✅ The Fast Way:
Cursor anywhere in quoted text, di"
TRY IT NOW: Create a file with text like: The "quick brown fox" jumps over (the lazy dog).
Position cursor inside "quick" and type ciw, type "fast", ESC.
Position inside quotes and type di".
Position inside parentheses and type ci(, type new text.
Essential Line Operations
These commands work on entire lines and are incredibly fast.
| Command |
Action |
dd |
Delete line |
yy or Y |
Yank line |
cc or S |
Change line |
>> |
Indent line |
<< |
Unindent line |
J |
Join current line with next (remove newline) |
== |
Auto-indent line (vim) |
With Counts
3dd # Delete 3 lines
5yy # Yank 5 lines
4>> # Indent 4 lines
3J # Join next 3 lines with current
PRO TIP: dd is actually "cut" not just delete. The deleted line goes into a buffer and can be pasted with p. So dd followed by p moves a line down, and ddp is a fast way to swap two lines!
Efficient Character and Small Change Operations
| Command |
Action |
x |
Delete character under cursor |
X |
Delete character before cursor |
r |
Replace single character (stay in Command Mode) |
R |
Enter Replace Mode (overwrite text) |
s |
Substitute character (delete and enter Insert Mode) |
~ |
Toggle case of character |
. |
Repeat last change (EXTREMELY USEFUL!) |
The Magic Dot Command
The . command repeats your last change. This is one of the most powerful efficiency tools in VI.
# Example: Delete a word with dw, then move to next word and press . to delete it too
dw # Delete word
w # Move to next word
. # Repeat the delete
# Example: Change "foo" to "bar" multiple times
ciw # Change inner word
bar # Type new word
ESC # Back to Command Mode
/foo # Find next "foo"
n # Next occurrence
. # Repeat the change (changes to "bar")
n. # Find next and change
n. # Find next and change
Efficiency Pattern: Make a change, move, press dot. This is faster than search and replace for many tasks!
TRY IT NOW: Create text with the word "test" appearing 5 times. Type ciw, change to "demo", ESC. Then use /test to find next occurrence, press n to find, press . to repeat the change. Repeat n. pattern.
Search and Replace - The Essential Patterns
While we covered basic search, here are the patterns you'll use constantly.
Basic 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 entire file
:%s/old/new/gc # Replace all with confirmation
:5,12s/old/new/g # Replace in lines 5-12
:.,$s/old/new/g # Replace from current line to end
Common Search Patterns
/\<word\> # Search for exact word (not as part of another word)
/^start # Search for lines starting with "start"
/end$ # Search for lines ending with "end"
/\d\+ # Search for one or more digits (vim)
/[Tt]est # Search for "Test" or "test"
/.* # Search for any character sequence
Search Flags
:%s/old/new/g # g = global (all occurrences on each line)
:%s/old/new/gi # i = case insensitive
:%s/old/new/gc # c = confirm each replacement
:%s/old/new/gI # I = case sensitive (override ignorecase setting)
PRACTICE EXERCISE:
- Create a file with multiple instances of "foo" and "Foo"
- Try:
:%s/foo/bar/g (replaces only lowercase)
- Undo:
u
- Try:
:%s/foo/bar/gi (replaces all, case insensitive)
- Undo:
u
- Try:
:%s/foo/bar/gc (asks for confirmation)
Working with Multiple Lines
Visual mode makes it easy to work with multiple lines at once.
Visual Modes
| Command |
Mode |
v |
Character-wise visual mode |
V |
Line-wise visual mode |
Ctrl+v |
Block visual mode (vim) |
Common Visual Mode Operations
V # Enter line-wise visual mode
5j # Select 5 lines down
d # Delete selected lines
V # Enter line-wise visual mode
3j # Select 3 lines
> # Indent selected lines
v # Enter character-wise visual
3w # Select 3 words
y # Yank selection
Ctrl+v # Block visual mode
5j # Down 5 lines
3l # Right 3 characters
I # Insert at start of block
# (type text, then ESC to apply to all lines)
PRO TIP: Visual mode is great for learning, but experienced users often skip it. For example, d5j deletes 5 lines down without entering visual mode, which is faster.
TRY IT NOW: Create a file with 10 lines. Press V, select 5 lines with 4j, press > to indent. Press u to undo. Now try >4j (without visual mode) - same result, fewer keystrokes!
Essential File Operations
| Command |
Action |
:w |
Save file |
:w filename |
Save as filename |
:w! |
Force write (override readonly) |
:q |
Quit |
:q! |
Quit without saving |
:wq or ZZ |
Save and quit |
:x |
Save (if modified) and quit |
:e filename |
Edit another file |
:e! |
Reload file (discard changes) |
:e# |
Edit alternate file (toggle between two files) |
:r filename |
Read file and insert below cursor |
:r !command |
Read command output and insert below cursor |
:w !sudo tee % # Save file with sudo (when you forgot to use sudo)
:r !date # Insert current date/time
:r !ls -la # Insert directory listing
:r /etc/hosts # Insert contents of /etc/hosts
Sysadmin Lifesaver: :w !sudo tee % - Use this when you opened a root-owned file without sudo and made changes. It saves the file through sudo without having to quit and lose your work!
Marks and Jumping
Marks let you save positions and jump back to them instantly.
Setting and Using Marks
| Command |
Action |
ma |
Set mark 'a' at current position |
'a |
Jump to line of mark 'a' |
`a |
Jump to exact position of mark 'a' |
:marks |
List all marks |
Special Marks (Automatic)
| Mark |
Meaning |
'' |
Previous position (toggle back and forth) |
`` |
Position before last jump |
'. |
Last change |
'^ |
Last insert |
Jump Commands
Ctrl+o # Jump to older position in jump list
Ctrl+i # Jump to newer position in jump list
% # Jump to matching bracket/paren/brace
* # Search forward for word under cursor
# # Search backward for word under cursor
PRO TIP: Use ma to set mark 'a' before making a big jump (like searching or going to end of file). Then use 'a to jump back instantly!
TRY IT NOW: Open a long file. Press ma to set mark. Jump to end with G. Jump back with 'a. Try '' to toggle between the two positions.
Essential Workflow Patterns
Here are the most common editing patterns you'll use every day:
Pattern 1: Quick Navigation Edit
# Fix a typo you see on screen
/typo # Search for it
cw # Change the word
(type correction)
ESC # Back to command mode
'' # Jump back to where you were
Pattern 2: Change Multiple Occurrences
* # Search for word under cursor
cw # Change it
(type new word)
ESC
n # Next occurrence
. # Repeat the change
n. # Next and change
n. # Repeat...
Pattern 3: Reorganize Lines
dd # Cut line
5j # Move down 5 lines
p # Paste below
# Or move multiple lines:
3dd # Cut 3 lines
/pattern # Find where to put them
p # Paste
Pattern 4: Duplicate Lines
yy # Yank line
p # Paste below (duplicate)
# Or duplicate multiple:
3yy # Yank 3 lines
j # Move down
p # Paste
# Quick duplicate current line:
yyp # Yank and paste (fast duplicate)
Pattern 5: Indent/Unindent Block
V # Visual line mode
5j # Select 5 lines
> # Indent
. # Repeat (indent more)
. # Again...
# Or without visual mode:
>5j # Indent 5 lines down
>> # Indent current line
Common Configuration Settings
These settings can be added to your ~/.exrc or ~/.vimrc file, or set during a session with :set
:set number # Show line numbers (nu)
:set nonumber # Hide line numbers (nonu)
:set ignorecase # Case-insensitive search (ic)
:set smartcase # Case-sensitive if capital in search (scs)
:set hlsearch # Highlight search results (hls)
:set incsearch # Incremental search (is)
:set autoindent # Auto-indent new lines (ai)
:set tabstop=4 # Tab width (ts)
:set shiftwidth=4 # Indent width (sw)
:set expandtab # Use spaces instead of tabs (et)
:set showmatch # Highlight matching brackets (sm)
:syntax on # Enable syntax highlighting (vim)
# Quick settings during session:
:set nu # Abbreviation for number
:set nonu # Turn off numbers
:set ic # Abbreviation for ignorecase
:noh # Turn off search highlighting temporarily
Your Daily Driving Commands
If you master just these commands, you'll be more efficient than most VI users:
The Essential 20:
i, a, o, O - Enter insert mode
ESC - Return to command mode
h j k l - Navigate (or use arrow keys)
w, b - Move by word
0, $ - Start/end of line
G, gg - End/start of file
dd - Delete line
yy - Yank line
p, P - Paste
u - Undo
. - Repeat last change
/pattern - Search
n, N - Next/previous search
ciw - Change inner word
di" - Delete inside quotes
* - Search word under cursor
% - Jump to matching bracket
:w - Save
:wq or ZZ - Save and quit
:q! - Quit without saving
Real-World Sysadmin Scenarios
SCENARIO 1: Edit SSH Config
vi ~/.ssh/config
G # Go to end
o # New line
(type new host entry)
ESC
:wq # Save and exit
SCENARIO 2: Clean Up Log File
vi /var/log/application.log
:%s/^DEBUG.*//g # Remove all DEBUG lines
:g/^$/d # Delete empty lines
:wq
SCENARIO 3: Duplicate and Modify Config Section
/<VirtualHost # Find section
V # Visual line mode
/<\/VirtualHost # Select to closing tag
y # Yank selection
p # Paste below
/ServerName # Find ServerName in new section
cw # Change word
(type new name)
ESC
:wq
SCENARIO 4: Comment Out Multiple Lines
:5,10s/^/# / # Add # to start of lines 5-10
# Or with visual mode:
Ctrl+v # Block visual
9j # Select 10 lines
I # Insert at start
# # Type comment char
ESC # Apply to all lines
Speed Challenge
ULTIMATE PRACTICE:
- Create a file with 50 lines of text (use
:r !seq 1 50 to generate)
- Delete lines 10-15 in one command:
:10,15d
- Go to line 25:
25G
- Change the number to "MIDDLE":
ciw MIDDLE ESC
- Duplicate that line 3 times:
3yy then 3p
- Search for "30":
/30
- Change it to "THIRTY":
cw THIRTY ESC
- Find all instances of digits and replace with "NUM":
:%s/\d\+/NUM/g
- Undo everything:
u u u u u (or hold u)
- Quit without saving:
:q!
Time yourself. With practice, this should take under 30 seconds!
What's Next?
You now know the commands that handle 80% of your editing tasks. You can:
- Use operator-motion combinations fluently
- Work with text objects efficiently
- Use the magic dot command to repeat changes
- Search and replace powerfully
- Navigate and edit at high speed
- Handle real-world sysadmin tasks
The next tutorial covers intermediate topics: buffers, marks in depth, macros, and advanced search/replace techniques.
Created for Linux System Administrators | Part 2 of the VI Learning Series
Practice daily, and these commands will become muscle memory!
← Back to VI Index
↑ Back to EXPANDED