Complete Nano Editor Tutorial with Usage Examples

Table of Contents

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?

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

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).

5. Editing Text

Once you've opened a file in Nano, you can simply start typing. It works like a basic word processor.

6. Saving and Exiting

These are common operations, always visible at the bottom of the Nano interface.

# 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.

# 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.

# 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.

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:

11. Nano Features and Options

Nano can be launched with various command-line options to alter its behavior.

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

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.