How to Install and Use Nano Text Editor {+ Nano Commands}

March 25, 2024

Introduction

Nano is a simple command-line text editor commonly found in Unix-based operating systems. It allows users to quickly edit text files directly from the command line.

The editor's intuitive interface and keyboard shortcuts make it convenient for editing configuration files, scripts, and other text-based documents.

In this tutorial, learn how to install and use the Nano text editor by following simple examples.

How to Install and Use Nano Text Editor

Prerequisites

How to Install Nano Text Editor

Nano is included in most Linux distributions by default. However, if it's not present on your system, install Nano using the following steps:

1. Update the apt repositories with the apt update command:

sudo apt update
sudo apt update terminal output

2. Install Nano with:

sudo apt install nano
sudo apt install nano terminal output

Note: If you're running CentOS or Fedora, execute yum install nano to complete this step.

How to Use Nano Text Editor

Nano is a versatile text editor primarily used to edit files directly from the command line of Unix-based systems. Its straightforward interface and keyboard shortcuts make it a convenient tool for quick text editing tasks in the terminal environment.

How to Open and Create Files

Nano allows users to create and edit text files in different ways. The following sections elaborate on how to create a new file and open an existing one.

Create a New File

There are two ways to create new text files with Nano. One way is:

nano [file-name]

Replace [file-name] with the actual name of the file. For example, create a Nano file called text1 with:

nano text1
nano text1 terminal output

Another way is to open a new blank Nano file. To do so, enter:

nano
nano terminal output

The program opens a blank page without any specified name. If changes were made, when you decide to exit the file (Ctrl+X), Nano asks whether to save or discard the file.

sSve modified buffer in Nano

If you press y to save the file, Nano prompts you to name it. Type in a name and press Enter.

In this example, the name is file1.

File name in Nano

Open an Existing File

To open an existing file, append the file name to the command. For example, if the file is called file1, the command is:

nano file1

However, to open a file from another directory, include the path in which the file is located. For example:

nano /home/documents/file1

It is also possible to open a file and directly go to a specific line or column.

nano +line,column [file-name]

For instance, go to column 2 with:

nano +2 file1

Nano Command Keyboard Shortcuts

What makes Nano so attractive is an easy graphical user interface (GUI) that allows users to interact directly with the text. There is no need to switch to an edit mode, like in Vim.

Users directly write, edit, and navigate through content using different keyboard shortcuts.

Navigating Text in Nano

In Nano, users move through text using the arrow keys. Additionally, keyboard shortcuts let you navigate through lines or pages.

Control Characters and Keys

There are two keyboard combination types for each function in Nano.

  • Control shortcuts. Used with the Ctrl button. Represented by a carat (^) followed by a symbol in nano documentation.

For example, the shortcut to exit out of the Nano text editor is Ctrl+X (displayed as ^X).

  • Meta shortcuts. Require the Meta key (usually the Alt button). Represented by the letter M followed by a symbol.

For example, the shortcut to Undo an action is Alt+U (displayed as M-U).

The two bottom lines in the text editor display the most commonly used shortcuts, as seen in the image below.

Commonly used shortcuts

To see all valid shortcuts, press Ctrl+G (displayed as ^G) or F1. This opens Nano's help text and lists all possible keystrokes.

Nano shortcuts

Note: Do not use the Shift button in Nano. All shortcuts use unmodified numbers and lowercase letters.

Shortcuts for Navigation

When dealing with a large file, it is helpful to know how to navigate through the text quickly. Nano allows you to do this using the arrow keys or keyboard shortcuts.

Useful keyboard shortcuts for navigating include:

  • Ctrl+F (^F). Move forward one character.
  • Ctrl+B (^B). Move back one character.
  • Ctrl+Space (^Space). Go one word forward.
  • Alt+Space (M-Space). Go one word backward.
  • Ctrl+P (^P). Navigate to the previous line.
  • Ctrl+N (^N). Navigate to the next line.
  • Ctrl+V (^V). Go to the next page.
  • Ctrl+Y (^Y). Move to the previous page.
  • Ctrl+A (^A). Go to the beginning of the line.
  • Ctrl+E (^E). Move to the end of the line.

Search a Text File

To search for a particular word or part of a text inside the editor, use the where is option with the Ctrl+W shortcut (^W).

This opens a search prompt to type in the text you want to find. In the following example, the search word is and.

Search for a word in Nano

Hit Enter. Nano highlights the matching result.

Highlighted search result in Nano

Continue to the next result with Alt+W (M-W).

Replace Text

To replace text in the file, follow these steps:

1. Open the search bar with Ctrl+W (^W) and type the search item. For example and.

Search for an item in Nano

2. Press Ctrl+R (^R) to open a new bar and type in the replacement term. For example, or, like in the image below:

Replace word in Nano

3. Hit Enter and choose whether to replace the word or not, replace all instances of the word, or cancel the operation.

Replacing word menu in Nano

Select, Copy, Cut, and Paste Text

To select a part of a file, navigate to the beginning of the part, press the Alt+A shortcut (M-A), and use the arrow keys to move over the text you wish to select.

Select text in Nano

Next, copy the selected text with the Alt+6 combination (M-6) or cut with Ctrl+K (^K). If you use these shortcuts without selecting any text first, they copy or cut the entire line of text.

To paste the text, use Ctrl+U (^U).

Insert Another File Into the Current One

While editing a file in Nano, it is possible to insert the entire contents of another file into the current one with the Ctrl+R (^R) shortcut.

The command opens the bottom bar to write the path to the file you want to import.

Insert contents of another file in Nano

Spell Check in Nano

Nano also offers a spell-checking feature. However, to enable it, first install the spell package.
To install the spell package, run the following command:

sudo apt install spell
sudo apt install spell terminal output

After installing the package, spell check in the text editor by pressing Ctrl+T (^T). It selects the misspelled word and asks for a correct replacement.

In the image below, the word command is misspelled.

Spell check in Nano

Save a File

To save a file, use the Ctrl+O (^O) keyboard combination. Nano asks for a file name or to confirm an existing file name.

Saving file in Nano

Once you hit Enter, the status bar indicates the number of lines that the file has.

Number of lines in nano

Nano is also able to create a file backup. To do this, use the Alt+B command (M-B). Pressing the keys once enables backup, and doing it again disables it.

Exit Nano

To exit out of Nano, press Ctrl+X (^X). If changes were made, Nano asks whether to save them or not. Type y or n to save or discard changes or Ctrl+C to cancel exiting the file.

Save before exiting the file in Nano

Advanced Nano Commands

Advanced commands in Nano offer specialized functionality beyond basic features. These arguments modify the command's behavior and customize how Nano operates.

Relevant Nano arguments are listed in the table below:

OptionDescription
-cConstantly displays cursor position.
-iEnables automatic indentation based on the previous line's indentation.
-kEnables cutting from the cursor position.
-mEnables mouse support.
-BCreates a file backup before saving.
-XStops creating backup files when saving.
-wPrevents word wrapping.
-UTurns on Undo functionality
-TSets tab size to the given number of spaces.
-oEnables viewing large files.
-REnables restricted mode in which certain commands are not available.
-vPrints Nano version info.

The following text elaborates on common advanced Nano options.

Enable Undo Function

Using the -U option enables the undo functionality in Nano. This allows users to revert to previous changes made during the editing session, which helps correct mistakes or experiment with different edits. For instance, to enable Undo in file1, run:

nano -U file1

Once you enable the undo functionality in Nano, use the -U option to undo changes by pressing the Ctrl+Shift+\ keyboard shortcut. This command undos the most recent change made in the editing session.

Enable Constant Cursor Position Display

Use the -c option with Nano to show the cursor position (line and column numbers) at the bottom of the editor window. This helps keep track of the current position within the file. For example, to constantly show the cursor position in file1, run:

nano -c file1
Constant cursor position  display in Nano

Enable Mouse Support

Use the -m argument to enable mouse support. The option allows users to use the mouse to navigate and select text in addition to using the keyboard shortcuts. To enable mouse support for file1, execute:

nano -m file1

Enable Automatic Indentation

The -i option automatically indents new lines based on the indentation style of the previous line. This is particularly useful for maintaining consistent indentation in code or structured text files. To enable this functionality in file1, run:

nano -i file1
Enable automatic indentation in Nano

Conclusion

After reading this article, you know how to install the Nano text editor on Linux. You also learned how to navigate a Nano file as well as the basic commands and shortcuts for creating, editing, and saving files.

Next, learn other important Linux commands.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Save a File in Vi / Vim & Exit
September 22, 2022

Vim (Vi IMproved) is a well-known, open-source text editor for Linux or Unix systems. It is a powerful and ...
Read more
How to Use mkdir Command to Make or Create a Linux Directory
December 1, 2023

The mkdir command in Linux allows users to create or make new directories. mkdir stands for "make directory".
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through all the ...
Read more
Linux SCP Command: Securely Copy & Transfer Files
March 25, 2024

Tutorial on securely transferring files between Unix or Linux systems using the SCP command. The SCP or ...
Read more