Linux: Nano Text Editor Guide
Nano is a simple, user-friendly command-line text editor for Unix and Linux systems. This guide covers installation, shortcuts, and advanced usage.
Table of Contents
Introduction
Nano is a lightweight text editor that runs in the terminal, making it ideal for:
Quick file editing on servers
Editing configuration files
Working in environments without a GUI
Beginners who find vi/vim too complex
Key Features:
Easy to use with on-screen help
Syntax highlighting for many languages
Search and replace functionality
Multiple file buffers
Undo/redo support
Official Reference: Nano Editor Cheatsheet
Installation
Install on Debian/Ubuntu
sudo apt-get update
sudo apt-get install nano
Install on RHEL/CentOS/Fedora
sudo yum install nano
# or on newer versions
sudo dnf install nano
Verify Installation
nano --version
Basic Usage
Opening Files
# Open a file
nano filename.txt
# Create a new file
nano newfile.txt
# Open file at specific line
nano +10 filename.txt
# Open file in read-only mode
nano -v filename.txt
Understanding the Interface
When you open nano, you’ll see:
Top line: Nano version and filename
Main area: File content (editable)
Bottom two lines: Available shortcuts (help menu)
The ^ symbol represents the Ctrl key, and M- represents the Alt key.
Keyboard Shortcuts Reference
File Handling
Shortcut |
Description |
|---|---|
|
Save current file |
|
Offer to write file (“Save as”) |
|
Insert a file into current one |
|
Close buffer, exit from nano |
Editing Operations
Shortcut |
Description |
|---|---|
|
Cut current line into cutbuffer |
|
Copy current line into cutbuffer |
|
Paste contents of cutbuffer |
|
Cut until end of buffer |
|
Complete current word (auto-complete) |
|
Comment/uncomment line or region |
|
Undo last action |
|
Redo last undone action |
Search and Replace
Shortcut |
Description |
|---|---|
|
Start forward search |
|
Start backward search |
|
Find next occurrence forward |
|
Find next occurrence backward |
|
Start a replacing session (search and replace) |
Deletion
Shortcut |
Description |
|---|---|
|
Delete character before cursor (Backspace) |
|
Delete character under cursor |
|
Delete word to the left |
|
Delete word to the right |
|
Delete current line |
Advanced Operations
Shortcut |
Description |
|---|---|
|
Execute some command (spell checker, etc.) |
|
Justify paragraph or region |
|
Justify entire buffer |
|
Run a syntax check |
|
Run a formatter/fixer/arranger |
|
Start/stop recording of macro |
|
Replay macro |
Information and Help
Shortcut |
Description |
|---|---|
|
Report cursor position |
|
Report line/word/character count |
|
Display help text |
View and Display Options
Shortcut |
Description |
|---|---|
|
Turn the mark on/off (for selecting text) |
|
Indent marked region |
|
Unindent marked region |
|
Enter next keystroke verbatim |
|
Turn line numbers on/off |
|
Turn visible whitespace on/off |
|
Hide or unhide the help lines |
|
Refresh the screen |
Common Tasks
How to Save and Exit
Save and continue editing:
Press
Ctrl+SorCtrl+OConfirm filename (press Enter)
Save and exit:
Press
Ctrl+XPress
Ywhen asked to saveConfirm filename (press Enter)
Exit without saving:
Press
Ctrl+XPress
Nwhen asked to save
How to Cut, Copy, and Paste
Cut a line:
Position cursor on the line
Press
Ctrl+K
Copy a line:
Position cursor on the line
Press
Alt+6
Paste:
Move cursor to desired location
Press
Ctrl+U
Cut/Copy multiple lines:
Move cursor to start of selection
Press
Alt+Ato set markMove cursor to end of selection
Press
Ctrl+Kto cut orAlt+6to copy
How to Search and Replace
Search:
Press
Ctrl+WType search term
Press Enter
Press
Alt+Wto find next occurrence
Search and replace:
Press
Alt+REnter search term, press Enter
Enter replacement term, press Enter
Press
Yto replace,Nto skip, orAto replace all
How to Go to a Specific Line
Press
Alt+GEnter line number
Press Enter
Configuration
Nano Configuration File
Nano can be customized using a configuration file located at:
System-wide:
/etc/nanorcUser-specific:
~/.nanorc
Creating Custom Configuration
Create or edit your personal configuration file:
nano ~/.nanorc
Common configuration options:
# Enable syntax highlighting
include /usr/share/nano/*.nanorc
# Show line numbers
set linenumbers
# Enable mouse support
set mouse
# Auto-indent new lines
set autoindent
# Convert tabs to spaces
set tabstospaces
# Set tab size to 4 spaces
set tabsize 4
# Enable soft wrapping
set softwrap
# Backup files
set backup
set backupdir "~/.nano/backups"
# Smooth scrolling
set smooth
Useful Command-Line Options
# Open with line numbers
nano -l filename.txt
# Open with mouse support
nano -m filename.txt
# Open with auto-indent
nano -i filename.txt
# Open in read-only mode
nano -v filename.txt
# Create backup of original file
nano -B filename.txt
# Set tab width to 4
nano -T 4 filename.txt
Tips and Best Practices
Essential Tips
Always use Ctrl+X to exit - This ensures you’re prompted to save changes
Enable line numbers - Helps with debugging and referencing code (
Alt+N)Use search instead of scrolling - Faster for large files (
Ctrl+W)Master undo/redo -
Alt+UandAlt+Ecan save you from mistakesUse marks for selecting -
Alt+Ato start selecting, then navigate to select text
Syntax Highlighting
Nano automatically detects file types and applies syntax highlighting. To manually set syntax:
# View available syntax definitions
ls /usr/share/nano/
# Open with specific syntax
nano -Y python script.txt
Working with Multiple Files
# Open multiple files
nano file1.txt file2.txt file3.txt
# Switch between buffers
# Use Alt+< (previous) and Alt+> (next)
Troubleshooting
Common Issues
Nano not found:
# Install nano
sudo apt-get install nano # Debian/Ubuntu
sudo yum install nano # RHEL/CentOS
Shortcuts not working:
Check if your terminal emulator is capturing the key combinations
Try using the
Esckey instead ofAlt(e.g.,EscthenUinstead ofAlt+U)
No syntax highlighting:
# Install syntax highlighting files
sudo apt-get install nano-syntax-highlighting
# Add to ~/.nanorc
include /usr/share/nano/*.nanorc
Can’t save file (Permission denied):
# Edit file with sudo
sudo nano /path/to/protected/file
Conclusion
Nano is an excellent text editor for beginners and experienced users who need a straightforward, efficient editing experience in the terminal. With this guide, you should be able to:
Navigate and edit files efficiently
Use advanced features like search/replace and macros
Customize nano to fit your workflow
Troubleshoot common issues
Additional Resources: