nano
nano#
Concepts#
Why You Need a Terminal Text Editor#
Many Linux tasks require editing configuration files, scripts, or system files directly from the command line. You cannot always use a graphical editor — especially on servers without a desktop environment, over SSH, or when the desktop is broken.
The two most common terminal editors are nano (simple) and vim (powerful). This lesson covers nano.
What Is nano?#
nano is a straightforward, beginner-friendly terminal text editor. It is pre-installed on both Ubuntu and Debian. It shows a list of keyboard shortcuts at the bottom of the screen, so you do not need to memorize anything to get started.
Opening Files#
nano filename.txt # open a file (creates it if it doesn't exist)
nano +15 filename.txt # open and jump to line 15
nano -l filename.txt # show line numbers
sudo nano /etc/hostname # edit system files (requires sudo)
The nano Interface#
When you open a file, you see:
GNU nano 7.2 filename.txt
This is the file content.
You type here directly.
The cursor blinks where text will be inserted.
^G Help ^O Write Out ^W Where Is ^K Cut ^U Paste
^X Exit ^R Read File ^\ Replace ^J Justify ^T Spell
- Title bar (top) — shows the nano version and filename
- Editing area (middle) — the file content
- Shortcut bar (bottom) — keyboard shortcuts
^meansCtrl. So^OmeansCtrl + O.M-meansAlt(Meta key). SoM-UmeansAlt + U.
Essential Shortcuts#
File Operations#
| Shortcut | Action |
|---|---|
Ctrl + O |
Save (Write Out). Press Enter to confirm the filename. |
Ctrl + X |
Exit. If unsaved changes exist, it asks to save first. |
Ctrl + R |
Read/Insert a file at cursor position |
Navigation#
| Shortcut | Action |
|---|---|
| Arrow keys | Move cursor |
Ctrl + A |
Go to beginning of line |
Ctrl + E |
Go to end of line |
Ctrl + Y |
Page up |
Ctrl + V |
Page down |
Ctrl + _ (underscore) |
Go to a specific line number |
Alt + \ |
Go to beginning of file |
Alt + / |
Go to end of file |
Editing#
| Shortcut | Action |
|---|---|
Ctrl + K |
Cut the current line (or selection) |
Ctrl + U |
Paste (Uncut) the last cut text |
Alt + 6 |
Copy the current line (without cutting) |
Ctrl + Shift + K |
Delete the current line |
Alt + U |
Undo |
Alt + E |
Redo |
Alt + 3 |
Toggle comment/uncomment (prefixes with #) |
Search and Replace#
| Shortcut | Action |
|---|---|
Ctrl + W |
Search (Where Is). Type the term and press Enter. |
Alt + W |
Search for next occurrence |
Ctrl + \ |
Search and Replace |
Selection#
| Shortcut | Action |
|---|---|
Alt + A |
Start a selection (mark). Move cursor to expand selection. |
Ctrl + K |
Cut the selection |
Alt + 6 |
Copy the selection |
Ctrl + ^ (Ctrl + 6) |
Set mark (alternative) |
Configuration: ~/.nanorc#
You can customize nano’s behavior by creating or editing ~/.nanorc:
# Useful settings for ~/.nanorc
# Show line numbers
set linenumbers
# Enable mouse support
set mouse
# Use smooth scrolling
set smooth
# Set tab size to 4 spaces
set tabsize 4
# Convert tabs to spaces
set tabstospaces
# Enable auto-indentation
set autoindent
# Show cursor position in the status bar
set constantshow
# Enable syntax highlighting (usually enabled by default)
include "/usr/share/nano/*.nanorc"
Syntax Highlighting#
nano supports syntax highlighting for many languages. On both Ubuntu and Debian, highlighting files are in /usr/share/nano/. They are usually included by default. If not, add this to ~/.nanorc:
include "/usr/share/nano/*.nanorc"
Lab#
Exercise 1: Create and Edit a File#
# Open a new file
nano ~/lab_notes.txt
# Type the following text:
# This is my first nano edit.
# I am learning Linux.
# nano is simple and effective.
# Save: Ctrl+O, then Enter
# Exit: Ctrl+X
Exercise 2: Navigate a File#
# Open a long file to practice navigation
nano /etc/services
# Page down: Ctrl+V (several times)
# Page up: Ctrl+Y
# Go to line 100: Ctrl+_ then type 100 and Enter
# Go to beginning: Alt+\
# Go to end: Alt+/
# Exit without saving: Ctrl+X (press N if asked to save)
Exercise 3: Search and Replace#
# Open the file you created
nano ~/lab_notes.txt
# Search for "Linux": Ctrl+W, type "Linux", press Enter
# The cursor jumps to the match
# Search and Replace: Ctrl+\
# Search for: simple
# Replace with: straightforward
# Press Y to confirm (or A for all occurrences)
# Save and exit: Ctrl+O, Enter, Ctrl+X
Exercise 4: Cut, Copy, and Paste#
nano ~/lab_notes.txt
# Add a few more lines (type them in):
# Line four
# Line five
# Line six
# Cut a line: move to "Line five", press Ctrl+K
# (The line disappears — it's in the clipboard)
# Move to the end of the file
# Paste: Ctrl+U
# (Line five appears at the new location)
# Copy without cutting: move to a line, press Alt+6
# Move somewhere else and press Ctrl+U to paste
# Undo: Alt+U
# Save and exit
Exercise 5: Select a Block of Text#
nano ~/lab_notes.txt
# Place cursor at the beginning of a line
# Start selection: Alt+A
# Move cursor down 2 lines (the selected text is highlighted)
# Cut selection: Ctrl+K
# Move to a different line
# Paste: Ctrl+U
# Save and exit
Exercise 6: Configure nano#
# Create your nano configuration
nano ~/.nanorc
# Add these settings:
set linenumbers
set tabsize 4
set tabstospaces
set autoindent
set constantshow
include "/usr/share/nano/*.nanorc"
# Save and exit
# Now open a file — you should see line numbers and the cursor position
nano ~/lab_notes.txt
# Exit when done
Exercise 7: Edit a System File#
# Edit the hostname file (requires sudo)
sudo nano /etc/hostname
# Note the current hostname
# Do NOT change it — just observe
# Exit without saving: Ctrl+X
# View available syntax highlighting files
ls /usr/share/nano/
Clean Up#
rm ~/lab_notes.txt
Review#
1. How do you save a file in nano?
Ctrl + O (Write Out), then press Enter to confirm the filename.
2. How do you exit nano?
Ctrl + X. If there are unsaved changes, nano asks if you want to save (Y), discard (N), or cancel (Ctrl+C).
3. How do you search for text in nano?
Ctrl + W, type the search term, press Enter. Use Alt + W to find the next occurrence.
4. How do you undo an action in nano?
Alt + U for undo, Alt + E for redo.
5. What does `^` mean in nano's shortcut bar?
^ means the Ctrl key. So ^X means Ctrl + X and ^O means Ctrl + O.
6. How do you jump to a specific line number?
Ctrl + _ (Ctrl + underscore), type the line number, press Enter.
7. Where is nano's configuration file?
~/.nanorc (per-user) or /etc/nanorc (system-wide).
Previous: Building from Source | Next: vim