The Magic of Linux Text Editors: vi/vim and sed for Editing Like a Pro
The Problem: Editing Files on Remote Servers
You SSH into a server. Need to edit a config file. No GUI. No VS Code. Just the terminal.
You open the file with vi and... nothing works. You're stuck. Can't exit. Can't save. Panic sets in.
Or you need to replace a word in 100 files. Opening each one manually? That'll take hours.
This is why vi/vim and sed exist. One for interactive editing, one for automated text transformation.
Understanding vi/vim
vi (visual editor) is on every Linux system. vim (vi improved) is the enhanced version with more features.
Two Modes: The Key Concept
vi/vim has two modes:
- Command Mode (default) - Navigate, delete, copy, search
- Insert Mode - Type text normally
This confuses beginners, but it's powerful once you understand it.
Starting vi/vim
vi filename # Opens file in vi
vim filename # Opens file in vim
You start in Command Mode.
Insert Mode: Adding Text
From Command Mode, press:
Insert at cursor position
i # Insert mode at cursor
Insert with space
a # Insert mode after cursor (adds space)
New line and insert
o # New line below, enter insert mode
O # New line above, enter insert mode (capital O)
Press ESC to return to Command Mode.
Command Mode: Navigation and Editing
Basic Movement
h # Left
j # Down
k # Up
l # Right
# Or use arrow keys
Deleting
x # Delete character under cursor
dd # Delete entire line
dw # Delete word
D # Delete from cursor to end of line
Undo
u # Undo last change
Ctrl+r # Redo
Replace
r # Replace single character (type new char)
R # Replace mode (keep typing to replace multiple chars)
Note: r can be unreliable for multiple characters. Use R for replacing continuously or use search/replace.
Search
/keyword # Search forward for "keyword"
?keyword # Search backward for "keyword"
n # Next occurrence
N # Previous occurrence
Copy and Paste
yy # Copy (yank) current line
p # Paste below cursor
P # Paste above cursor (capital P)
# Copy multiple lines
3yy # Copy 3 lines
Save and Exit
:w # Save (write)
:q # Quit (only if no changes)
:wq # Save and quit
:wq! # Save and quit (force)
:q! # Quit without saving (discard changes)
# Shortcut for save and quit
Shift+ZZ # Same as :wq
Important: Type these commands only in Command Mode (press ESC first).
Search and Replace in vim
While in Command Mode:
:%s/old/new/g # Replace all occurrences in file
:%s/old/new/gc # Replace all, with confirmation
:s/old/new/g # Replace in current line only
Breaking down :%s/old/new/g:
-
:- Enter command -
%- Apply to entire file -
s- Substitute -
/old/new/- Replace "old" with "new" -
/g- Global (all occurrences on each line)
Replace on Specific Lines
:1,10s/old/new/g # Lines 1-10
:5s/old/new/g # Line 5 only
Example: Change Variable Name
# Replace "userName" with "userId" everywhere
:%s/userName/userId/g
vi vs vim
vi - Original editor, basic features, always available
vim - Enhanced version with:
- Syntax highlighting
- Multiple undo levels
- Split windows
- Plugins
- Better search/replace
Check out openvim.com for interactive vim tutorial.
Most systems have vim. If not:
sudo apt install vim # Ubuntu/Debian
sudo yum install vim # CentOS/RHEL
The sed Command: Stream Editor
sed (stream editor) transforms text non-interactively. Perfect for scripts and bulk operations.
Basic Syntax
sed 'command' filename
Find and Replace
# Replace first occurrence per line
sed 's/Name/NewName/' filename
# Replace all occurrences (global)
sed 's/Name/NewName/g' filename
# Replace and save to file (in-place)
sed -i 's/Name/NewName/g' filename
Important:
- Without
-i, output goes to screen (original file unchanged) - With
-i, modifies file directly -
/gflag makes it global (all occurrences on each line)
Delete Text
# Delete a specific word
sed 's/Name//g' filename
# Delete lines containing keyword
sed '/keyword/d' filename
# Delete empty lines
sed '/^$/d' filename
# Delete first two lines
sed '1,2d' filename
# Delete lines 5-10
sed '5,10d' filename
Replace Special Characters
# Replace tabs with spaces
sed -i 's/\t/ /g' filename
# Replace spaces with tabs
sed -i 's/ /\t/g' filename
Display Specific Lines
# Show lines 12-18
sed -n '12,18p' filename
# Show all except lines 12-18
sed '12,18d' filename
# Show first 10 lines (like head)
sed -n '1,10p' filename
Add Blank Lines
# Add blank line after each line
sed G filename
# Double-space the file
sed G filename > newfile.txt
Advanced Replace: Skip Lines
# Replace in all lines EXCEPT line 1
sed '1!s/word/newword/g' filename
# Replace in lines 10-20 only
sed '10,20s/word/newword/g' filename
Real-World Scenarios
Scenario 1: Update Configuration
You need to change a port number in a config file:
# Check current value
grep "port" /etc/app/config.yml
# Replace with sed
sed -i 's/port: 8080/port: 3000/g' /etc/app/config.yml
# Verify change
grep "port" /etc/app/config.yml
Scenario 2: Clean Log Files
Remove empty lines and lines with "DEBUG":
# Remove empty lines
sed -i '/^$/d' app.log
# Remove DEBUG lines
sed -i '/DEBUG/d' app.log
# Or chain them
sed -i '/^$/d; /DEBUG/d' app.log
Scenario 3: Bulk File Updates
Replace API endpoint in all JavaScript files:
# Find all .js files and replace
find . -name "*.js" -exec sed -i 's/api.old.com/api.new.com/g' {} \;
Scenario 4: Edit Config on Server
SSH into server and edit nginx config:
ssh user@server
sudo vim /etc/nginx/nginx.conf
# Press i to enter insert mode
# Make changes
# Press ESC
# Type :wq to save and exit
Scenario 5: Remove Comments
Remove lines starting with # (comments):
sed -i '/^#/d' script.sh
Combining vi/vim and sed
Use vi/vim for interactive editing. Use sed for automated changes.
When to Use vim
- Editing single files
- Need to see context
- Making multiple different changes
- Interactive work
When to Use sed
- Batch processing multiple files
- Scripted changes
- Simple find/replace operations
- Automated deployments
Example Workflow
# 1. Check what needs changing
grep "old_value" *.conf
# 2. Test sed command (without -i)
sed 's/old_value/new_value/g' config.conf
# 3. Apply to all files
sed -i 's/old_value/new_value/g' *.conf
# 4. Verify one file in vim if needed
vim config.conf
Quick Reference
vim Commands
| Command | Action |
|---|---|
i |
Insert mode at cursor |
a |
Insert after cursor |
o |
New line below |
ESC |
Back to command mode |
x |
Delete character |
dd |
Delete line |
u |
Undo |
r |
Replace character |
/keyword |
Search |
yy |
Copy line |
p |
Paste |
:w |
Save |
:q |
Quit |
:wq |
Save and quit |
:q! |
Quit without saving |
Shift+ZZ |
Save and quit |
:%s/old/new/g |
Replace all |
sed Commands
| Command | Action |
|---|---|
sed 's/old/new/g' file |
Replace all |
sed -i 's/old/new/g' file |
Replace in-place |
sed '/keyword/d' file |
Delete lines with keyword |
sed '/^$/d' file |
Delete empty lines |
sed '1,5d' file |
Delete lines 1-5 |
sed -n '10,20p' file |
Show lines 10-20 |
sed 's/\t/ /g' file |
Tabs to spaces |
sed G file |
Double-space |
Common Mistakes
Mistake #1: Forgetting to exit insert mode
You're typing commands but they appear as text. Press ESC first.
Mistake #2: Using sed without testing
# Wrong - modifies file immediately
sed -i 's/old/new/g' important_file.txt
# Right - test first
sed 's/old/new/g' important_file.txt
# Check output, then add -i
Mistake #3: Not escaping special characters
# Wrong
sed 's/http://old.com/http://new.com/g' file
# Right - escape forward slashes or use different delimiter
sed 's|http://old.com|http://new.com|g' file
Mistake #4: Forgetting the /g flag
# Only replaces first occurrence per line
sed 's/old/new/' file
# Replaces all occurrences
sed 's/old/new/g' file
Tips for Efficiency
Tip 1: Create backups before sed -i
# Create backup with .bak extension
sed -i.bak 's/old/new/g' file.txt
Tip 2: Use vim for learning, sed for automation
Learn editing in vim interactively. Once you know what to change, automate with sed.
Tip 3: Chain sed commands
# Multiple operations at once
sed -i '/^$/d; /DEBUG/d; s/old/new/g' file.txt
Tip 4: Vim line numbers
# In vim command mode
:set number # Show line numbers
:set nonumber # Hide line numbers
Practical Examples
Example 1: Update Database Connection
# Old: localhost
# New: db.server.com
sed -i 's/localhost/db.server.com/g' config.php
Example 2: Change All Ports
# Update port 8080 to 3000 in all configs
find /etc/app -name "*.conf" -exec sed -i 's/:8080/:3000/g' {} \;
Example 3: Remove Trailing Whitespace
sed -i 's/[[:space:]]*$//' file.txt
Example 4: Add Text to Beginning of Lines
# Add "# " to beginning of each line (comment out)
sed 's/^/# /' file.txt
Example 5: Replace in Specific Section
# Replace only in lines 10-50
sed '10,50s/old/new/g' file.txt
Key Takeaways
-
vim has two modes: Command (default) and Insert (press
i) - ESC returns to command mode - Always remember this
-
:wq saves and quits - Or use
Shift+ZZ -
sed is for automation - Use
-ito modify files - Test sed without -i first - Preview changes before applying
- Use /g for global replace - Without it, only first occurrence changes
- vim for interactive, sed for batch - Choose the right tool
- Practice on openvim.com - Interactive vim tutorial
vim and sed aren't relics. They're essential tools for server management, automation, and efficient text editing. Master them and you'll edit files faster than anyone clicking through a GUI.
What's your most useful vim or sed command? Share your go-to editing tricks in the comments.
Top comments (0)