Linux CLI 24 ๐ง nano CLI editor
Nano is a popular command-line text editor on Unix-based systems. It’s open source, simple, and powerful โ a favorite for beginners and system administrators alike.
Introduction and Installation
Nano comes pre-installed on many Linux distributions. If not, install it with:
Debian / Ubuntu:
sudo apt-get install nano
RHEL / Fedora:
sudo yum install nano
Arch / Manjaro:
sudo pacman -S nano
openSUSE:
sudo zypper install nano
Opening Files with Nano
nano filename
nano aaa.txt
nano /etc/hosts
nano
| Command | Description |
|---|---|
nano filename | Open or create filename |
nano /etc/hosts | Open a system file (may need sudo) |
nano | Start empty โ specify name when saving |
Examples:
# Create or open a file
$ nano notes.txt
# Open a system file with sudo
$ sudo nano /etc/hosts
# Start empty โ save with name later
$ nano
# (type content, then Ctrl+O to specify filename)
Search Text and Navigation
Searching
Ctrl + W โ Enter search term, press Enter
Alt + W โ Continue searching for the same term
Ctrl + R โ Find and replace (after Ctrl+W)
| Shortcut | Action |
|---|---|
Ctrl + W | Search forward |
Alt + W | Find next occurrence |
Ctrl + R | Replace (search + replace) |
Search example:
# In nano:
# 1. Press Ctrl+W
# 2. Type "error"
# 3. Press Enter
# โ Cursor jumps to first "error"
# 4. Press Alt+W repeatedly to find next
Find and replace:
# 1. Press Ctrl+W โ search for "old"
# 2. Press Enter โ found
# 3. Press Ctrl+R โ replace mode
# 4. Type "new"
# 5. Press Enter
# 6. Press Y to replace one, or A to replace all
Navigation
Use arrow keys or these shortcuts:
| Shortcut | Action |
|---|---|
โ โ โ โ | Move cursor |
Ctrl + P | Move up |
Ctrl + N | Move down |
Ctrl + F | Move forward (right) |
Ctrl + B | Move backward (left) |
Home / Ctrl + A | Beginning of line |
End / Ctrl + E | End of line |
PgUp / Ctrl + Y | Page up |
PgDn / Ctrl + V | Page down |
Visual:
Ctrl + P / โ
โฒ
โ
Ctrl + B โโโโโผโโโโถ Ctrl + F
/ โ โ / โ
โผ
Ctrl + N / โ
Ctrl + A / Home โโโ Beginning of line
Ctrl + E / End โโโ End of line
Ctrl + Y / PgUp โโโ Page up
Ctrl + V / PgDn โโโ Page down
Edit, Save, and Exit
Selecting Text
| Shortcut | Action |
|---|---|
Alt + A | Start selection (mark) |
| Arrow keys | Extend selection |
Alt + A | Stop selection |
Example:
# 1. Position cursor at start of text
# 2. Press Alt+A โ "Mark Set" appears
# 3. Use arrows to select text
# 4. Press Alt+A again to cancel marking
Copy and Paste
| Shortcut | Action |
|---|---|
Alt + 6 | Copy selection to clipboard |
Ctrl + K | Cut selection or cut to end of line |
Ctrl + U | Paste or cut to start of line |
Ctrl + Y | Paste the cut from Ctrl + U |
Note:
Ctrl + KandCtrl + Uhave dual purposes:
- With selection: Cut the selected text
- Without selection: Cut to end/start of line
Examples:
# Copy and paste:
# 1. Select text with Alt+A + arrows
# 2. Alt+6 to copy
# 3. Move cursor to where you want to paste
# 4. Ctrl+U to paste
# Cut a line:
# 1. Cursor anywhere on line
# 2. Ctrl+K โ cuts to end of line
# 3. Move to new position
# 4. Ctrl+U โ pastes it
# Cut and paste multiple lines:
# 1. Press Ctrl+K repeatedly to cut lines
# 2. Move cursor
# 3. Ctrl+U to paste all at once
Saving and Exiting
| Shortcut | Action |
|---|---|
Ctrl + O | Save the file (write out) |
Ctrl + X | Exit nano |
Ctrl + G | Show help menu |
Saving a file:
# 1. Press Ctrl+O
# 2. Press Enter to confirm filename
# (or type a new name to save as)
# โ File saved
Exiting with unsaved changes:
# 1. Press Ctrl+X
# 2. Nano asks: "Save modified buffer?"
# 3. Press Y to save, N to discard, Ctrl+C to cancel
Complete Example Session
# ============================================
# PART 1: CREATE A NEW FILE
# ============================================
$ nano notes.txt
# Empty editor opens
# Type content:
# Hello, this is my first note.
# This is the second line.
# This is the third line.
# Save and exit:
# Ctrl+O โ Enter โ Ctrl+X
# ============================================
# PART 2: EDIT AN EXISTING FILE
# ============================================
$ nano notes.txt
# File opens with previous content
# Navigate to end of file:
# Ctrl+E (end of line)
# Ctrl+N (move down)
# Add new line:
# This is the fourth line.
# Save:
# Ctrl+O โ Enter
# ============================================
# PART 3: SEARCH AND REPLACE
# ============================================
$ nano notes.txt
# Search for "second":
# 1. Ctrl+W
# 2. Type "second"
# 3. Enter
# โ Cursor at "second"
# Find next:
# Alt+W (repeats search)
# Find and replace:
# 1. Ctrl+W โ search "second"
# 2. Enter
# 3. Ctrl+R โ replace
# 4. Type "2nd"
# 5. Enter
# 6. Press Y (replace this) or A (replace all)
# ============================================
# PART 4: SELECT, COPY, PASTE
# ============================================
$ nano notes.txt
# Select a block:
# 1. Move cursor to start
# 2. Alt+A (mark set)
# 3. Arrow keys to extend selection
# Copy:
# Alt+6
# Move cursor to end
# Paste:
# Ctrl+U
# ============================================
# PART 5: CUT AND PASTE LINES
# ============================================
$ nano notes.txt
# Cut current line to end:
# Ctrl+K
# Cut more lines:
# Ctrl+K (again)
# Move cursor to desired position
# Paste all cut lines:
# Ctrl+U
# ============================================
# PART 6: OPEN A SYSTEM FILE
# ============================================
$ sudo nano /etc/hosts
# Edit the file
# Add a line:
# 127.0.0.1 myapp.local
# Save and exit:
# Ctrl+O โ Enter โ Ctrl+X
# ============================================
# PART 7: HELP MENU
# ============================================
$ nano
# Ctrl+G โ shows help menu with all shortcuts
# Ctrl+X โ exit help
Complete Shortcut Reference
File Operations
| Shortcut | Action |
|---|---|
Ctrl + O | Save (Write Out) |
Ctrl + X | Exit |
Ctrl + R | Read file into current |
Ctrl + G | Help |
Search
| Shortcut | Action |
|---|---|
Ctrl + W | Search |
Alt + W | Find next |
Ctrl + R | Replace |
Navigation
| Shortcut | Action |
|---|---|
โ โ โ โ | Move cursor |
Ctrl + P | Up |
Ctrl + N | Down |
Ctrl + F | Forward |
Ctrl + B | Backward |
Ctrl + A / Home | Start of line |
Ctrl + E / End | End of line |
Ctrl + Y / PgUp | Page up |
Ctrl + V / PgDn | Page down |
Editing
| Shortcut | Action |
|---|---|
Alt + A | Start/stop selection |
Alt + 6 | Copy selection |
Ctrl + K | Cut selection/line-end |
Ctrl + U | Paste/cut line-start |
Ctrl + Y | Paste the Ctrl+U cut |
Ctrl + D | Delete character under cursor |
Backspace | Delete character before cursor |
Ctrl + H | Delete character before cursor |
Alt + D | Delete word forward |
Alt + Backspace | Delete word backward |
Screen
| Shortcut | Action |
|---|---|
Ctrl + L | Redraw screen |
Ctrl + _ | Go to line number |
Misc
| Shortcut | Action |
|---|---|
Ctrl + C | Show cursor position |
Ctrl + J | Justify paragraph |
Ctrl + T | Spell check (if available) |
Nano vs Vim vs Emacs
| Aspect | Nano | Vim | Emacs |
|---|---|---|---|
| Learning curve | โ Easy | โ Steep | โ ๏ธ Moderate |
| Modes | โ None | โ Multiple | โ Multiple |
| Shortcut style | Ctrl+key | Single keys | Ctrl+/Alt+ |
| Power | โ Good | โ โ Excellent | โ โ Excellent |
| Best for | Beginners | Power users | Programmers |
| Availability | โ Universal | โ Universal | โ ๏ธ Less common |
Best Practices
โ Do This:
# Use nano for quick edits
sudo nano /etc/hosts
# Use Ctrl+O to save often
# (avoid losing work)
# Use Ctrl+G when unsure
# (help is always available)
# Use Alt+A + arrows for selections
# (then Alt+6 to copy, Ctrl+U to paste)
# Use Ctrl+W for searching
# (Alt+W to find next)
# Use sudo for system files
sudo nano /etc/ssh/sshd_config
โ Don’t Do This:
# Don't forget to save before exit
# Ctrl+X without Ctrl+O โ may lose changes
# Don't use sudo on your own files
sudo nano ~/notes.txt # โ ๏ธ Creates root-owned file!
nano ~/notes.txt # โ
# Don't panic if you press wrong key
# Ctrl+C cancels most operations
# Don't forget: Ctrl+K cuts to end of line
# (not just the whole line)
# Don't confuse Ctrl+U and Ctrl+Y
# Ctrl+U: paste OR cut to start
# Ctrl+Y: paste the cut from Ctrl+U
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
| Forgot to save | Lost changes | Always Ctrl+O before exit |
Confused Ctrl+K | Cuts line to end | Cursor position matters |
Ctrl+U didn’t paste | Depending on context | Ctrl+Y for the pasted buffer |
| Can’t exit nano | Stuck in editor | Ctrl+X, then Y or N |
| Wrong file permissions | Can’t save | Use sudo nano for system files |
| Accidentally deleted | No undo for multi-line | Ctrl+U re-pastes the cut |
Real-World Use Cases
1. Edit System Configuration
$ sudo nano /etc/ssh/sshd_config
# Edit config
# Ctrl+O โ Enter โ Ctrl+X
$ sudo systemctl restart sshd
2. Add a Host Entry
$ sudo nano /etc/hosts
# Add at end:
127.0.0.1 myapp.local
# Save and exit
$ ping myapp.local
3. Edit Crontab
$ crontab -e
# Opens in nano (if EDITOR=nano)
# Add cron jobs
# Save and exit
4. Write a Shell Script
$ nano backup.sh
#!/bin/bash
echo "Backup starting..."
tar -czf backup.tar.gz /home/kronos
echo "Backup complete!"
# Save, exit
$ chmod +x backup.sh
$ ./backup.sh
5. Fix a Broken Config
$ sudo nano /etc/nginx/nginx.conf
# Fix the problem
# Ctrl+O โ Enter โ Ctrl+X
$ sudo nginx -t # Test config
$ sudo systemctl reload nginx
6. Write Documentation
$ nano README.md
# Type Markdown content
# Ctrl+O โ Enter โ Ctrl+X
7. Search and Replace in Bulk
$ nano config.txt
# Ctrl+W โ "localhost" โ Enter
# Ctrl+R โ "127.0.0.1"
# Press A โ replace all
# Ctrl+O โ Enter โ Ctrl+X
Visual: Nano Screen Layout
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GNU nano 5.1 File: notes.txt โ โ Title bar
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Hello, this is my first note. โ
โ This is the second line. โ
โ This is the third line. โ
โ โ โ โ Cursor
โ โ
โ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ^G Help ^O Write Out ^W Where Is โ โ Shortcut bar
โ ^K Cut ^T Execute ^C Location โ
โ ^X Exit ^R Read File ^\ Replace โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Feature | Shortcut |
|---|---|
| Open file | nano file.txt |
| Save | Ctrl + O |
| Exit | Ctrl + X |
| Search | Ctrl + W |
| Find next | Alt + W |
| Replace | Ctrl + R |
| Select | Alt + A |
| Copy | Alt + 6 |
| Cut | Ctrl + K |
| Paste | Ctrl + U |
| Help | Ctrl + G |
Key takeaways:
- Nano is a simple, beginner-friendly editor โ no modes to learn
- Install with
sudo apt install nano(Debian) orsudo yum install nano(RHEL) - Open files with
nano filename Ctrl + Osaves โCtrl + XexitsCtrl + Wsearches โAlt + Wfinds next โCtrl + RreplacesAlt + Astarts selection,Alt + 6copies,Ctrl + UpastesCtrl + Kcuts to end of line โCtrl + Upastes itCtrl + Gshows help โ always available if you forget a shortcut- Use
sudo nanofor system files โ but never on your own files - Save often with
Ctrl+Oโ no autosave!
Remember: Nano is your friendly command-line editor. Its shortcut bar at the bottom always shows the most common keys, and Ctrl+G brings up the complete help menu. The most important shortcuts are Ctrl+O (save), Ctrl+X (exit), and Ctrl+W (search). Once you know those three, you can handle almost any file. Master Ctrl+K for cutting lines and Ctrl+U for pasting โ these are how you move text around in nano!
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!