Lesson 4: Text Editors
Goal: Learn to edit files directly in the terminal with nano and vim.
Table of Contents
nano — The Beginner-Friendly Editor
nano is the easiest terminal text editor. All keyboard shortcuts are shown at the bottom of the screen.
Open / create a file
nano myfile.txt
If the file doesn't exist, nano creates it when you save.
nano Interface
GNU nano 6.2 myfile.txt
Hello World
This is my file content.
| ← cursor is here
^G Help ^O Write Out ^W Where Is ^K Cut ^U Paste
^X Exit ^R Read File ^\ Replace ^J Justify ^T Spell
The ^ symbol means Ctrl. So ^O means Ctrl + O.
Essential nano shortcuts
| Shortcut | Action |
|---|---|
Ctrl + O |
Save file (Write Out) |
Ctrl + X |
Exit nano |
Ctrl + K |
Cut current line |
Ctrl + U |
Paste cut line |
Ctrl + W |
Search for text |
Ctrl + \ |
Search and replace |
Ctrl + G |
Help screen |
Ctrl + C |
Show cursor position (line number) |
Alt + U |
Undo |
Alt + E |
Redo |
Ctrl + _ |
Go to line number |
Alt + A |
Select text (mark) |
Ctrl + 6 |
Copy selected text |
Practical workflow
# 1. Open the file
nano config.txt
# 2. Type your content
# 3. Save: Ctrl + O, then Enter to confirm filename
# 4. Exit: Ctrl + X
Open file at a specific line
nano +15 config.txt # opens at line 15
nano with syntax highlighting
# Open a Python file — nano highlights syntax automatically
nano script.py
nano with line numbers
nano -l myfile.txt
vim — The Power User Editor
vim is extremely powerful but has a learning curve. The key concept: vim has modes.
The Three Modes
┌─────────────┐
│ NORMAL MODE │ ← You start here. Navigate and execute commands.
│ (default) │
└──────┬──────┘
│
│ Press 'i' to enter Insert Mode
│ Press ':' to enter Command Mode
▼
┌─────────────┐ ┌──────────────┐
│ INSERT MODE │ │ COMMAND MODE │
│ (editing) │ │ (save/quit) │
└──────┬──────┘ └──────┬───────┘
│ │
│ Press 'Esc' │ Press 'Enter' to execute
│ to go back │ then back to Normal Mode
└────────┬──────────┘
│
▼
Back to NORMAL
Open a file
vim myfile.txt
Survival guide: Save and Quit
If you accidentally open vim, here's how to get out:
1. Press Esc (make sure you're in Normal Mode)
2. Type :wq (write and quit)
3. Press Enter
Other quit commands:
| Command | Action |
|---|---|
:w |
Save (write) |
:q |
Quit (only if no changes) |
:wq |
Save and quit |
:q! |
Quit WITHOUT saving |
:wq! |
Force save and quit |
ZZ |
Save and quit (shortcut) |
Normal Mode — Navigation
| Key | Action |
|---|---|
h |
Move left |
j |
Move down |
k |
Move up |
l |
Move right |
w |
Jump to next word |
b |
Jump to previous word |
0 |
Go to beginning of line |
$ |
Go to end of line |
gg |
Go to first line |
G |
Go to last line |
15G |
Go to line 15 |
Ctrl + f |
Page down |
Ctrl + b |
Page up |
Normal Mode — Editing
| Key | Action |
|---|---|
i |
Insert before cursor |
a |
Insert after cursor |
A |
Insert at end of line |
o |
Open new line below |
O |
Open new line above |
x |
Delete character |
dd |
Delete entire line |
5dd |
Delete 5 lines |
yy |
Copy (yank) line |
5yy |
Copy 5 lines |
p |
Paste below |
P |
Paste above |
u |
Undo |
Ctrl + r |
Redo |
dw |
Delete word |
cw |
Change word (delete + insert) |
Normal Mode — Search
| Key | Action |
|---|---|
/pattern |
Search forward |
?pattern |
Search backward |
n |
Next match |
N |
Previous match |
:%s/old/new/g |
Replace all occurrences |
:%s/old/new/gc |
Replace all with confirmation |
Practical vim workflow
# Open file
vim server.conf
# 1. Press 'i' to enter Insert Mode
# 2. Type or edit your text
# 3. Press 'Esc' to return to Normal Mode
# 4. Type ':wq' and press Enter to save and quit
Open file at a specific line
vim +20 config.txt # opens at line 20
vim with line numbers
In Normal Mode, type:
:set number
To make it permanent, add to ~/.vimrc:
echo "set number" >> ~/.vimrc
When to Use Which
| Scenario | Recommended |
|---|---|
| Quick config edit | nano |
| Editing on a remote server | nano (always available) |
| Programming / coding | vim |
| Complex find & replace | vim |
| First time using Linux | nano |
| Speed and efficiency | vim (once learned) |
Exercises
Exercise 1: nano basics
# 1. Create and edit a file with nano
nano grocery-list.txt
# 2. Type this content:
# Milk
# Bread
# Eggs
# Butter
# 3. Save: Ctrl + O, Enter
# 4. Exit: Ctrl + X
# 5. Verify
cat grocery-list.txt
Exercise 2: nano search and replace
# 1. Open the file
nano grocery-list.txt
# 2. Search: Ctrl + W, type "Bread", Enter
# 3. Replace: Ctrl + \, type "Bread", Enter, type "Whole Wheat Bread", Enter
# 4. Confirm: Y
# 5. Save and exit: Ctrl + O, Enter, Ctrl + X
Exercise 3: vim survival
# 1. Open a file in vim
vim practice.txt
# 2. Press 'i' to enter Insert Mode
# 3. Type: "I survived vim!"
# 4. Press 'Esc'
# 5. Type ':wq' and press Enter
# 6. Verify
cat practice.txt
Exercise 4: vim editing
# 1. Open the file
vim practice.txt
# 2. In Normal Mode:
# - Press 'o' to open a new line below
# - Type: "This is line 2"
# - Press Esc
# - Press 'yy' to copy the line
# - Press 'p' to paste it below
# - Press 'dd' to delete the duplicate
# - Type ':wq' to save and quit
cat practice.txt
Key Takeaways
- nano — easy, visual shortcuts at the bottom,
Ctrl+Osave,Ctrl+Xexit - vim — modal editor,
ito type,Escto stop,:wqto save and quit - Start with nano, learn vim gradually — it pays off enormously
Escis your safety key in vim — when in doubt, pressEsc
Next Lesson: Lesson 5: Getting Help →