Complete Nano Editor Tutorial with Usage Examples
1. What is Nano?
Nano is a user-friendly text editor for Unix-like operating systems, primarily used in the command-line interface (CLI). It's designed to be simple and intuitive, making it a popular choice for beginners and those who need to quickly edit configuration files or short scripts directly in the terminal.
Unlike modal editors like Vim, Nano operates in a single, direct editing mode, similar to Notepad on Windows or TextEdit on macOS. Its most distinguishing feature is the always-visible list of common keyboard shortcuts at the bottom of the screen.
Why Use Nano?
- Ease of Use: Minimal learning curve, ideal for new Linux users.
- Intuitive Interface: On-screen help for basic commands.
- Lightweight: Small footprint, runs well on minimal systems.
- Ubiquitous: Often pre-installed on many Linux distributions (like Ubuntu, Debian, Kali).
2. Installing Nano
Nano is typically pre-installed on most Linux distributions. If it's not, you can easily install it using your distribution's package manager:
3. Opening and Creating Files
To open an existing file or create a new one, simply type `nano` followed by the filename in your terminal.
nano my_document.txt
- If `my_document.txt` exists, Nano will open it for editing.
- If `my_document.txt` does not exist, Nano will open a new, blank buffer with that name. The file will be created when you save it.
You can also open multiple files in separate buffers (tabs in graphical editors), though Nano's interface won't show them side-by-side like Vim splits. You'll switch between them.
nano file1.txt file2.txt file3.txt
To switch to the next buffer when multiple files are open, press Alt + > (or Alt + N). For the previous buffer, use Alt + < (or Alt + P).
4. Basic Navigation
Unlike Vim, you can use your **arrow keys** to move the cursor around in Nano. However, knowing the keyboard shortcuts can be faster for many users.
Nano's commands are shown at the bottom of the screen. `^` represents the Ctrl key, and `M-` represents the Alt key (or Esc followed by the key on some terminals).
- Arrow Keys: Move cursor one character/line at a time.
- Ctrl + f (Forward): Move cursor forward one character (same as Right Arrow).
- Ctrl + b (Backward): Move cursor backward one character (same as Left Arrow).
- Ctrl + n (Next): Move cursor to next line (same as Down Arrow).
- Ctrl + p (Previous): Move cursor to previous line (same as Up Arrow).
- Ctrl + a (Ahead): Move cursor to the beginning of the current line.
- Ctrl + e (End): Move cursor to the end of the current line.
- Ctrl + v (Page Down): Move forward one page.
- Ctrl + y (Page Up): Move backward one page.
- Alt + / or Ctrl + _ (Go To Line): Prompt for a specific line number to jump to.
- Alt + g (Go To Line): Same as Alt + /.
- Alt + j (Justify): Justify the entire paragraph.
5. Editing Text
Once you've opened a file in Nano, you can simply start typing. It works like a basic word processor.
- Typing: Just type your text.
- Backspace / Delete: Use these keys to delete characters.
- Inserting Lines: Press Enter to insert a new line.
- Deleting Characters: Use Backspace or Delete key.
- Deleting a Line: Press Ctrl + k (Cut Text) to cut the current line. If you don't paste it, it's effectively deleted.
6. Saving and Exiting
These are common operations, always visible at the bottom of the Nano interface.
- Ctrl + o (Write Out): Save the current file.
- Nano will prompt you for the filename at the bottom. Press Enter to confirm the current filename or type a new one.
- Ctrl + x (Exit): Exit Nano.
- If you have unsaved changes, Nano will ask if you want to save (`Y` for Yes, `N` for No, Ctrl + c to Cancel).
- Combined: You can typically press Ctrl + x, then `Y` (if prompted to save) and Enter to save and exit quickly.
# Open/create a file
nano example.txt
# Type some text...
This is a test document.
It contains some text.
# Press Ctrl+O to save. Confirm filename by pressing Enter.
# Press Ctrl+X to exit. If unsaved changes, press Y and Enter.
7. Searching and Replacing
Nano offers straightforward search and replace functionality.
- Ctrl + w (Where Is): Search for text.
- Type the text you want to search for at the prompt at the bottom.
- Press Enter. Nano will jump to the first occurrence.
- To find the next occurrence, press Alt + w.
- Ctrl + \ (Replace): Search and replace text.
- Nano will prompt for "Search (to replace)". Enter the text to find and press Enter.
- Then, it will prompt for "Replace with". Enter the replacement text and press Enter.
- You'll be given options: `Y` (Yes) to replace the current match, `N` (No) to skip, `A` (All) to replace all matches without asking, Ctrl + c to cancel.
# Inside nano...
# Sample text:
# This is line one.
# This is line two.
# This is line three.
# Press Ctrl+W, type "line", Enter. Cursor jumps to "line" in "line one".
# Press Alt+W. Cursor jumps to "line" in "line two".
# Press Ctrl+\, type "line", Enter.
# Type "row", Enter.
# Press 'Y' to replace "line one" with "row one".
# Press 'A' to replace "line two" and "line three" with "row two" and "row three" without further prompts.
# Result:
# This is row one.
# This is row two.
# This is row three.
8. Cut, Copy, and Paste
Nano uses "Cut" for both cutting and copying, and "Uncut" for pasting.
- Select Text (Mark Set):
- Move cursor to the start of the text you want to select.
- Press Ctrl + ^ (or Ctrl + 6) to set a mark.
- Move cursor to the end of the text you want to select (using arrow keys or other navigation). The text will be highlighted.
- Cut Selected Text:
- With text selected, press Ctrl + k. The selected text is cut and placed in Nano's buffer.
- Copy Selected Text:
- With text selected, press Alt + 6 (Copy Text). The selected text is copied to Nano's buffer.
- Cut Entire Line:
- Place cursor anywhere on the line. Press Ctrl + k. The entire line is cut.
- You can press Ctrl + k multiple times to cut multiple consecutive lines.
- Paste (Uncut) Text:
- Move cursor to where you want to paste.
- Press Ctrl + u (Uncut Text). The text from Nano's buffer is pasted.
# Inside nano...
# Current text:
# Line 1
# Line 2
# Line 3
# To copy "Line 2":
# 1. Move cursor to the beginning of "Line 2".
# 2. Press Ctrl+^ (or Ctrl+6) to set mark.
# 3. Move cursor to the end of "Line 2". (Text will be highlighted)
# 4. Press Alt+6 to copy the selection.
# 5. Move cursor to a new line.
# 6. Press Ctrl+U to paste.
# Result after pasting:
# Line 1
# Line 2
# Line 3
# Line 2
# To cut "Line 1":
# 1. Move cursor anywhere on "Line 1".
# 2. Press Ctrl+K. (Line 1 disappears)
# 3. Move cursor to the bottom.
# 4. Press Ctrl+U to paste.
# Result after pasting:
# Line 2
# Line 3
# Line 1
9. Undo and Redo
Nano has basic undo and redo functionality.
- Alt + u (Undo): Undo the last change.
- Alt + e (Redo): Redo the last undone change.
10. Common Keyboard Shortcuts (Visible at bottom)
Nano's most helpful feature is the constant display of common commands at the bottom of the screen. Here's a breakdown of what they mean:
- `^G` Get Help (Ctrl + g)
- `^X` Exit (Ctrl + x)
- `^O` Write Out (Ctrl + o) - Save
- `^R` Read File (Ctrl + r) - Insert another file into the current buffer
- `^W` Where Is (Ctrl + w) - Search
- `^K` Cut Text (Ctrl + k) - Cut current line or selected text
- `^U` Uncut Text (Ctrl + u) - Paste
- `^T` To Spell (Ctrl + t) - Spell check (if enabled)
- `^C` Cur Pos (Ctrl + c) - Display current cursor position (line/column)
- `^J` Justify (Ctrl + j) - Justify paragraph (wrap text)
- `^D` Delete (Ctrl + d) - Delete character under cursor (similar to Delete key)
- `^Y` Prev Pg (Ctrl + y) - Page Up
- `^V` Next Pg (Ctrl + v) - Page Down
- `^B` Back (Ctrl + b) - Left Arrow
- `^F` Forward (Ctrl + f) - Right Arrow
- `^P` Prev Line (Ctrl + p) - Up Arrow
- `^N` Next Line (Ctrl + n) - Down Arrow
- `^A` To Head (Ctrl + a) - Go to beginning of line
- `^E` To End (Ctrl + e) - Go to end of line
- `^\` Replace (Ctrl + \) - Search and Replace
- `M-W` Next (Alt + w) - Find next match
- `M-^` Copy (Alt + 6) - Copy selected text
- `M-U` Undo (Alt + u)
- `M-E` Redo (Alt + e)
- `M-S` Softwrap (Alt + s) - Toggle soft wrapping (lines break visually, not by newlines)
- `M-G` Go To Line (Alt + g) - Prompt for line/column number
- `M-M` Mouse Support (Alt + m) - Toggle mouse support (allows clicking to move cursor)
11. Nano Features and Options
Nano can be launched with various command-line options to alter its behavior.
- `--help`: Display Nano's help message.
nano --help
- `-c` or `--const`: Constantly display the cursor position.
nano -c my_file.txt
- `-l` or `--linenumbers`: Display line numbers on the left margin.
nano -l my_file.txt
- `-m` or `--mouse`: Enable mouse support (allows clicking to move the cursor).
nano -m my_file.txt
- `-w` or `--nowrap`: Disable softwrapping of long lines.
nano -w my_file.txt
- `-B` or `--backup`: When saving, make a backup of the previous version of the file.
- `-i` or `--autoindent`: Automatically indent new lines to the same level as the previous line.
- `-S` or `--softwrap`: Enable softwrapping of long lines (default behavior).
- `-v` or `--view`: Open the file in view-only (read-only) mode.
nano -v /etc/fstab
12. Nano Configuration File (.nanorc)
You can customize Nano's default behavior by creating or editing the `.nanorc` file in your home directory (`~/.nanorc`).
nano ~/.nanorc
Common `.nanorc` settings:
# Enable line numbers by default
set linenumbers
# Enable mouse support by default
set mouse
# Enable auto-indentation
set autoindent
# Set default tab size to 4 spaces instead of 8
set tabsize 4
set tabstospaces # Convert typed tabs to spaces
# Enable softwrapping (default)
set softwrap
# Enable syntax highlighting for specific file types
# include "/usr/share/nano/*.nanorc" # Uncomment or add specific includes
# For example, to enable Python syntax highlighting:
# include "/usr/share/nano/python.nanorc"
# Set a custom status bar format
# set statusformat "File: %f | Line: %l/%L | Col: %c"
After editing and saving `.nanorc`, restart Nano for changes to take effect.
13. Tips for Efficient Use
- Memorize Basic Shortcuts: Focus on `Ctrl+O` (Save), `Ctrl+X` (Exit), `Ctrl+W` (Search), `Ctrl+K` (Cut), `Ctrl+U` (Paste).
- Use `Ctrl` for actions, `Alt` for settings/features: This is a general pattern in Nano.
- Practice in a Dummy File: Create a temporary file to experiment with commands without worrying about damaging important data.
nano playground.txt
- Leverage the On-Screen Help: The bottom bar is your friend. It constantly reminds you of the most relevant commands.
- Use Search and Replace: For editing multiple occurrences of text, `Ctrl+\` is a huge time-saver.
- Customize with `.nanorc`: Set your preferred options so Nano behaves exactly how you want it to every time you open it.
Nano: The Friendly Terminal Editor!
Nano stands out for its simplicity and immediate usability, making it an excellent choice for anyone starting with Linux command-line text editing or needing a quick, no-fuss editor. While it lacks the advanced power of Vim, its straightforward interface and on-screen help ensure you're never lost. It's the perfect tool for quick edits, configuration file adjustments, and general text manipulation in a terminal environment.