Nano: The User-Friendly Terminal Editor

Nano is a simple, user-friendly text editor for Unix and Linux systems. Created as a free replacement for the Pico editor, nano provides an intuitive interface with on-screen help, making it ideal for beginners and quick edits.

Why Choose Nano?

  • Beginner-friendly: Easy to learn with on-screen help
  • No modes: Direct typing without modal complexity
  • Lightweight: Minimal resource usage, perfect for Linux servers
  • Universal: Available on all Linux distributions by default
  • Quick edits: Perfect for Linux configuration files and system administration
  • Syntax highlighting: Built-in support for many file types
  • System administration: Ideal for editing Linux config files and scripts

Installation

Most Linux Distributions (Pre-installed)

# Check if nano is installed
which nano
nano --version

# If not installed:
# Ubuntu/Debian
sudo apt update
sudo apt install nano

# CentOS/RHEL/Fedora
sudo yum install nano
# or
sudo dnf install nano

# Arch Linux
sudo pacman -S nano

# Alpine Linux
sudo apk add nano

Additional Linux Distributions

# SUSE/openSUSE
sudo zypper install nano

# Gentoo
sudo emerge app-editors/nano

# Void Linux
sudo xbps-install nano

Basic Usage

Starting Nano

# Create new file
nano filename.txt

# Edit existing file
nano /path/to/file.txt

# Open file at specific line
nano +25 filename.txt

# Open file as read-only
nano -v filename.txt

# Open with line numbers
nano -l filename.txt

# Open with syntax highlighting disabled
nano -Y no filename.txt

Interface Overview

  GNU nano 6.2                    filename.txt                    Modified

This is the content of your file.
You can type directly here.
No modes to worry about!


^G Get Help    ^O Write Out   ^W Where Is    ^K Cut Text    ^J Justify
^X Exit        ^R Read File   ^\ Replace     ^U Paste Text  ^T To Spell

The bottom shows available commands where ^ means Ctrl key.

Essential Commands

File Operations

# Save file
Ctrl+O (Write Out)
# Then press Enter to confirm filename

# Save as different name
Ctrl+O
# Type new filename and press Enter

# Exit nano
Ctrl+X
# If unsaved changes, nano will ask to save

# Read/Insert another file
Ctrl+R
# Type filename to insert at cursor position

# New buffer (new file)
Alt+F (if multiple buffers enabled)

Text Editing

# Cut and paste
Ctrl+K           # Cut current line
Ctrl+U           # Paste (uncut)
Alt+6            # Copy current line
Ctrl+6           # Mark text (start selection)
                 # Move cursor to select text
Ctrl+K           # Cut selected text

# Delete operations
Backspace        # Delete character before cursor
Delete           # Delete character at cursor
Alt+Backspace    # Delete word to the left
Ctrl+Delete      # Delete word to the right

# Undo/Redo
Alt+U            # Undo
Alt+E            # Redo

Search and Replace

# Search
Ctrl+W           # Open search prompt
# Type search term and press Enter
# Use Ctrl+W again to search for next occurrence

# Search and replace
Ctrl+\           # Open replace prompt
# Enter search term, press Enter
# Enter replacement term, press Enter
# Choose: Y (yes), N (no), A (all), Ctrl+C (cancel)

# Case-sensitive search
Alt+C            # Toggle case sensitivity during search

# Regular expression search
Alt+R            # Toggle regex mode during search

Configuration

Global Configuration

# System-wide configuration
/etc/nanorc

# User configuration
~/.nanorc

Sample .nanorc Configuration

# ~/.nanorc

## Use auto-indentation
set autoindent

## Use bold text instead of reverse video text
set boldtext

## Set the characters treated as closing brackets
set brackets "\"')>]}}"

## Do case-sensitive searches by default
set casesensitive

## Constantly display the cursor position in the statusbar
set constantshow

## Use cut-from-cursor-to-end-of-line by default
set cutfromcursor

## Set the line length for wrapping text and justifying paragraphs
set fill 72

## Remember the used search/replace strings for the next session
set historylog

## Display line numbers to the left of the text
set linenumbers

## Enable vim-style lock-files
set locking

## Don't display the helpful shortcut lists at the bottom of the screen
# set nohelp

## Don't automatically add a newline when a file doesn't end with one
set nonewlines

## Set operating directory (chroot of sorts)
# set operatingdir "/tmp"

## Remember the cursor position in each file for the next editing session
set positionlog

## Do extended regular expression searches by default
set regexp

## Make the Home key smarter
set smarthome

## Use smooth scrolling as the default
set smooth

## Use this spelling checker instead of the internal one
set speller "aspell -x -c"

## Allow nano to be suspended
set suspend

## Use this tab size instead of the default; it must be greater than 0
set tabsize 4

## Convert typed tabs to spaces
set tabstospaces

## Save automatically on exit; don't prompt
# set tempfile

## Disallow file modification; why would you want this in an rc file?
# set view

## The two single-column characters used to display the first characters
## of tabs and spaces
set whitespace "»·"

## Detect word boundaries more accurately by treating punctuation
## characters as parts of words
set wordbounds

## Color syntax highlighting
include "/usr/share/nano/*.nanorc"

Syntax Highlighting

# Enable syntax highlighting for specific file types
# Add to ~/.nanorc:

## Python
include "/usr/share/nano/python.nanorc"

## HTML
include "/usr/share/nano/html.nanorc"

## CSS
include "/usr/share/nano/css.nanorc"

## JavaScript
include "/usr/share/nano/javascript.nanorc"

## JSON
include "/usr/share/nano/json.nanorc"

## Markdown
include "/usr/share/nano/markdown.nanorc"

## Shell scripts
include "/usr/share/nano/sh.nanorc"

## Configuration files
include "/usr/share/nano/conf.nanorc"

## Or include all available syntax files
include "/usr/share/nano/*.nanorc"

Custom Syntax Highlighting

# ~/.config/nano/custom.nanorc

## Custom syntax for log files
syntax "log" "\.log$"
color red "ERROR.*"
color yellow "WARNING.*"
color green "INFO.*"
color blue "DEBUG.*"
color magenta "\[[0-9-]+ [0-9:]+\]"

## Custom syntax for configuration files
syntax "config" "\.(conf|config|cfg)$"
color green "^[[:space:]]*[^=]*="
color red "^[[:space:]]*#.*"
color yellow ""[^"]*""
color cyan "[0-9]+"

Advanced Features

Multiple Buffers

# Enable multiple buffers in ~/.nanorc
set multibuffer

# Commands for multiple buffers
Alt+F            # Switch to next buffer
Alt+B            # Switch to previous buffer
Ctrl+R           # Read file into new buffer

Spell Checking

# Install spell checker
sudo apt install aspell aspell-en

# Use spell check in nano
Ctrl+T           # Start spell check
# Navigate through misspelled words
# Choose replacement or skip

Backup Files

# Enable backup files in ~/.nanorc
set backup

# Set backup directory
set backupdir "/home/user/.nano/backups"

# Create backup directory
mkdir -p ~/.nano/backups

Mouse Support

# Enable mouse support in ~/.nanorc
set mouse

# Mouse functions:
# - Click to position cursor
# - Double-click to select word
# - Triple-click to select line
# - Scroll wheel to scroll text

Real-World Usage Scenarios

Scenario 1: Quick Configuration File Edit

# Edit SSH configuration
sudo nano /etc/ssh/sshd_config

# Navigate to specific setting
Ctrl+W
# Search for "PasswordAuthentication"

# Change the value
# Move cursor to "yes" and change to "no"

# Save and exit
Ctrl+O
Enter
Ctrl+X

# Restart SSH service
sudo systemctl restart sshd

Scenario 2: System Log Analysis

# View system log
sudo nano -v /var/log/syslog

# Search for errors
Ctrl+W
# Type "error" and press Enter

# Continue searching
Ctrl+W
Enter (to search for same term)

# Go to specific time
Ctrl+W
# Type timestamp like "2023-12-01 10:"

# Exit without changes
Ctrl+X

Scenario 3: Script Development

# Create new script
nano backup_script.sh

# Add shebang and content
#!/bin/bash
# Backup script
DATE=$(date +%Y%m%d)
tar -czf backup_$DATE.tar.gz /home/user/documents

# Enable line numbers for debugging
Alt+N

# Save and make executable
Ctrl+O
Enter
Ctrl+X
chmod +x backup_script.sh

Scenario 4: Crontab Editing

# Edit user crontab
crontab -e
# If nano is default editor, it opens in nano

# Add scheduled job
# 0 2 * * * /home/user/backup_script.sh

# Save crontab
Ctrl+O
Enter
Ctrl+X

Command Line Options

Useful Nano Options

# Open with line numbers
nano -l filename.txt

# Open in restricted mode (no file operations)
nano -R filename.txt

# Set tab width
nano -T 8 filename.txt

# Enable soft wrapping
nano -S filename.txt

# Disable wrapping
nano -w filename.txt

# Show cursor position
nano -c filename.txt

# Enable mouse support
nano -m filename.txt

# Ignore nanorc files
nano -I filename.txt

# Set operating directory
nano -o /tmp filename.txt

# Enable suspension
nano -z filename.txt

Environment Variables

# Set nano as default editor
export EDITOR=nano
export VISUAL=nano

# Add to ~/.bashrc or ~/.zshrc
echo 'export EDITOR=nano' >> ~/.bashrc
echo 'export VISUAL=nano' >> ~/.bashrc

Tips and Best Practices

Productivity Tips

  1. Learn the shortcuts: Memorize Ctrl+O (save), Ctrl+X (exit), Ctrl+W (search)
  2. Use line numbers: Enable with -l option or set linenumbers in .nanorc
  3. Configure syntax highlighting: Include language-specific nanorc files
  4. Use search effectively: Ctrl+W for quick navigation in large files
  5. Enable mouse support: Makes selection and positioning easier

Common Shortcuts Summary

# File operations
Ctrl+O           # Save (Write Out)
Ctrl+X           # Exit
Ctrl+R           # Read/Insert file

# Navigation
Ctrl+A           # Beginning of line
Ctrl+E           # End of line
Ctrl+Y           # Previous page
Ctrl+V           # Next page
Ctrl+_           # Go to line number

# Editing
Ctrl+K           # Cut line
Ctrl+U           # Paste (Uncut)
Ctrl+6           # Mark text
Alt+U            # Undo
Alt+E            # Redo

# Search
Ctrl+W           # Search
Ctrl+\           # Replace
Alt+W            # Find next

# Help
Ctrl+G           # Get help

Customization for Different Use Cases

For Programming

# ~/.nanorc for programming
set autoindent
set linenumbers
set tabsize 4
set tabstospaces
set mouse
set smooth
set constantshow
include "/usr/share/nano/*.nanorc"

For System Administration

# ~/.nanorc for sysadmin
set backup
set backupdir "/home/user/.nano/backups"
set linenumbers
set constantshow
set mouse
set locking
include "/usr/share/nano/*.nanorc"

For Writing

# ~/.nanorc for writing
set fill 80
set justify
set smooth
set mouse
set speller "aspell -x -c"
set tempfile

Troubleshooting

Common Issues

  1. Nano not found

    # Install nano
    sudo apt install nano
  2. No syntax highlighting

    # Check if nanorc files exist
    ls /usr/share/nano/
    # Add to ~/.nanorc
    include "/usr/share/nano/*.nanorc"
  3. Can’t save file (permission denied)

    # Use sudo
    sudo nano /etc/hosts
    # Or change file permissions
    sudo chmod 644 filename.txt
  4. Accidental changes

    # Exit without saving
    Ctrl+X
    # Choose 'N' when asked to save
  5. Lost in large file

    # Go to specific line
    Ctrl+_
    # Enter line number

Recovery Options

# If nano crashes, look for backup files
ls ~/.nano/backups/
# or
ls /tmp/

# Recover from backup
nano ~/.nano/backups/filename.txt~

Nano excels as a straightforward, accessible text editor that doesn’t require learning complex commands or modes. It’s perfect for quick edits, system administration tasks, and users who prefer a simple, intuitive interface over the power and complexity of vim or emacs.