Nano Text Editor Basics

When working with the Raspberry Pi, you’ll often see the instructions, “open a file in the nano text editor.” This HowTo briefly familiarizes you with nano basics.

(Material in this HowTo was originally published in “Workspace: Command-Line Tools” by Dmitri Popov, Linux Pro Magazine, April 2009, pg. 88, and “Command Line: Text Editors” by Heike Jurzik, Linux Pro Magazine, June 2007, pg. 87)

A decent text editor is probably the most important tool in any user’s toolbox. Although there are quite a few text editors you can choose from, nano [2] strikes a perfect balance between power and ease of use. Nano is bundled with many popular Linux distros, and you can launch it by running the nano command. All actions in nano are initiated with keyboard shortcuts, and you can see the most important ones at the bottom of the screen (Figure 1).

Figure 1: The nano editing program. Keyboard shortcuts are shown at the bottom of the screen.

The circumflex (^) represents the Ctrl key – the shortcut Ctrl+G displays Help, for example. Some functions are accessible via the meta key (shown as M- in the onscreen Help). Which key is defined as Meta depends on your system; in most cases, it’s either the Esc, Alt, or Windows key (we use Alt in this HowTo). You can periodically save the file during editing using the Ctrl+O shortcut. To exit nano, press Ctrl+X. If you exit nano from a modified file, it will prompt you to save it first.

To create a new file, you just launch the editor by typing nano at the command line. As an alternative, you can pass the filename to the command to open the file when the program launches:

nano <filename>.txt

Nano shows the name of the file you are currently editing at the top of the screen

Cut and Paste

Like any text editor worth its salt, nano supports cut and paste actions. The Ctrl+K shortcut cuts a single line. To paste the line, place the cursor where you want to insert the line and press Ctrl+U. To move multiple lines, cut them using the Ctrl+K shortcut, then paste them all together by pressing Ctrl+U.

If you need to cut a single word or a text fragment, press Ctrl+6 or Alt+A, select the text you want, then press Ctrl+K to cut it. You can then paste the snippet using the Ctrl+U shortcut.

Search

Performing a text search in nano is also easy: Hit Ctrl+W, enter the search string, and press Enter. To find the same string again, press Ctrl+W again and hit Enter. To perform the search and replace

action, press Ctrl+\, specify a search string and its replacement, and press Enter. Then you can choose whether you want to replace only the first match found or all occurrences of the search string.

Spell Check

Although nano is not designed to replace a dedicated word processor, it has both word count and spell-checking features, which make it a perfect tool for drafting articles or taking notes (Figure 2). To view the current word count, press the Alt+D. To run a spell check, press Ctrl+T. By default, nano uses its own interactive spell checker that requires the spell program to be installed on your system. Alternatively, you can force nano to use the aspell program instead. To force this behavior globally (for all users), open the nanorc file for editing as root

nano /etc/nanorc

and uncomment the following line:

set speller "aspell -x -c"

While you’re there, you might want to tweak other settings, too. For example, if you want to enable the mouse in nano, uncomment the set mouse option. This allows you to use the mouse to place the cursor, set the mark, and execute shortcuts.

Configuration

You also can modify the configuration file to specify whether to use automatic indenting (set autoindent), whether to let the editor create automatic backups (set backup), and many other things. The entries in the configuration file are self-explanatory for the most part, with useful comments in the file. A couple of entries show how to set up syntax highlighting. The examples for C, HTML, and TeX are easy to follow, and you just need to remove the comment signs to reinstate them. On the basis of these examples, it should be easy enough to set up syntax highlighting for other programs.

To modify your personal version of nano, just copy the nanorc file to your home directory as a template,

cp /etc/nanorc ~/.nanorc

then make changes in that file.

Additional Help

Besides the very readable on-screen Help, nano comes with a man page. In the terminal, just type:

man nano

The man page supplies more advanced information, such as how to control nano at launch time by passing in command-line parameters.

Related content