web-coding Practical introduction to GNU Nano

Nano is an ideal text editor for a power user who wants a convenient, familiar, and minimal application

Practical introduction to GNU Nano

GNU Nano is a text editor that runs in a terminal window. It seems to me to be somewhat overshadowed by Vim and Emacs, but for me Nano provides the best features of both. Like Vim, GNU Nano is light on system resources, and installed by default on the vast majority of Linux distributions (it's easy to compile on most systems where it's otherwise missing). Like Emacs, GNU Nano uses common keybindings that any Bash user is already familiar with, and has many design choices similar to other GNU applications. Nano is an ideal text editor for a power user who wants a convenient, familiar, and minimal application. It may be all of these things, but it's also a powerful text editor.

white text on a black background, with purple highlight for line numbers on the left

Open a file (C-r M-f)

You can open a file in GNU Nano in a few different ways. It's common to launch Nano by referencing a file as part of the command:

$ nano ~/my-example-file.txt

Nano is capable of creating an empty file, so the file doesn't have to exist.

To open a file from within Nano:

  1. Press Ctrl+r and then Alt+f.
  2. Type the path of the file you want to open, or press Alt+t to browse your file system.

You have a two choices when navigating GNU Nano. You can use Bash-like keybindings, or you can use arrow keys and the other navigation keys appearing on conventional keyboards. Bash navigation is likely convenient to you if you're used to Bash navigation. Most of the usual key combinations apply:

  • Ctrl+p: Move up
  • Ctrl+f: Move right
  • Ctrl+n: Move down
  • Ctrl+b: Move left
  • Ctrl+a: Move to the start of a line
  • Ctrl+e: Move to the end of a line

Selecting text (M-a)

Alt-a starts a selection from your cursor's current position. After pressing Alt+a, you can move your cursor to a new location, and everything between your original location and the new location is considered selected.

To exit selection mode, press Alt+a again.

white text on a black background, with purple highlight for selected text

You can copy or cut a selection, which is a pretty common task in text editing.

Copy (M-^) and paste (C-u)

Copying text in GNU Nano follows a similar workflow as any GUI editor does. First, you select the word (or words), then copy, move, paste.

  1. At the start of the word (or region) you want to copy, press Alt+a to start your selection.
  2. Move your cursor to the end of the word (or region), and then press Ctrl+^ to copy the selection. This also ends your selection.
  3. Move your cursor to the location you want to paste, and then press Ctrl+u.

Cut (C-k) and paste (C-u)

To cut a selection, press Ctrl+k while you have text selected.

You can also just press Ctrl+k without a selection to copy and remove a whole line of text.

Press Ctrl+u to paste the line back into your document (or some other document open in Nano).

Undo (M-u) and redo (M-e)

Once you start editing text, mistakes are inevitable. The undo command is Alt+u. When you undo, you step backwards in the history of your operations.

To undo an undo, use the redo function by pressing Alt+e.

Buffers (C-r M-f)

As with Vim and Emacs, GNU Nano has the ability to open multiple documents at once.

File buffers (C-r M-f)

The Read file function of Nano is powerful and provides at least three different services.

First, as you know, it can open a file into a new buffer. When you open a file with Ctrl+r Alt+f, you're reading a file from a drive into a Nano buffer.

Secondly, it can read a file into your current buffer. For example, suppose you have a file called start.txt containing these lines:

Mary had a little lamb
its fleece was white as snow

And another file called end.txt containing these lines:

Everywhere that Mary went
the lamb was sure to go

You can append end.txt to start.txt by opening start.txt and reading end.txt into its buffer.

  1. Launch nano and press Ctrl+r Alt+f to open begin.txt.
  2. Place your cursor on a new line at the end of the text.
  3. Press Ctrl+r to insert the contents of end.txt at your cursor position.

A third way to use the Read file function is to open several files in separate buffers all at once. This a little like opening tabs in your web browser, or screens in GNU Screen or Tmux.

  1. Press Ctrl+r and then Alt+f to open begin.txt in a buffer.
  2. Press Ctrl+r and then Alt+f again to open end.txt in a new buffer. This buffer becomes your current buffer, but begin.txt is still open in the background.
  3. Press Alt+< or Alt+> to cycle through buffers. Alternately, you can use Alt+Left or Alt+Right.

Saving a file (C-o)

To save your work, press Ctrl+o. If the buffer contains the data of an existing file, then Nano confirms that you want to overwrite the old data with the new data. If the buffer is new, then you must provide a file name and location.

Exit (C-x)

To exit Nano, press Ctrl+x. If there's unsaved data, Nano gives you the chance to save your work first.

Establish a home configuration and theme early

As with most text editors on Linux, you can customise GNU Nano with a text configuration file saved to your home directory. When you're just getting started with GNU Nano, you won't know what you need to customise. In my experience, it's nice to have a configuration file close at hand so you can make changes to the application's behaviour as you find the things you want to adjust.

Create a copy of the system default GNU Nano in your home directory:

$ cat /etc/nanorc > ~/.nanorc

Alternately, you might want to use a theme to make Nano more visually appealing. The Catppuccin-nano project provides 4 themes: Latte is a light theme, Frappe and Mocha and Macchiato are dark themes. Each theme has its own .nanorc file and theme files for syntax highlighting, so I symlink my ~/.nanorc to the one I want to use. You must also correct the path to the syntax highlighting files. For example, to use the Frappe theme:

$ git clone \
https://github.com/ahmadnassri/catppuccin-nano.git \
~/.nano
$ ln -s ~/.nano/dist/frappe/.nanorc ~/.nanorc
$ sed -i 's_/\*.nanorc_dist/frappe/?*.nanorc_g' ~/.nanorc

To change themes, just reassign the symlink and correct the path to the syntax highlighting files if you haven't already:

$ ln -s ~/.nano/dist/frappe/.nanorc ~/.nanorc
$ sed -i 's_/\*.nanorc_/dist/frappe/?*.nanorc_g' ~/.nanorc

Regardless of how you generate your .nanorc file, refer to man nanorc to discover all the options available to you.

Simplicity of Nano

GNU Nano makes a nice substitute for Vim or Emacs. It's installed by default on most Linux systems, it's simple to use, and it's got just enough of the right features and flexibility to make it convenient and comfortable. Spend some time with Nano the next time you're working on a configuration file or a text document, make some customisations to it, and see if it's a useful tool to add to your kit.

Buy the book!
From Markup to Markdown