nano
- Text Editor
1. Introduction
nano
(Nano’s ANOther editor) is a simple, user-friendly, modeless text editor designed for use in command-line environments on Linux and other Unix-like operating systems. It was inspired by the Pico text editor (part of the Pine email client) but is free software and includes several enhancements. nano
is often favored by beginners due to its straightforward interface, on-screen help for common commands, and lack of complex modes found in editors like Vim or Emacs.
Despite its simplicity, nano
is a capable editor for quick configuration file edits, writing scripts, or composing short text documents directly in the terminal. This guide provides an in-depth look at nano
, covering its basic usage, editing commands, command-line options, and customization. Whether you’re editing a single line in a config file or drafting a multi-page document, nano
offers an accessible entry point into terminal-based text editing.
2. Basic Syntax
To start nano
, you typically invoke it followed by the name of the file you wish to edit or create:
# Basic syntax for invoking nano
$ nano [OPTIONS] [[+LINE[,COLUMN]] FILE]...
[OPTIONS]...
: Optional flags that modify nano’s behavior or initial settings. These can be single letters (e.g.,-l
) or long-form (e.g.,--linenumbers
), often used to tweak how nano starts or operates.[[+LINE[,COLUMN]] FILE]...
:FILE
: The name of the file to open. If the file doesn’t exist, nano will create it when you save. You can specify multiple files to open them in different buffers (if-F
or--multibuffer
is enabled or set as default).+LINE[,COLUMN]
: Optional. Opens the file and places the cursor at the specifiedLINE
number. If,COLUMN
is also given, it places the cursor at that column on the specified line. Negative numbers count from the end of the file or line (e.g.,+-2
for the second-to-last line).- Instead of
+LINE
, you can use+[cCrR](/|?)STRING
to open the file and place the cursor at the first or last occurrence ofSTRING
. This is useful for jumping to specific content immediately.
If nano
is invoked without a filename, it opens an empty, unnamed buffer where you can start typing. If a dash (-
) is given as the filename, nano
reads data from standard input, allowing you to edit piped content.
The Nano Interface:
When nano
starts, you’ll see:
- Title Bar: Displays the nano version (e.g., “GNU nano 7.2”) and the filename being edited (or “New Buffer” if unnamed).
- Editing Area: The main space where you type and edit text. This is where the file content appears or where you input new text.
- Status Bar (Optional): Can show messages (e.g., ”[Wrote 5 lines]”) or cursor position (with
-c
or--constantshow
). It’s usually blank unless triggered. - Shortcut Key Help: The bottom two or three lines display common commands.
^
denotes the Ctrl key (e.g.,^X
for Exit), andM-
denotes the Alt or Meta key (e.g.,M-U
for Undo). These shortcuts are always visible unless hidden with-x
.
3. Core Use Cases with Examples
Let’s set up a working environment to demonstrate nano’s core functionalities.
# Create a directory for nano examples
$ mkdir nano_examples_dir
$ cd nano_examples_dir
# Create a sample config file with initial content
$ echo "This is the first line of my_config.conf." > my_config.conf
$ echo "Another setting = value" >> my_config.conf
3.1 Opening an Existing File for Editing
# Open my_config.conf with nano
$ nano my_config.conf
Output:
nano will open, displaying the content of my_config.conf
:
This is the first line of my_config.conf.
Another setting = value
You can now edit the text using the keyboard.
Common Actions:
- Saving: Press
Ctrl+O
(Write Out). Nano will prompt for the filename to write to (defaults tomy_config.conf
). PressEnter
to confirm. - Exiting: Press
Ctrl+X
(Exit). If you’ve made unsaved changes, nano will ask: “Save modified buffer? (Y/N/C)” (Yes/No/Cancel). Choose accordingly.
Explanation: This is the most basic use of nano—opening an existing file to make changes. It’s ideal for tweaking configuration files or logs.
3.2 Creating a New File
# Start nano to create a new file named my_script.sh
$ nano my_script.sh
Output:
nano opens with an empty buffer. The title bar shows “File: my_script.sh”.
Action:
Type some content, e.g.:
#!/bin/bash
echo "Hello, World!"
Then:
- Press
Ctrl+O
, accept the default filename (my_script.sh
), and pressEnter
to save. - Press
Ctrl+X
to exit.
Verification:
# Check the file content
$ cat my_script.sh
#!/bin/bash
echo "Hello, World!"
Explanation: This demonstrates creating a new file from scratch, useful for writing scripts or notes directly in the terminal.
3.3 Opening a File at a Specific Line Number
This is useful for quickly jumping to a known location in a larger file.
# Create a longer file for demonstration
$ printf "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10\n" > long_file.txt
# Open long_file.txt and place the cursor on line 7
$ nano +7 long_file.txt
Output:
nano opens long_file.txt
with the cursor at the beginning of “Line 7”.
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Explanation: This is handy for debugging code or reviewing logs where you know the line number of interest.
3.4 Opening a File at a Specific Line and Column
# Open long_file.txt, go to line 3, column 5
$ nano +3,5 long_file.txt
Output:
nano opens long_file.txt
with the cursor on line 3, positioned before the 5th character (“e” in “Line 3”).
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Explanation: This precise positioning is useful for editing specific parts of a line, like fixing a typo or adjusting a value.
3.5 Opening a File and Searching for a String
You can open a file and have nano
immediately position the cursor at the first occurrence of a specific string.
# Add a specific string to my_config.conf
$ echo "ImportantSetting = true" >> my_config.conf
# Open my_config.conf and jump to the first occurrence of "ImportantSetting" (case-sensitive)
$ nano +c/"ImportantSetting" my_config.conf
Output:
nano opens my_config.conf
with the cursor at the beginning of “ImportantSetting”.
This is the first line of my_config.conf.
Another setting = value
ImportantSetting = true
Search Modifiers for +/STRING
or +?STRING
:
+/STRING
: Search forward forSTRING
.+?STRING
: Search backward forSTRING
.+c/STRING
: Case-sensitive search.+C/STRING
: Case-insensitive search (default unlesscasesensitive
is set innanorc
).+r/STRING
: InterpretSTRING
as a regular expression.+R/STRING
: Do not interpretSTRING
as a regular expression (default unlessregexp
is set innanorc
).
You can combine these, e.g., +cr/"[Ii]mportant[Ss]etting"
for a case-sensitive regex search. If the string contains spaces, enclose it in quotes:
# Example with spaces
$ nano +c/"Another setting" my_config.conf
Explanation: This feature saves time when you need to edit a specific section of a file identified by a keyword or phrase.
3.6 Reading from Standard Input
nano
can edit content piped from another command or read directly from standard input if -
is given as the filename.
# Edit the output of 'ls -l' (or 'dir' on some systems)
$ ls -l | nano -
Output:
nano opens with the piped content, e.g.:
dir nano_examples_dir
file my_config.conf
file long_file.txt
Action:
Edit the content as needed, then press Ctrl+O
to save to a file (nano will prompt for a filename), and Ctrl+X
to exit.
Explanation: This is useful for processing command output (e.g., logs or lists) before saving or further piping.
4. Key Options Explained (with Examples)
nano
has many command-line options to customize its startup behavior. Below, each key option is explained with examples distinct from the core use cases above.
4.1 -A, —smarthome
Purpose: Makes the Home key behave “smarter.” When pressed on a line not at the very beginning of non-whitespace characters, the cursor jumps to that beginning. If already there, it jumps to the true beginning of the line.
Syntax:
$ nano -A filename
Use Case: More intuitive navigation to the start of indented code or text, especially in programming or formatted documents.
Example:
# Create a file with an indented line
$ echo -e "First Line\n Indented Line" > smart_home_test.txt
# Open with smarthome enabled
$ nano -A smart_home_test.txt
Action:
- Place the cursor in the middle of “Indented Line” (e.g., on “t”).
- Press
Home
: Cursor jumps to before “Indented”. - Press
Home
again: Cursor jumps to the start of the line (before the spaces).
Output:
nano opens with:
First Line
Indented Line
Explanation: Without -A
, Home
always jumps to the absolute start of the line. With -A
, it’s context-aware, enhancing navigation in structured text.
4.2 -B, —backup
Purpose: When saving a file, nano
creates a backup of the previous version. The backup filename is the original filename suffixed with a tilde (~
).
Syntax:
$ nano -B filename
Use Case: Simple, automatic backup for important files before overwriting them, providing a safety net against accidental data loss.
Example:
# Create a file to edit
$ echo "Original content" > backup_test.txt
# Open with backup enabled
$ nano -B backup_test.txt
Action:
- Edit the file to “New content”.
- Save with
Ctrl+O
and exit withCtrl+X
.
Verification:
# Check directory contents
$ ls
backup_test.txt backup_test.txt~
# Check backup content
$ cat backup_test.txt~
Original content
# Check updated file
$ cat backup_test.txt
New content
Explanation: The ~
backup preserves the original state, useful for critical files like system configs.
4.3 -C directory, —backupdir=directory
Purpose: Used in conjunction with -B
(backups enabled). Instead of just one ~
backup, this option creates uniquely numbered backup files each time the file is saved, and stores them in the specified directory.
Syntax:
$ nano -B -C ./my_backups filename
Use Case: Maintaining a versioned history of backups in a dedicated directory, ideal for tracking changes over time.
Example:
# Create a backup directory
$ mkdir nano_backups
# Create a file to edit
$ echo "Version 1" > file_alpha.txt
# Edit with numbered backups
$ nano -B -C ./nano_backups file_alpha.txt
Action:
- Change to “Version 2”, save, and exit.
- Reopen, change to “Version 3”, save, and exit.
Verification:
# List backups
$ ls nano_backups/
file_alpha.txt.~1~ file_alpha.txt.~2~
# Check backup contents
$ cat nano_backups/file_alpha.txt.~1~
Version 1
$ cat nano_backups/file_alpha.txt.~2~
Version 2
$ cat file_alpha.txt
Version 3
Explanation: Each save creates a new backup, numbered sequentially, allowing you to revert to any previous version.
4.4 -D, —boldtext
Purpose: Uses bold text instead of reverse video (highlighted background) for UI elements like the title bar and status bar. Can be overridden by nanorc
color settings.
Syntax:
$ nano -D filename
Use Case: Primarily for visual preference or if reverse video is problematic on a specific terminal (e.g., poor contrast).
Example:
# Open a file with bold UI elements
$ nano -D notes.txt
Output:
nano opens with the title bar and status messages in bold text rather than reverse video, depending on terminal support.
Explanation: This is a cosmetic tweak for better readability or compatibility with certain terminal emulators.
4.5 -E, —tabstospaces
Purpose: Converts any Tab key presses into the equivalent number of spaces (defined by --tabsize
). Existing tabs in the file are not converted unless you manually edit them.
Syntax:
$ nano -E filename
Use Case: Enforcing space-based indentation while editing, common in some coding styles (e.g., Python).
Example:
# Open a new file with tabs-to-spaces enabled
$ nano -E coding_test.txt
Action:
- Press
Tab
: Inserts spaces (default 8, or as set by-T
). - Type “def main():”, press
Enter
, pressTab
, type “pass”. - Save and exit.
Verification:
$ cat coding_test.txt
def main():
pass
Explanation: Ensures consistent spacing, avoiding tab characters that might render differently across systems.
4.6 -F, —multibuffer
Purpose: When multiple files are specified on the command line, or when opening a file with Ctrl+R
(Read File), this option makes nano
read each file into its own separate buffer. Without it (or if disabled by nanorc
), nano
might insert files into the current buffer.
Syntax:
$ nano -F file1.txt file2.txt
Use Case: Editing multiple files simultaneously and switching between them (using M-<
and M->
by default). This is the default behavior in modern nano
.
Example:
# Create two files
$ echo "File A" > file_a.txt
$ echo "File B" > file_b.txt
# Open both with multibuffer
$ nano -F file_a.txt file_b.txt
Action:
- Nano opens with
file_a.txt
. - Press
M->
(Alt+RightArrow) to switch tofile_b.txt
. - Press
M-<
(Alt+LeftArrow) to switch back.
Explanation: Multibuffer mode enhances productivity when working on related files, like code and documentation.
4.7 -G, —locking
Purpose: Enables Vim-style file locking. Creates a .filename.swp
type lock file to prevent simultaneous editing by different nano
instances (or other editors respecting this lock type).
Syntax:
$ nano -G filename
Use Case: In environments where multiple users or processes might try to edit the same file concurrently, to prevent data corruption.
Example:
# Open a file with locking
$ nano -G shared_doc.txt
Action:
- In another terminal, try opening the same file:
$ nano -G shared_doc.txt
Output:
nano warns: “File ‘shared_doc.txt’ is being edited; continue? (y/n)“. Choosing “n” avoids conflicts.
Explanation: Locking ensures data integrity in multi-user or multi-session scenarios.
4.8 -H, —historylog
Purpose: Saves the last hundred search strings, replacement strings, and executed commands (like “Go to line”) to ~/.nano/search_history
(or a similar path based on XDG specs). These are then available for recall in subsequent nano
sessions.
Syntax:
$ nano -H filename
Use Case: Conveniently reuse previous search terms or commands, saving time on repetitive tasks.
Example:
# Open with history logging
$ nano -H history_test.txt
Action:
- Search for “test” with
Ctrl+W
. - Go to line 5 with
Ctrl+_
. - Save and exit.
- Reopen:
$ nano -H history_test.txt
- Press
Ctrl+W
, then↑
to recall “test”. - Press
Ctrl+_
, then↑
to recall “5”.
Explanation: History persists across sessions, streamlining workflows.
4.9 -I, —ignorercfiles
Purpose: Tells nano
not to read any system-wide (/etc/nanorc
) or user-specific (~/.nanorc
or ~/.config/nano/nanorc
) configuration files. nano
will start with its built-in defaults.
Syntax:
$ nano -I filename
Use Case: Troubleshooting issues that might be caused by a nanorc
setting, or ensuring a “vanilla” nano
experience.
Example:
# Open without custom configs
$ nano -I plain_text.txt
Output:
nano opens with default settings (e.g., no syntax highlighting unless specified with -Y
).
Explanation: Useful for isolating behavior or ensuring consistency across systems.
4.10 -J number, —guidestripe=number
Purpose: Draws a vertical guiding stripe at the specified column number.
Syntax:
$ nano -J 80 code.py
Use Case: Helps visually align text or code, for example, to adhere to an 80-column line limit. The color can be set in nanorc
.
Example:
# Open with a guide stripe at column 80
$ nano -J 80 alignment_test.py
Output:
nano shows a vertical line at column 80, aiding in keeping lines within that limit.
Explanation: Enhances readability and adherence to coding standards.
4.11 -K, —rawsequences
Purpose: Interpret keyboard escape sequences directly instead of relying on the ncurses library. Disables mouse support.
Syntax:
$ nano -K filename
Use Case: Troubleshooting keyboard issues on problematic terminals. If you need this, it might indicate a terminal configuration problem.
Example:
# Open with raw keyboard sequences
$ nano -K raw_input.txt
Explanation: Rarely needed unless arrow keys or other inputs misbehave due to terminal quirks.
4.12 -L, —nonewlines
Purpose: Prevents nano
from automatically adding a newline character at the end of the file if one is not already present when saving. POSIX text files are expected to end with a newline.
Syntax:
$ nano -L data.bin
Use Case: Editing files where a trailing newline is undesirable (e.g., certain data files, or when the exact byte stream matters).
Example:
# Create a file without a newline
$ echo -n "No newline" > no_newline.txt
# Edit without adding newline
$ nano -L no_newline.txt
Action:
- Add ” here” and save.
Verification:
$ cat no_newline.txt
No newline here
Explanation: Preserves exact file structure, critical for some binary or script formats.
4.13 -M, —trimblanks
Purpose: When lines are hard-wrapped (either automatically with -b
or manually with M-J
), this option snips trailing whitespace from the end of the line before it’s wrapped to the next.
Syntax:
$ nano -M filename
Use Case: Keeping lines clean of unnecessary trailing spaces during wrapping, improving file cleanliness.
Example:
# Open with trim blanks
$ nano -M trim_test.txt
Action:
- Type “This is a long line ” (with trailing spaces).
- Press
M-J
to justify/wrap. - Save and exit.
Verification:
$ cat trim_test.txt
This is a long
line
Explanation: Removes trailing spaces, ensuring tidy wrapped text.
4.14 -N, —noconvert
Purpose: Disables automatic conversion of file line endings from DOS (CRLF) or Mac (CR) format to Unix (LF) format when a file is read.
Syntax:
$ nano -N windows_file.txt
Use Case: When you need to preserve the original line endings of a file created on a different operating system.
Example:
# Create a file with DOS endings
$ printf "Line 1\r\nLine 2\r\n" > dos_file.txt
# Open without conversion
$ nano -N dos_file.txt
Action:
- Edit and save; endings remain CRLF.
Explanation: Maintains compatibility with non-Unix systems.
4.15 -O, —morespace
Purpose: Obsolete and ignored. Modern nano
includes the line below the title bar in the editing space by default. Use -e
or --emptyline
if you want that line blank.
Syntax:
$ nano -O filename
Explanation: Included for completeness; has no effect in current versions.
4.16 -P, —positionlog
Purpose: For the 200 most recently opened files, nano
logs the last cursor position. When reopening one of these files, the cursor is placed at that saved position.
Syntax:
$ nano -P filename
Use Case: Conveniently resume editing where you left off in frequently accessed files.
Example:
# Open with position logging
$ nano -P resume_test.txt
Action:
- Move cursor to line 3, column 4.
- Save and exit.
- Reopen:
$ nano -P resume_test.txt
Output:
Cursor returns to line 3, column 4.
Explanation: Enhances workflow continuity.
4.17 -Q “regex”, —quotestr=“regex”
Purpose: Sets the regular expression used to identify quoted text (e.g., in emails) or comment blocks in code. This helps features like rejustification (Ctrl+J
) work correctly on these blocks.
Syntax:
$ nano -Q "^> " email.txt
Use Case: Customizing how nano
handles reformatting of quoted replies in emails or comment blocks in specific programming languages.
Example:
# Create an email-style file
$ echo -e "> Quoted text here\n> Another line" > email_reply.txt
# Open with quote string set
$ nano -Q "^> " email_reply.txt
Action:
- Press
Ctrl+J
to rejustify only the quoted block.
Explanation: Ensures proper handling of structured text.
4.18 -R, —restricted
Purpose: Runs nano
in a restricted mode. In this mode, nano
will not:
- Read or write history files.
- Allow suspending (
Ctrl+Z
). - Allow spell checking.
- Allow appending, prepending, or saving under a different name if the file already has one.
- Make backup files.
- Read or write to any file not specified on the command line.
Can also be activated by invoking nano
as rnano
.
Syntax:
$ nano -R filename
# OR
$ rnano filename
Use Case: Providing a limited editing capability, for example, in environments where file access needs to be strictly controlled.
Example:
# Open in restricted mode
$ nano -R restricted_file.txt
Explanation: Limits functionality for security or simplicity.
4.19 -S, —smooth
Purpose: Obsolete and ignored. Smooth (linewise) scrolling is now the default. Use -j
or --jumpyscrolling
for the old half-screen scrolling.
Syntax:
$ nano -S filename
Explanation: No effect in modern versions; included for historical context.
4.20 -T number, —tabsize=number
Purpose: Sets the width of a tab character to number
space columns. Must be greater than 0. Default is 8.
Syntax:
$ nano -T 4 code.py
Use Case: Adjusting tab display width to match project coding standards or personal preference.
Example:
# Open with tab size 4
$ nano -T 4 tab_test.py
Action:
- Press
Tab
, type “pass”. - Save and exit.
Verification:
$ cat tab_test.py
pass
Explanation: Customizes tab rendering for consistency.
4.21 -U, —quickblank
Purpose: Makes status bar messages disappear after 1 keystroke instead of the default 25. Overridden by -c
(--constantshow
).
Syntax:
$ nano -U filename
Use Case: For users who prefer status messages to be less persistent, reducing screen clutter.
Example:
# Open with quick blanking
$ nano -U quick_test.txt
Action:
- Save with
Ctrl+O
; message ”[Wrote X lines]” vanishes after one keypress.
Explanation: Speeds up message dismissal.
4.22 -V, —version
Purpose: Shows nano
’s version number and build information, then exits.
Syntax:
$ nano -V
Output:
GNU nano, version 7.2
(C) 1999-2011, 2013-2023 Free Software Foundation, Inc.
...
Explanation: Useful for checking installed version.
4.23 -W, —wordbounds
Purpose: Detects word boundaries differently by treating punctuation characters as part of a word. Affects word-wise cursor movement (Ctrl+Left/Right
) and selection.
Syntax:
$ nano -W document.txt
Use Case: When editing prose or text where punctuation should be considered part of words for navigation.
Example:
# Create a file with punctuation
$ echo "Hello, world!" > punctuation.txt
# Open with word bounds
$ nano -W punctuation.txt
Action:
- Press
Ctrl+Right
from “H”: Cursor moves to end of “Hello,” (including comma).
Explanation: Adjusts navigation for text with punctuation.
4.24 -X “characters”, —wordchars=“characters”
Purpose: Specifies additional characters (besides alphanumeric) that should be considered part of a word. Overrides -W
.
Syntax:
$ nano -X "_-" script.sh
Use Case: Customizing word navigation for specific programming languages or file types where identifiers might include special characters (e.g., _
or -
).
Example:
# Create a script with underscores
$ echo "my_variable_name" > script_test.sh
# Open with custom word chars
$ nano -X "_" script_test.sh
Action:
- Press
Ctrl+Right
from “m”: Cursor moves to end of “my_variable_name”.
Explanation: Tailors word navigation to specific needs.
4.25 -Y name, —syntax=name
Purpose: Explicitly specifies the syntax highlighting definition name to use for the current file(s). The name should correspond to a syntax defined in a nanorc
file (e.g., “c”, “python”, “html”).
Syntax:
$ nano -Y python my_script.py
Use Case: Overriding automatic syntax detection or applying highlighting to a file with an unusual extension.
Example:
# Open with Python syntax highlighting
$ nano -Y python custom_script.txt
Output:
nano highlights Python keywords (e.g., def
, if
) even if the file isn’t .py
.
Explanation: Ensures correct highlighting for editing.
4.26 -Z, —zap
Purpose: Allows an unmodified Backspace
or Delete
key press to erase the entire marked (selected) region, instead of just a single character. Does not affect the cutbuffer.
Syntax:
$ nano -Z filename
Use Case: Quickly deleting selected blocks of text without extra steps.
Example:
# Open with zap enabled
$ nano -Z zap_test.txt
Action:
- Type “Delete this text”.
- Mark “this” with
M-A
, pressDelete
: “this” is removed instantly.
Explanation: Streamlines text deletion.
4.27 -a, —atblanks
Purpose: When soft line wrapping (-$
or --softwrap
) is enabled, this option makes lines wrap at whitespace boundaries (spaces, tabs) instead of abruptly at the screen edge.
Syntax:
$ nano -$a long_prose.txt
Use Case: More aesthetically pleasing soft wrapping for prose or comments, improving readability.
Example:
# Create a long line
$ echo "This is a long line with many words to demonstrate wrapping behavior" > wrap_test.txt
# Open with softwrap and atblanks
$ nano -$a wrap_test.txt
Output:
Text wraps at spaces, not mid-word.
Explanation: Enhances display of long text.
4.28 -b, —breaklonglines
Purpose: Automatically hard-wraps lines when they become longer than the wrapping width (see -r
or --fill
). This is the opposite of -w
(--nowrap
). The last one given takes effect. Modern nano
defaults to not breaking long lines (--nowrap
).
Syntax:
$ nano -b text_to_wrap.txt
Use Case: For users who prefer automatic hard line wrapping as they type, similar to older Pico behavior.
Example:
# Open with hard wrapping
$ nano -b -r 20 wrap_hard.txt
Action:
- Type “This is a very long line to wrap”.
- Save and exit.
Verification:
$ cat wrap_hard.txt
This is a very long
line to wrap
Explanation: Inserts actual newlines, altering file content.
4.29 -c, —constantshow
Purpose: Constantly displays the cursor’s line number, column number, and character position on the status bar. Overrides -U
(--quickblank
).
Syntax:
$ nano -c filename
Use Case: For users who always want to see the precise cursor location, useful in large files.
Example:
# Open with constant cursor display
$ nano -c position_test.txt
Output:
Status bar shows, e.g., “L:1 C:1 Ch:1” and updates as you move.
Explanation: Provides ongoing positional feedback.
4.30 -d, —rebinddelete
Purpose: Reinterprets Delete
and Backspace
keys to ensure both work as expected (Delete
deletes character under/after cursor, Backspace
deletes character before).
Syntax:
$ nano -d filename
Use Case: Only needed if your terminal or keyboard configuration causes Delete
to act like Backspace
, or vice-versa.
Example:
# Open with delete rebinding
$ nano -d fix_keys.txt
Explanation: Fixes rare key mapping issues.
4.31 -e, —emptyline
Purpose: Leaves the line below the title bar (sometimes called the “key bar” or “shortcut bar”) entirely blank, instead of it being part of the editing area.
Syntax:
$ nano -e filename
Use Case: For users who prefer the older Pico-style interface with a distinct empty line below the title.
Example:
# Open with empty line
$ nano -e old_style.txt
Output:
Extra blank line appears below title bar.
Explanation: Cosmetic adjustment for familiarity.
4.32 -f file, —rcfile=file
Purpose: Read nano
’s configuration options, syntax definitions, and key bindings only from the specified file, ignoring the system-wide nanorc
and the user’s personal nanorc
file.
Syntax:
$ nano -f /path/to/my_custom.nanorc filename
Use Case: Testing specific configurations, using project-specific nano
settings, or running nano
in a restricted environment where user/system nanorc
files should be ignored.
Example:
# Use a custom config
$ nano -f ./custom.nanorc test_file.txt
Explanation: Allows isolated customization.
4.33 -g, —showcursor
Purpose: Makes the cursor visible in the file browser (Ctrl+R
then Ctrl+T
) and in the help viewer (Ctrl+G
), placing it on the highlighted item.
Syntax:
$ nano -g filename
Use Case: Primarily benefits users of braille devices or those with visual impairments.
Example:
# Open with cursor visibility
$ nano -g assist_test.txt
Explanation: Improves accessibility.
4.34 -h, —help
Purpose: Displays a summary of available command-line options and exits.
Syntax:
$ nano -h
Output:
Usage: nano [OPTIONS] [[+LINE[,COLUMN]] FILE]...
...
Explanation: Quick reference for options.
4.35 -i, —autoindent
Purpose: Automatically indents a newly created line (when Enter
is pressed) to match the indentation (tabs and/or spaces) of the previous line. If the previous line is the start of a paragraph, it indents to match the next line.
Syntax:
$ nano -i code_file.py
Use Case: Very helpful for writing code or structured text, maintaining consistent indentation.
Example:
# Open with autoindent
$ nano -i indent_test.py
Action:
- Type “def test():”, press
Enter
, type “pass”.
Verification:
$ cat indent_test.py
def test():
pass
Explanation: Simplifies coding by auto-aligning lines.
4.36 -j, —jumpyscrolling
Purpose: Scrolls the buffer contents by half-screens at a time, instead of line-by-line (smooth scrolling, which is the default in modern nano
).
Syntax:
$ nano -j filename
Use Case: For users who prefer the older, chunkier scrolling behavior.
Example:
# Open with jumpy scrolling
$ nano -j jumpy_test.txt
Explanation: Alters scrolling feel.
4.37 -k, —cutfromcursor
Purpose: Modifies the “Cut Text” command (Ctrl+K
). Instead of cutting the entire current line, it cuts from the current cursor position to the end of the line.
Syntax:
$ nano -k filename
Use Case: More precise cutting of text from the cursor onwards without affecting the beginning of the line.
Example:
# Open with cut from cursor
$ nano -k cut_test.txt
Action:
- Type “Keep this, cut this”.
- Move cursor to comma, press
Ctrl+K
.
Verification:
Keep this,
Explanation: Offers finer control over cuts.
4.38 -l, —linenumbers
Purpose: Displays line numbers in a column to the left of the text editing area.
Syntax:
$ nano -l script.sh
Use Case: Essential for code editing, debugging, or when referencing specific lines in a document.
Example:
# Open with line numbers
$ nano -l numbered_test.sh
Output:
1 #!/bin/bash
2 echo "Line two"
Explanation: Aids in navigation and reference.
4.39 -m, —mouse
Purpose: Enables mouse support if available for your terminal system (e.g., in X Window, or on the console with gpm running). Allows using the mouse to:
- Place the cursor.
- Set the mark (usually with a double-click).
- Execute shortcuts displayed at the bottom.
Text can still be selected by dragging while holding Shift
.
Syntax:
$ nano -m filename
Use Case: For users who prefer mouse interaction within the terminal editor.
Example:
# Open with mouse support
$ nano -m mouse_test.txt
Action:
- Click to move cursor.
- Double-click to mark text.
Explanation: Adds GUI-like functionality.
4.40 -n, —noread
Purpose: Treat any filename given on the command line as a new, empty file, even if a file with that name already exists. This allows nano
to write to named pipes or special files without trying to read their (potentially problematic) existing content first.
Syntax:
$ nano -n /path/to/my_pipe
Use Case: Using nano
as an editor in a pipeline, for example, with gpg
to edit encrypted content without writing sensitive unencrypted data to a temporary disk file.
Example:
# Create a named pipe (conceptual example)
$ mkfifo mypipe
# Edit via pipe
$ nano -n mypipe
Action:
- Type content, save, and exit; content goes to the pipe.
Explanation: Supports advanced workflows.
4.41 -o directory, —operatingdir=directory
Purpose: Sets an “operating directory.” nano
will behave somewhat like it’s chroot
ed into this directory, restricting file browsing and saving operations relative to this path.
Syntax:
$ nano -o /home/user/projectX main.c
Use Case: Limiting nano
’s file access scope, perhaps for security or to simplify navigation within a large project.
Example:
# Restrict to a directory
$ nano -o ./project project_file.txt
Explanation: Enhances security or focus.
4.42 -p, —preserve
Purpose: Preserves the XON (Ctrl+Q
) and XOFF (Ctrl+S
) sequences, allowing them to be caught by the terminal for software flow control, instead of being used by nano
(e.g., Ctrl+S
for save in some contexts, though Ctrl+O
is standard in nano
).
Syntax:
$ nano -p filename
Use Case: On older systems or specific terminal configurations where XON/XOFF flow control is essential and might conflict with nano
’s default key bindings.
Example:
# Open with flow control preserved
$ nano -p flow_test.txt
Explanation: Rare but useful for legacy systems.
4.43 -r number, —fill=number
Purpose: Sets the target column width for justifying text (Ctrl+J
) and for automatic hard-wrapping (if -b
is enabled).
- If
number
is positive, it’s the absolute column number. - If
number
is 0 or negative, wrapping occurs atscreen_width - |number|
columns. Default is-8
.
Syntax:
$ nano -r 72 document.txt
Use Case: Formatting text to a specific line width, e.g., for emails or documentation.
Example:
# Open with 72-column fill
$ nano -r 72 format_test.txt
Action:
- Type a long paragraph, press
Ctrl+J
.
Explanation: Controls text width.
4.44 -s “program [args…]”, —speller=“program [args…]”
Purpose: Specifies an external program (and its arguments) to use for spell checking (Ctrl+T
), instead of nano
’s built-in method (which typically calls hunspell
or spell
).
Syntax:
$ nano -s "aspell -c" report.txt
Use Case: Using a preferred external spell checker like aspell
or ispell
with custom options.
Example:
# Open with aspell spell checker
$ nano -s "aspell -c" spell_test.txt
Action:
- Type “teh”, press
Ctrl+T
to check spelling.
Explanation: Integrates custom tools.
4.45 -t, —tempfile
Purpose: When exiting nano
with Ctrl+X
after making changes, this option causes nano
to save the buffer automatically without prompting for confirmation or filename. Use with caution, as it can lead to accidental overwrites if you’re not careful about the filename nano
is using.
Syntax:
$ nano -t my_notes.txt
Use Case: In scripts or situations where you want to quickly save changes on exit without interaction, assuming the filename is already correct.
Example:
# Open with auto-save on exit
$ nano -t auto_save.txt
Action:
- Edit, press
Ctrl+X
; saves automatically.
Explanation: Speeds up exit process.
4.46 -u, —unix
Purpose: Saves files by default in Unix format (LF line endings). This overrides nano
’s default behavior of trying to save a file in the format it was read in (e.g., preserving CRLF if it was a DOS-format file). Has no effect if --noconvert
(-N
) is also used.
Syntax:
$ nano -u windows_text_file.txt
Use Case: Ensuring files are saved with Unix line endings, especially when working in mixed-OS environments.
Example:
# Open DOS file and force Unix endings
$ nano -u dos_to_unix.txt
Explanation: Standardizes line endings.
4.47 -v, —view
Purpose: Opens the file(s) in read-only mode. Editing is disallowed. This is equivalent to invoking nano
as nview
.
Syntax:
$ nano -v important_config.conf
# OR
$ nview important_config.conf
Use Case: Safely viewing sensitive configuration files or any file you don’t want to accidentally modify.
Example:
# View a file read-only
$ nano -v /etc/hosts
Output:
File displays but cannot be edited.
Explanation: Protects against unintended changes.
4.48 -w, —nowrap
Purpose: Disables automatic hard-wrapping of long lines. Lines will extend beyond the screen width and you’ll need to scroll horizontally. This is the default behavior in modern nano
. It’s the opposite of -b
(--breaklonglines
).
Syntax:
$ nano -w code_with_long_lines.c
Use Case: Preferred for editing source code or configuration files where hard line breaks are undesirable.
Example:
# Open without wrapping
$ nano -w nowrap_test.c
Action:
- Type a long line; it extends off-screen.
Explanation: Preserves line integrity.
4.49 -x, —nohelp
Purpose: Hides the two help lines (key shortcuts) normally displayed at the bottom of the nano
screen.
Syntax:
$ nano -x filename
Use Case: Maximizing editing space for users who are already familiar with nano
’s shortcuts.
Example:
# Open without help lines
$ nano -x expert_mode.txt
Output:
More editing space, no shortcut display.
Explanation: Cleaner interface for pros.
4.50 -y, —afterends
Purpose: Makes Ctrl+RightArrow
(next word) and Ctrl+LeftArrow
(previous word) stop at word ends instead of word beginnings.
Syntax:
$ nano -y filename
Use Case: Personal preference for word navigation behavior.
Example:
# Open with afterends navigation
$ nano -y nav_test.txt
Action:
- Type “word1 word2”, press
Ctrl+Right
; stops after “word1”.
Explanation: Adjusts navigation style.
4.51 -$ (dollar sign), —softwrap
Purpose: Enables soft wrapping. Lines longer than the screen width are displayed wrapped onto multiple screen lines without inserting actual newline characters into the file. Use -$
or pass it as a separate argument like nano -$
because $
has special meaning to the shell.
Syntax:
$ nano -$ filename
# OR
$ nano --softwrap filename
Use Case: Improving readability of long lines without modifying the file’s actual line structure. Often combined with -a
(--atblanks
) for wrapping at whitespace.
Example:
# Create a file with a very long line
$ echo "This is a very very very very very very very very very very very very very very very very very very very very long line of text." > longline.txt
# Open with softwrap
$ nano -$ longline.txt
Output:
Text wraps on screen without altering file content.
Explanation: Enhances visual clarity.
5. Combining Options
Many options can be combined for a tailored editing experience.
5.1 View File with Line Numbers and No Help Bar
# Combine -l (line numbers) and -x (no help)
$ nano -lx view_script.sh
Output:
Shows line numbers, hides help, in read-only mode.
Explanation: Combines visibility and minimalism.
5.2 Autoindent, Tabs to Spaces, and Specific Tab Size
# Combine -i (autoindent), -E (tabs to spaces), and -T4 (tabsize 4)
$ nano -iET4 python_code.py
Output:
Sets up nano
for Python coding: auto-indent, tabs as 4 spaces.
Explanation: Optimizes for coding standards.
5.3 Backup, Constant Cursor Position, and Soft Wrap at Blanks
# Combine -B (backup), -c (constant cursor show), -$ (softwrap), -a (at blanks)
$ nano -Bc$a important_notes.txt
Output:
Backups enabled, cursor position shown, soft wrapping at spaces.
Explanation: Comprehensive editing setup.
6. Handling Special Cases
6.1 Filenames Starting with Hyphen (-) or Plus (+)
If a filename starts with -
or +
, nano
might interpret it as an option or line number specifier.
Solution: Use --
to signify the end of options, or prefix with ./
.
# Edit a file named -my-config.ini
$ nano -- -my-config.ini
# OR
$ nano ./-my-config.ini
# Edit a file named +data.log
$ nano -- +data.log
# OR
$ nano ./+data.log
Explanation: Prevents misinterpretation of filenames.
6.2 Filenames with Spaces
Quote the filename or escape the spaces.
# Edit a file with spaces
$ nano "My Document With Spaces.txt"
# OR
$ nano My Document With Spaces.txt
Explanation: Ensures proper file recognition.
6.3 Permissions
- Reading a file: You need read (
r
) permission on the file. - Saving a file (Write Out):
- If creating a new file: You need write (
w
) permission in the directory where you are saving. - If overwriting an existing file: You need write (
w
) permission on that file.
- If creating a new file: You need write (
- If you open a file you don’t have write permission for,
nano
will indicate ”[ Read 0 lines (Converted) ]” or similar and prevent saving to that name (you can “Save As” to a location where you do have permission).
Example:
# Try editing a read-only file
$ nano /etc/readonly.conf
Output:
nano opens but warns on save attempt if permissions lack.
Explanation: Highlights permission constraints.
6.4 Editing System Files (Requiring Privileges)
To edit system files (e.g., in /etc/
), you typically need root privileges. Use sudo
:
# Edit a system file
$ sudo nano /etc/hosts
Explanation: Grants necessary access.
7. Frequently Asked Questions (FAQ)
nano
?
7.1 What is the main purpose of To provide a simple, modeless, user-friendly text editor for the command line, suitable for quick edits and beginners.
nano
?
7.2 How do I save a file in Press Ctrl+O
(Write Out). Confirm the filename and press Enter
.
nano
?
7.3 How do I exit Press Ctrl+X
(Exit). If there are unsaved changes, nano
will ask if you want to save them (Y/N/C
- Yes/No/Cancel).
nano
?
7.4 How do I cut, copy, and paste in - Cut line(s):
Ctrl+K
(cuts current line or selected region to cutbuffer). - Copy region: Mark text with
M-A
(or^6
), thenM-6
(copies marked region to cutbuffer). - Paste:
Ctrl+U
(pastes from cutbuffer).
Example:
# Open a file
$ nano edit_test.txt
Action:
- Type “Line 1”, press
Ctrl+K
. - Move cursor, press
Ctrl+U
.
Output:
Line 1
Explanation: Basic text manipulation.
7.5 How do I search for text?
Press Ctrl+W
(Where Is). Type your search term and press Enter
. M-W
finds the next occurrence.
Example:
# Search in a file
$ nano search_test.txt
Action:
- Type “find me”, search with
Ctrl+W
, enter “me”.
Explanation: Quick text location.
7.6 How do I enable syntax highlighting?
Syntax highlighting is configured via nanorc
files (system-wide in /etc/nanorc
or user-specific in ~/.nanorc
or ~/.config/nano/nanorc
). nano
comes with default syntax definitions for many common languages. You can also use the -Y syntax_name
command-line option.
Example:
# Force Python syntax
$ nano -Y python script.py
Explanation: Enhances code readability.
7.7 Why are my arrow keys or Backspace/Delete not working correctly?
This can happen due to terminal emulation issues. Try the -d
(--rebinddelete
) option. If arrow keys produce ^[[A
etc., your terminal might not be correctly identified; check your TERM
environment variable. The -K
(--rawsequences
) option is a last resort.
Example:
# Fix key issues
$ nano -d key_fix.txt
Explanation: Resolves input problems.
nano
different from vi
/vim
or emacs
?
7.8 How is - Simplicity:
nano
is much simpler, with on-screen command help and no distinct modes (likevi
’s command/insert modes). - Learning Curve:
nano
has a gentle learning curve.vi
/vim
andemacs
are more powerful but complex. - Features:
vi
/vim
andemacs
offer advanced features and customization;nano
focuses on core editing.
nano
’s configuration files?
7.9 Where are - System-wide: Usually
/etc/nanorc
. - User-specific:
~/.nanorc
or$XDG_CONFIG_HOME/nano/nanorc
(often~/.config/nano/nanorc
). User settings override system settings.
nano
edit multiple files?
7.10 Can Yes. List multiple files on the command line (e.g., nano file1 file2
). If -F
(--multibuffer
) is active (default in modern versions), each opens in a separate buffer. Use M-<
(Alt+Left
) and M->
(Alt+Right
) to switch.
Example:
$ nano -F doc1.txt doc2.txt
M-U
Undo)?
7.11 What does “M-” mean in the shortcuts (e.g., M-
refers to the “Meta” key, typically Alt
. If Alt
doesn’t work (e.g., intercepted by your window manager), press Esc
, release, then the key (e.g., Esc
then U
for Undo).
nano
?
7.12 How do I get more help within Press Ctrl+G
(Get Help) to see a list of all commands and key bindings.
7.13 How do I undo or redo changes?
- Undo:
M-U
(Alt+U). - Redo:
M-E
(Alt+E).
Example:
$ nano undo_test.txt
Action:
- Type “mistake”, press
Ctrl+K
, thenM-U
to restore.
7.14 Can I customize key bindings?
Yes, via nanorc
files. Edit ~/.nanorc
to rebind commands.
nano
say “File is unwritable”?
7.15 Why does You lack write permissions. Use sudo
for system files or check file/directory permissions with ls -l
.
8. Conclusion
nano
stands out as an accessible and efficient command-line text editor, especially for users who prefer a straightforward, modeless editing experience. While it may not offer the extensive feature set of more complex editors like Vim or Emacs, its simplicity, on-screen help, and essential editing capabilities make it an invaluable tool for quick edits, script writing, and general text manipulation directly within the terminal. Its command-line options and nanorc
configuration allow for a good degree of customization to suit various preferences and tasks, making it versatile despite its lightweight nature.
9. nano Command: Reference Table of Key Options
Option(s) | Description | Example Command | Use Case |
---|---|---|---|
+LINE[,COL] | Start at specified LINE (and optionally COLumn) | $ nano +10,5 script.sh | Jump directly to a specific location in a file on opening |
+/STRING | Start at first occurrence of STRING (forward search) | $ nano +/"config_error" log.txt | Quickly find and edit a known string in a file |
-A , --smarthome | Make Home key “smarter” for navigating indented lines | $ nano -A code.py | More intuitive Home key behavior for indented text |
-B , --backup | Create backup file (filename~ ) when saving | $ nano -B important.conf | Automatically back up previous version on save |
-C DIR , --backupdir=DIR | Store numbered backups in DIR (used with -B ) | $ nano -BC backups/ file.txt | Maintain multiple, versioned backups in a separate directory |
-E , --tabstospaces | Convert typed tabs to spaces | $ nano -E code.c | Enforce space-based indentation while typing |
-F , --multibuffer | Read each file into a new buffer (default in modern nano) | $ nano -F file1 file2 | Edit multiple files simultaneously, switching between them |
-H , --historylog | Save search/replace/goto line history | $ nano -H notes.txt | Reuse previous search terms and commands across sessions |
-I , --ignorercfiles | Do not consult nanorc configuration files | $ nano -I config.sys | Start with default nano settings, ignoring customizations |
-l , --linenumbers | Display line numbers to the left of the text | $ nano -l script.pl | Easily reference line numbers while editing code or documents |
-m , --mouse | Enable mouse support (if available) | $ nano -m file.md | Use mouse for cursor placement, selection, and shortcuts |
-P , --positionlog | Remember and restore last cursor position for recent files | $ nano -P long_doc.txt | Resume editing where you left off in frequently used files |
-r NUM , --fill=NUM | Set wrapping/justification width to NUM columns | $ nano -r 72 email.txt | Format text to a specific line width |
-s PROG , --speller=PROG | Use external PROG for spell checking (Ctrl+T ) | $ nano -s "aspell -c" report | Integrate a preferred external spell checker |
-T NUM , --tabsize=NUM | Set tab width to NUM spaces (default 8) | $ nano -T 4 source.java | Adjust display width of tab characters |
-v , --view | View file in read-only mode (disallow editing) | $ nano -v /etc/passwd | Safely inspect sensitive files without risk of modification |
-w , --nowrap | Do not automatically hard-wrap long lines (default in modern nano) | $ nano -w config_file | Prevent automatic line breaking, allow horizontal scrolling |
-Y SYNTAX , --syntax=SYNTAX | Specify syntax highlighting SYNTAX to use | $ nano -Y sh my_script | Force specific syntax highlighting for a file |
-$ , --softwrap | Enable soft wrapping of long lines (display over multiple screen lines) | $ nano -$ prose.txt | Read long lines without horizontal scrolling, no file modification |
-c , --constantshow | Constantly show cursor position (line, column) on status bar | $ nano -c data.csv | Always have cursor location visible |
-i , --autoindent | Automatically indent new lines to match previous line | $ nano -i code.rb | Maintain consistent indentation when writing code/structured text |
--help | Display help message and exit | $ nano --help | Get quick usage information |
--version | Output version information and exit | $ nano --version | Check the version of the nano editor |