Introduction

Nano is a simple, modeless, WYSIWYG command-line text editor included in most Linux installations. With a simple easy to use interface, it is a great choice for Linux beginners.

 In this tutorial, learn how to install and use the Nano text editor.

How to Install and Use Nano Text Editor

Installing the Nano Text Editor

Nano is by default included in most Linux distributions. However, if necessary, the installation process can be completed in two steps.

1. First, open the terminal and update the cache repository with the command:

sudo apt update

2. Then, install Nano by running the command:

Debian/Ubuntu: sudo apt install nano

CentOS/Fedora: yum install nano

With this, you have successfully installed the text editor.

Control Characters and Keys

There are keyboard combinations for each function in Nano. Control shortcuts (used with the CTRL button) are represented by a carat (^) followed by a symbol.

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

In addition, there are combinations that require the Meta key (usually the ALT button). They are represented by the letter M followed by a symbol.

For example, the shortcut to undo an action in a text is ALT+U (displayed as M-U).

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

viewing the shortcuts available at the bottom of the screen

To see all valid shortcuts, press CTRL+G (displayed as ^G) or F1. This will open Nano’s help text and list all possible keystrokes.

 

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

 

screenshot of the main nano help page with all text commands

How to open, create, and edit Nano files

There are several ways to open the text editor.
As it is a command-line editor, the first step is to open the terminal. The easiest way to access the terminal is the CTRL+ALT+T shortcut.

Create a new file

To open a new blank Nano file, run the command: nano

When you decide to exit (CTRL+X), it will ask whether to save or discard the file.

do you want to save of discard file

If you decide to save and press y it will ask to name the file. Type in a name and press ENTER.

In this example, the name is file1.php.

example of saving a file

Open an existing file

To open an existing file, add the file name to the command. For example, if the file is called file1.php, the command will be:

nano file1.php

However, to open a file in another directory, you must include the path in which the file is located:

nano /path/to/file1.php

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

nano +line,column file1.php

Edit files in Nano

What makes Nano so attractive is that it has an easy graphical user interface (GUI), allowing users to directly interact with the text. There is no need to switch to an edit mode, like in Vim. You can directly write, edit and navigate through a text and receive immediate on-screen feedback.

Nano Keyboard Shortcuts

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

Useful keyboard shortcuts for navigating include:

  • move forward one character: CTRL+F (^F)
  • move back one character: CTRL+B (^B)
  • move forward one word: CTRL+Space (^Space)
  • move back one word: ALT+Space (M-Space)
  • move to the previous line: CTRL+P (^P)
  • move to the next line: CTRL+N (^N)
  • move to the next page: CTRL+V (^V)
  • move to the previous page: CTRL+Y (^Y)
  • move to the beginning of the line: CTRL+A (^A)
  • move to the end of the line: CTRL+E (^E)

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 will open a search prompt where you can type in the text you want to find. To continue to the next result, use ALT+W (M-W).

The search bar can also find specific line numbers. Press CTRL+T (^T) while in it and the line number you want to find.

Regex searches

You can also search with regex (regular expressions). These represent a search pattern defined by a sequence of characters. To do so, use the ALT+R shortcut (M-R).

Replace text

To replace text in the file, first open the search bar with CTRL+W (^W) and then press CTRL+R (^R). It will open a search bar to type in what you want to replace, as seen in the image below.

example of replacing text inside nano

After you have selected the search item, it will ask what you want to replace it with.

selecting text to be replaced

Select, copy, cut and paste text

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

Next, you can 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 prior, it will copy or cut the entire line of text.

To paste text, use CTRL+U (displayed as ^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.

This command will open the bottom bar in which you must write the path and file name you wish to import.

inserting contents of another file command

Spell check in Nano

Nano also offers a spell checking feature. However, to enable it you must install the spell package.
To install the spell package, run the following command in the terminal:

sudo apt install spell

After installing the package, you can spell check in the text editor by pressing CTRL+T (^T). It will select the misspelled word and ask for a correct replacement.

In the image below, the words underlined are misspelled.

spell checking in nano, replacement errors

Save a file

To save a file, use the CTRL+O (^O) keyboard combination. It will ask you to enter a file name or confirm the name of an existing file.

Once it is saved, the status bar will indicate the number of lines that the file has.

display of the number of text lines saved

It can also create a backup of a file if you need to store temporary copies of the same file. To do this, use the ALT+B command (M-B). Pressing the keys once will enable backup and doing it again will disable it.

Exit

To exit out of Nano, press CTRL+X (Nano displays it as ^X).

Conclusion

Now you know how to install Nano text editor on Linux!

You learned the basic text commands and commands used for creating, editing and saving files.

Users can always refer to the Help text with CTRL+G (^G) for additional commands.