How to Use Nano, the Linux Command Line Text Editor

By 

Updated on

7 min read

Using Nano Text Editor

When working on the command line, quite often you will need to create or edit text files. Two of the most powerful and popular command-line editors are Vim and Emacs. Both have a steep learning curve that can be intimidating to new users. For those who need a simple editor, there is nano.

GNU nano is an easy-to-use command line text editor for Unix and Linux operating systems. It includes all the basic functionality you expect from a regular text editor, like syntax highlighting, multiple buffers, search and replace with regular expression support, spellchecking, UTF-8 encoding, and more.

This guide explains the basic usage of the nano editor, including how to create and open a file, edit a file, save a file, search and replace text, cut and paste text, and more.

Installing Nano

Nano text editor is pre-installed on macOS and most Linux distributions. To check if it is installed on your system, type:

Terminal
nano --version

The output will look something like this:

output
GNU nano, version 8.4
(C) 1999-2011, 2013-2024 Free Software Foundation, Inc.
(C) 2014-2024 the contributors to nano
Email: nano@nano-editor.org	Web: https://nano-editor.org/

If you do not have nano installed on your system, you can install it using your distribution’s package manager.

Install Nano on Ubuntu, Debian, and Derivatives

Terminal
sudo apt install nano

Install Nano on Fedora, RHEL, and Derivatives

Terminal
sudo dnf install nano

Opening and Creating Files

The general syntax for starting nano is:

Terminal
nano [OPTIONS] [FILE]

To open an existing file or to create a new file, type nano followed by the file name:

Terminal
nano filename
Nano Text Editor

This opens a new editor window, and you can start editing the file.

At the bottom of the window, there is a list of the most basic command shortcuts to use with the nano editor.

All commands are prefixed with either ^ or M characters. The caret symbol (^) represents the Ctrl key. For example, the ^J command means to press the Ctrl and J keys at the same time. The letter M represents the Alt key.

You can get a list of all commands by typing Ctrl+g.

To open a file, you must have read permissions to the file.

Editing System Files with sudo nano

To edit files that require root permissions, such as system configuration files, prefix the command with sudo:

Terminal
sudo nano /etc/hosts

You will be prompted for your password. Nano will open the file with elevated privileges, allowing you to make changes.

If you want to open a file with the cursor on a specific line and character, use the following syntax:

Terminal
nano +line_number,character_number filename

For example, nano +20 filename opens the file at line 20, and nano +20,5 filename opens at line 20, column 5. If you omit the column number, the cursor will be positioned on the first character.

Editing Files

Unlike vi, nano is a modeless editor, which means that you can start typing and editing the text immediately after opening the file.

To move the cursor to a specific line and character number, use the Ctrl+_ command. The menu at the bottom of the screen will change. Enter the number(s) in the “Enter line number, column number:” field and hit Enter.

Searching and Replacing

To search for a text, press Ctrl+w, type in the search term, and press Enter. The cursor will move to the first match. To move to the next match, press Alt+w.

If you want to search and replace, press Ctrl+\. Enter the search term and the text to be replaced with. The editor will move to the first match and ask you whether to replace it. After hitting Y or N, it will move to the next match. Pressing A will replace all matches.

Copying, Cutting, and Pasting

To select text, move the cursor to the beginning of the text and press Alt+a. This will set a selection mark. Move the cursor to the end of the text you want to select using the arrow keys. The selected text will be highlighted. If you wish to cancel the selection, press Ctrl+6.

Copy the selected text to the clipboard using the Alt+6 command. Ctrl+k will cut the selected text.

If you want to cut whole lines, move the cursor to the line and press Ctrl+k. You can cut multiple lines by hitting Ctrl+k several times.

To paste the text, move the cursor to where you want to put the text and press Ctrl+u.

Saving and Exiting

To save the changes you have made to the file, press Ctrl+o. If the file does not exist, it will be created once you save it.

To exit nano , press Ctrl+x. If there are unsaved changes, you will be asked whether you want to save the changes.

To save the file, you must have write permissions to the file. If you are creating a new file , you need to have write permission to the directory where the file is created.

Customizing Nano (nanorc)

When nano is launched, it reads its configuration parameters from the system-wide configuration file /etc/nanorc and from the user-specific files ~/.config/nano/nanorc and ~/.nanorc if the files are present.

Options specified in the user files take precedence over the global options.

Visit the nanorc page for a complete list of all available options.

Syntax Highlighting

Nano ships with syntax highlighting rules for most popular file types. On most Linux systems, the syntax files are stored in the /usr/share/nano directory and included by default in the /etc/nanorc configuration file.

/etc/nanorcini
include "/usr/share/nano/*.nanorc"

The easiest option to enable highlighting for a new file type is to copy the file containing the syntax highlighting rules to the /usr/share/nano directory.

Set Nano as the Default Text Editor

By default, on most Linux systems, the default text editor for commands such as visudo and crontab is set to vi. To use nano as the default text editor, you need to change the VISUAL and EDITOR environment variables .

Bash users can export the variables in the ~/.bashrc file:

~/.bashrcini
export VISUAL=nano
export EDITOR="$VISUAL"

Quick Reference

TaskShortcut / Command
Open filenano filename
Save fileCtrl+o
Exit nanoCtrl+x
Search textCtrl+w
Search next matchAlt+w
Replace textCtrl+\\
Select textAlt+a
Copy selected textAlt+6
Cut current lineCtrl+k
Paste textCtrl+u
Show helpCtrl+g
Go to line/columnCtrl+_

For a printable quick reference, see the Nano cheatsheet .

Troubleshooting

nano: command not found
Install nano with your package manager, then run nano --version to confirm it is available.

Cannot save file due to permission denied
Open the file with the correct permissions or use sudo when editing system files.

Keyboard shortcuts do not work in terminal
Check for terminal keybinding conflicts. For example, some terminal profiles remap Alt combinations.

FAQ

Is nano easier than Vim?
For most new users, yes. Nano is modeless, so you can start typing immediately without switching modes.

How do we open nano at a specific line?
Use nano +LINE filename, for example nano +42 notes.txt.

What does sudo nano do?
It opens a file in nano with superuser (root) privileges, letting you edit system configuration files that your regular user cannot modify.

Can nano edit system configuration files?
Yes, as long as you have the required permissions for the target file.

Conclusion

In this tutorial, we have shown you how to use the GNU nano text editor. It is a popular text editor among Linux users with a small learning curve.

For more information about GNU nano, visit the official nano documentation page.

If you have any questions, feel free to leave a comment below.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page