The Magic of Linux Text Editors
Source: Dev.to
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:
- vi/vim – interactive editing.
- sed – 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
| Mode | Description |
|---|---|
| Command Mode (default) | Navigate, delete, copy, search |
| Insert Mode | Type text normally |
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:
| Key | Action |
|---|---|
i | Insert at cursor position |
a | Insert after cursor (adds a space) |
o | Open a new line below and enter Insert mode |
O | Open a new line above and 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 the arrow keys.)
Deleting
x # Delete character under cursor
dd # Delete entire line
dw # Delete word
D # Delete from cursor to end of line
Undo / Redo
u # Undo last change
Ctrl+r # Redo
Replace
r # Replace single character (type new char)
R # Replace mode – keep typing to replace multiple characters
Note: r is unreliable for multiple characters. Use R or a search‑/replace command instead.
Search
/keyword # Search forward
?keyword # Search backward
n # Next occurrence
N # Previous occurrence
Copy and Paste (Yank / Put)
yy # Yank (copy) current line
p # Put (paste) below cursor
P # Put above cursor (capital P)
3yy # Yank 3 lines
Save and Exit
:w # Write (save)
:q # Quit (only if no changes)
:wq # Write and quit
:wq! # Force write and quit
:q! # Quit without saving (discard changes)
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 the file
:%s/old/new/gc " Replace all, with confirmation
:s/old/new/g " Replace in the current line only
Breakdown of :%s/old/new/g:
:– Enter command‑line mode.%– Apply to the whole 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
| Feature | vi | vim |
|---|---|---|
| Availability | Always present on Unix‑like systems | May need installation |
| Syntax highlighting | No | Yes |
| Multiple undo levels | Limited | Yes |
| Split windows | No | Yes |
| Plugins | No | Yes |
| Enhanced search/replace | Basic | Advanced |
Check out openvim.com for an interactive vim tutorial.
Installing vim (if not already present)
# Ubuntu/Debian
sudo apt install vim
# CentOS/RHEL
sudo yum install vim
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 stdout (original file unchanged). - With
-i, the file is modified directly. - The
/gflag makes the substitution global on each line.
Delete Text
# Delete a specific word
sed 's/Name//g' filename
# Delete lines containing a 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
TL;DR
- vi/vim – Use for interactive editing, quick fixes, and learning powerful keyboard‑driven workflows.
- sed – Use for one‑liners, scripts, and bulk text transformations across many files.
Master both, and you’ll edit remote Linux systems like a pro.
Screen Mode
Exit fullscreen mode
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 a blank line after each line
sed G filename
# Double‑space the file (output to a new 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
# 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
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
# 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 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
sed -i '/^#/d' script.sh
Combining vi/vim and sed
- Use vi/vim for interactive editing.
- Use sed for automated, batch changes.
When to Use vim
- Editing single files
- Need to see context while editing
- Making multiple, different changes
- Interactive work
When to Use sed
- Batch processing many 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 | Open a new line below |
ESC | Return to command mode |
x | Delete character |
dd | Delete line |
u | Undo |
r | Replace character |
/keyword | Search |
yy | Yank (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 occurrences |
sed Commands
| Command | Action |
|---|---|
sed 's/old/new/g' file | Replace all occurrences |
sed -i 's/old/new/g' file | Replace in‑place |
sed '/keyword/d' file | Delete lines containing 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 | Convert tabs to spaces |
sed G file | Double‑space (add a blank line after each line) |
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 if it looks good
Mistake #3 – Not escaping special characters
# Wrong
sed 's/http://old.com/http://new.com/g' file
# Right – escape slashes or use another delimiter
sed 's|http://old.com|http://new.com|g' file
Mistake #4 – Forgetting the /g flag
# Replaces only the 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 a .bak backup
sed -i.bak 's/old/new/g' file.txt
Tip 2 – Use vim for learning, sed for automation
Learn editing interactively in vim; once you know the pattern, 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
: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 *.conf files
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
# Prefix each line with "# "
sed -i 's/^/# /' file.txt
Commenting Out a Line
sed 's/^/# /' file.txt
Example 5: Replace in a 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 (
i). ESCreturns to command mode – always remember this.:wqsaves and quits – or useShift+ZZ.sedis for automation – use-ito modify files in‑place.- Test
sedwithout-ifirst – preview changes before applying them. - Use
/gfor global replace – without it only the first occurrence on a line changes. - Vim for interactive editing,
sedfor batch processing – choose the right tool for the job. - Practice on openvim.com – an 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.