The Magic of Linux Text Editors

Source: Dev.to | Dec 15, 2025 (UTC)

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:

  1. Command Mode (default) – Navigate, delete, copy, search
  2. 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

Enter fullscreen mode

Exit fullscreen mode

You start in Command Mode.

Insert Mode: Adding Text

From Command Mode, press:

Insert at cursor position

i # Insert mode at cursor

Enter fullscreen mode

Exit fullscreen mode

Insert with space

a # Insert mode after cursor (adds space)

Enter fullscreen mode

Exit fullscreen mode

New line and insert

o # New line below, enter insert mode O # New line above, enter insert mode (capital O)

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Deleting

x # Delete character under cursor dd # Delete entire line dw # Delete word D # Delete from cursor to end of line

Enter fullscreen mode

Exit fullscreen mode

Undo

u # Undo last change Ctrl+r # Redo

Enter fullscreen mode

Exit fullscreen mode

Replace

r # Replace single character (type new char) R # Replace mode (keep typing to replace multiple chars)

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Breaking down :%s/old/new/g:

Replace on Specific Lines

:1,10s/old/new/g # Lines 1-10 :5s/old/new/g # Line 5 only

Enter fullscreen mode

Exit fullscreen mode

Example: Change Variable Name

# Replace "userName" with "userId" everywhere :%s/userName/userId/g

Enter fullscreen mode

Exit fullscreen mode

vi vs vim

vi – Original editor, basic features, always available

vim – Enhanced version with:

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

Enter fullscreen mode

Exit fullscreen mode

The sed Command: Stream Editor

sed (stream editor) transforms text non-interactively. Perfect for scripts and bulk operations.

Basic Syntax

sed 'command' filename

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Important:

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

Enter fullscreen mode

Exit fullscreen mode

Replace Special Characters

# Replace tabs with spaces sed -i 's/\t/ /g' filename # Replace spaces with tabs sed -i 's/ /\t/g' filename

Enter fullscreen 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

Enter fullscreen mode

Exit fullscreen mode

Add Blank Lines

# Add blank line after each line sed G filename # Double-space the file sed G filename > newfile.txt

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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' {} \;

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Scenario 5: Remove Comments

Remove lines starting with # (comments):

sed -i '/^#/d' script.sh

Enter fullscreen mode

Exit fullscreen mode

Combining vi/vim and sed

Use vi/vim for interactive editing. Use sed for automated changes.

When to Use vim

When to Use sed

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Tips for Efficiency

Tip 1: Create backups before sed -i

# Create backup with .bak extension sed -i.bak 's/old/new/g' file.txt

Enter fullscreen mode

Exit fullscreen mode

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

Enter fullscreen mode

Exit fullscreen mode

Tip 4: Vim line numbers

# In vim command mode :set number # Show line numbers :set nonumber # Hide line numbers

Enter fullscreen mode

Exit fullscreen mode

Practical Examples

Example 1: Update Database Connection

# Old: localhost # New: db.server.com sed -i 's/localhost/db.server.com/g' config.php

Enter fullscreen mode

Exit fullscreen mode

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' {} \;

Enter fullscreen mode

Exit fullscreen mode

Example 3: Remove Trailing Whitespace

sed -i 's/[[:space:]]*$//' file.txt

Enter fullscreen mode

Exit fullscreen mode

Example 4: Add Text to Beginning of Lines

# Add "# " to beginning of each line (comment out) sed 's/^/# /' file.txt

Enter fullscreen mode

Exit fullscreen mode

Example 5: Replace in Specific Section

# Replace only in lines 10-50 sed '10,50s/old/new/g' file.txt

Enter fullscreen mode

Exit fullscreen mode

Key Takeaways

  1. vim has two modes: Command (default) and Insert (press i)
  2. ESC returns to command mode – Always remember this
  3. :wq saves and quits – Or use Shift+ZZ
  4. sed is for automation – Use -i to modify files
  5. Test sed without -i first – Preview changes before applying
  6. Use /g for global replace – Without it, only first occurrence changes
  7. vim for interactive, sed for batch – Choose the right tool
  8. 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.

For further actions, you may consider blocking this person and/or reporting abuse

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *

Читайте також