Overview
Purpose
Text editors are fundamental tools for system administrators. They're used for editing configuration files, scripts, documentation, and source code. Understanding multiple editors ensures you can work in any environment.
The Big Three
vi/vim: Powerful, ubiquitous, modal editor. Available on every Unix/Linux system. nano: Beginner-friendly, non-modal. Good for quick edits. ed: Minimal line editor. Used in scripts and remote sessions.
Modes in vim
Command Mode: Default. Movement and deletion. Insert Mode: Type text. Ex Mode: Execute commands with :. Press Escape to return to Command Mode.
Editor Comparison Chart
| Feature | vi/vim | nano | ed |
|---|---|---|---|
| Learning Curve | Steep | Gentle | Steep |
| Availability | Universal | Common | Universal |
| Power | Very High | Medium | High |
| Automation/Scripting | Excellent | Poor | Excellent |
| Remote Sessions | Good | Good | Best |
| Resource Usage | Minimal | Minimal | Minimal |
vi and vim - The Everywhere Editor
Starting vim
vim filename or vi filename for the original. If file doesn't exist, it's created on save.
| COMMAND MODE - Movement and Deletion | |
|---|---|
h, j, k, l |
Left, Down, Up, Right (arrow keys also work) |
w, b, e |
Next word, Previous word, End of word |
0, $ |
Beginning of line, End of line |
G, gg |
End of file, Beginning of file |
dd, d$, dw |
Delete line, to end of line, word |
yy, y$, yw |
Copy (yank) line, to end, word |
p, P |
Paste after, before cursor |
u, Ctrl-r |
Undo, Redo |
/, ? |
Search forward, backward |
n, N |
Next match, Previous match |
| COMMAND MODE - Entering Insert Mode | |
|---|---|
i, a |
Insert before, after cursor |
I, A |
Insert at beginning, end of line |
o, O |
Open new line below, above |
Esc |
Return to Command Mode (from Insert Mode) |
| EX MODE - Commands (press : in Command Mode) | |
|---|---|
:w |
Write (save) file |
:q |
Quit |
:wq or :x |
Save and quit |
:q! |
Quit without saving |
:set nu |
Show line numbers |
:s/old/new/ |
Replace on current line |
:%s/old/new/g |
Replace all in file |
10 Detailed Examples
~
~
"/etc/hosts" 3 lines
What This Does:
Opens the /etc/hosts file in vim. The file displays with line numbers (if set). The ~ characters show empty lines beyond the file's end.
Saving and Quitting:
:w- Save without exiting:q- Quit if no changes:wq- Save and quit:q!- Quit, discard changes
2. Type your text
3. Press Esc to return to Command Mode
What This Does:
The most basic editing operation. Insert Mode allows normal text entry. Escape returns you to Command Mode for navigation and deletion.
Insert Mode Variants:
i- Insert before cursora- Insert after cursor (append)I- Insert at line beginningA- Insert at line endo- Insert new line belowO- Insert new line above
What This Does:
Replaces the first occurrence of "localhost" with "127.0.0.1" on the current line.
Breaking It Down:
%- Apply to entire files- Substitute command/old/new/- Find/replace patterng- Global (all occurrences per line)
Advanced Patterns:
:%s/\s\+$/- Remove trailing whitespace:%s/^#/- Remove # from line start:%s/.*/& &/- Duplicate each line's content
d$ - Delete to end of line
dw - Delete word
5dd - Delete 5 lines
1,5d - Delete lines 1-5 (Ex mode: :1,5d)
What These Do:
All these commands remove text. Deleted text goes to the clipboard and can be pasted with p. These are often combined with counts for efficiency.
Practical Use:
Delete and paste is often faster than manual retyping. For example, dd p moves a line down.
y$ - Copy to end of line
5yy - Copy 5 lines
p - Paste after cursor
P - Paste before cursor
What This Does:
Copy text to clipboard without deleting it. Pasting is identical to deleting and pasting—the clipboard is the same buffer.
Use Case Example:
To duplicate a configuration line: position cursor, yy to copy, p to paste below, then edit the duplicate.
G - Go to end of file
:42 - Go to line 42
/pattern - Search forward for pattern
?pattern - Search backward
n - Next match
N - Previous match
Efficient Navigation:
For large files, use search and line jumping rather than arrow keys. For example, /ERROR finds the first ERROR, then n jumps to the next one.
Production Tip:
Searching with / is much faster than scrolling through thousands of lines. Combine with :set hlsearch to highlight all matches.
[file contents here]
^X Exit ^O Write Out ^R Read File ^Y First Line ^K Cut Text
^C Cursor Pos ^V Next Page ^W Where Is ^U Uncut Text^T To Spell
What This Does:
Nano is much simpler than vim. All commands are shown at the bottom. The ^ means Ctrl key. No modes—just type directly.
Common nano Commands:
Ctrl+X- Exit (prompts to save if changed)Ctrl+O- Write (save) fileCtrl+W- Search for textCtrl+K- Cut (delete) lineCtrl+U- PasteCtrl+A- Beginning of lineCtrl+E- End of line
When to Use nano:
Great for quick edits, especially on systems where you haven't configured vim. Less powerful but more intuitive for beginners.
1
a
[type text]
.
w
q
What This Does:
Ed is a line-oriented editor. Commands are single letters. a appends lines (end with .), w writes, q quits.
Basic ed Commands:
a- Append (add lines)d- Delete liness/old/new/- Substitutep- Print linesw- Write fileq- Quit.- End of input
Warning:
Ed has minimal feedback and is difficult for interactive use. However, it's scriptable and universal—available on every Unix system.
[navigate to end with 'G']
[press 'o' to create new line]
[type: 192.168.1.100 myserver.local myserver]
[press Esc]
:wq
What This Does:
A practical scenario: adding a host entry to /etc/hosts. The key vim operations are demonstrated: navigation (G), inserting (o), and saving (:wq).
Critical Files Often Edited with vim:
/etc/hosts- Host mappings/etc/hostname- System hostname/etc/network/interfacesor /etc/netplan/ - Network config/etc/ssh/sshd_config- SSH configuration/etc/sudoers(use visudo, not vim directly)
What This Does:
Uses sed to replace text without opening an interactive editor. The -i flag modifies the file in-place. Useful in scripts and automation.
Automation Patterns:
sed -i.bak 's/old/new/g' file- Create backupawk '{print $1}' file > file.tmp && mv file.tmp file- Column extractionex -c '%s/^M//' file- Remove Windows line endings
When to Use sed vs vim:
Use sed for automated, repeatable edits in scripts. Use vim for interactive, exploratory editing and complex changes requiring human judgment.
Configuration and Advanced Tips
vim Configuration (.vimrc)
Create ~/.vimrc to configure vim. Common settings: set number (line numbers), set tabstop=4 (tab width), syntax on (syntax highlighting), set hlsearch (highlight search results).
vim Plugins and Extensions
Vim has a huge plugin ecosystem for advanced features. Popular plugin managers: vim-plug, Vundle. For system administration, focus on core vim first.
SSH and Remote Editing
SSH connections to servers use the system's default editor or whatever you specify. Ed is often best for slow/unreliable connections. Set EDITOR=nano or EDITOR=vi as needed.
Emergency Editing
On broken systems, ed might be your only option. Know basic ed commands: 1d (delete line 1), 1a (append after line 1), w (write), q (quit).
Quick Reference - Choosing Your Editor
| Situation | Best Choice | Why |
|---|---|---|
| System configuration files | vim | Powerful, available everywhere |
| Quick edit by new admin | nano | Easier to learn quickly |
| Scripted/batch edits | sed or awk | Non-interactive, automatable |
| Very slow/unreliable link | ed | Minimal traffic, robust |
| Complex code editing | vim (with config) | Syntax highlighting, navigation |
| Emergency single-line fix | ed or sed | Fast, no GUI needed |
Troubleshooting Common Issues
Problem: Stuck in vim Insert Mode
Press Esc to return to Command Mode. If Esc isn't responding, try Ctrl+C or Ctrl+[.
Problem: File open but nothing appears
You might be in Command Mode. Try pressing i to enter Insert Mode, or check with typing a command (colon appears at bottom).
Problem: Changes lost
Always save before quitting. In vim, :w saves without quitting. Use :wq or :x to save and quit. Remember to press Esc first if you were in Insert Mode.
Problem: Permission denied when saving
File permissions prevent writing. Use :w !sudo tee % to write with sudo privileges. The % refers to the current filename.