
nano Command in Linux
Nano is a powerful, yet simple command-line text editor that comes pre-installed on Unix-based systems. Its intuitive interface and on-screen help make it an ideal choice for users who need to edit text files directly from the terminal.
Nano editor's ability to read initialization files in a structured order (from SYSCONFDIR/nanorc to ~/.nanorc) ensures a customizable and efficient editing experience.
If no spell checker is specified, nano checks the SPELL environment variable for a command. Besides that, If nano encounters an error (like receiving a SIGHUP or SIGTERM or running out of memory), it tries to save the buffer to an emergency file. This file will be named nano.save or append.save to the current filename, ensuring that data is not lost.
Table of Contents
Here is a comprehensive guide to the options available with the nano command −
Syntax of nano Command
The general syntax for the nano command is as follows −
nano [OPTIONS] [[+LINE[,COLUMN]] FILE]...
nano Command Options
The following are detailed descriptions of the options that make Nano a versatile text editor suitable for various tasks, from simple note-taking to editing configuration files and source code.
Options | Description |
---|---|
+LINE[,COLUMN] | Places the cursor at the specified line number (LINE) and column number (COLUMN) on startup, instead of the default of line 1, column 1. |
-? | Same as -h (--help), displays a summary of command-line options and exits. |
-A (--smarthome) | Makes the Home key smarter. When pressed, the cursor will jump to the beginning of non-whitespace characters on a line. If already at that position, it will jump to the true beginning of the line. |
-B (--backup) | Backs up the previous version of the file being edited with a ~ suffix when saving. |
-C dir (--backupdir=dir) | Sets the directory where nano puts unique backup files if file backups are enabled. |
-D (--boldtext) | Uses bold text instead of reverse video text. |
-E (--tabstospaces) | Converts typed tabs to spaces. |
-F (--multibuffer) | Enables multiple file buffers, if available. |
-H (--historylog) | Logs search and replace strings to ~/.nano_history, which can be retrieved in later sessions, if nanorc support is available. |
-I (--ignorercfiles) | Ignores SYSCONFDIR/nanorc or ~/.nanorc files, if nanorc support is available. |
-K (--rebindkeypad) | Interprets the numeric keypad keys so they all work properly. Use this option if they don't function correctly; note that mouse support won't work properly with this enabled. |
-L (--nonewlines) | Prevents nano from adding newlines at the end of files. |
-N (--noconvert) | Disables automatic conversion of files from DOS/Mac format. |
-O (--morespace) | Uses the blank line below the title bar as extra editing space. |
-Q str (--quotestr=str) | Sets the quoting string for justifying text. Default is ^([ \t]*[#:>\|}])+ if extended regular expression support is available, or > otherwise. |
-R (--restricted) | Runs nano in restricted mode, which disallows reading or writing to any file not specified on the command line, reading nanorc files, suspending, appending or prepending to files, using backup files, or spell-checking. |
-S (--smooth) | Enables smooth scrolling, where text scrolls line-by-line rather than chunk-by-chunk. |
-T cols (--tabsize=cols) | Sets the tab size to cols columns. Must be greater than 0. Default is 8. |
-U (--quickblank) | Enables quick status bar blanking, where status messages disappear after 1 keystroke instead of 25. This is overridden by -c. |
-V (--version) | Displays the current version number and exits. |
-W (--wordbounds) | Detects word boundaries more accurately by treating punctuation characters as part of a word. |
-Y str (--syntax=str) | Specifies a particular syntax highlighting to use from the nanorc file. |
-c (--const) | Constantly shows the cursor position, overriding -U. |
-d (--rebinddelete) | Interprets the Delete key differently so that both Backspace and Delete work properly. Use if Backspace acts like Delete on your system. |
-h (--help) | Shows a summary of command line options and exits. |
i (--autoindent) | Indents new lines to match the previous line's indentation, useful for editing source code. |
-k (--cut) | Cuts text from the cursor position to the end of the line. |
-l (--nofollow) | Replaces symbolic links with new files instead of following them. Useful for editing files in directories like /tmp. |
-m (--mouse) | Enables mouse support if available. Allows using the mouse to place the cursor, set marks, and execute shortcuts. |
-o dir (--operatingdir=dir) | Sets the operating directory, akin to setting up a chroot environment. |
-p (--preserve) | Preserves the XON and XOFF sequences (^Q and ^S) to be caught by the terminal. |
-r cols (--fill=cols) | Wraps lines at cols columns. If cols is 0 or less, wrapping occurs at the screen width minus cols columns, varying with screen resizing. Default is -8. |
-s prog (--speller=prog) | Enables an alternative spell checker command. |
-t (--tempfile) | Saves changes without prompting. Same as Pico's -t option. |
-v (--view) | Opens the file in read-only mode. |
-w (--nowrap) | Disables the wrapping of long lines. |
-x (--nohelp) | Disables the help screen at the bottom of the editor. |
-z (--suspend) | Enables the ability to suspend nano. |
-a, -b, -e, -f, -g, -j | These options are ignored for compatibility with Pico. |
Examples of nano Command in Linux
In this section, we'll showcase examples of how nano can be customized and utilized for various editing tasks.
Viewing a File with Line Numbers
Viewing a file with line numbers in nano is straightforward and useful for keeping track of your position in larger files. To achieve this, you can simply run the nano command with the "-l" option −
sudo nano -l config.txt
Upon executing this command, nano opens config.txt and lets you see line numbers along the left margin, making it easier to reference specific lines during editing.

Editing with Backup and Auto-Indent
You can create a backup of the file before editing and enable automatic indentation for new lines using the "-B" and "-i" options in nano −
sudo nano -B -i report.txt
Upon running this command, a backup file named report.txt~ is created in the same directory. As you type and press Enter, new lines will automatically indent to match the indentation of the previous line.
This ensures you don't lose data if something goes wrong and helps you maintain consistent formatting while editing.

Opening a File at a Specific Line and Column
To open a file at a specific line and column using nano, you can simply specify the desired line and column in the command −
sudo nano +6,5 notes.txt
This command ensures that when nano opens notes.txt, it positions the cursor directly at line 6, column 5, making it convenient to start editing precisely where needed.

Use a Specific Syntax Highlighting
To open a file with syntax highlighting for a specific language, you can simply use the nano command with the "-Y" flag −
sudo nano -Y python myscript.py
This command ensures that when nano opens myscript.py, it uses the specified syntax highlighting for Python, enhancing readability and aiding in coding.

Convert Tabs to Spaces
You can open a file in nano and automatically convert typed tabs into spaces using the "-E" option −
sudo nano -E myfile.txt
This is especially useful for maintaining consistent formatting, especially when working with code.

Conclusion
Nano's intuitive interface, combined with its robust features like search and replace, spell checking, and auto-indentation, makes it a preferred choice for users, from beginners to experienced administrators.
Whether you're editing configuration files, scripts, or simple notes, nano provides a reliable and straightforward tool for handling text editing directly within the terminal.