Editing files with nano

Table of contents

Managing a linux system often requires interacting with text files: changing configuration, checking logs or reading through source code are just a few examples. When no graphical interface is available (or desired), the nano editor comes in as a handy way to make changes to text files.

Handling files

Files are opened by passing them as an argument to the nano command. If the file exists, it will be opened, else it will be created when you save:

nano example.c

This will open the editor interface in the terminal, which will look similar to this:

The exact colors depend on your terminal configuration. Note how supports syntax highlighting for most programming languages out of the box.

The most common shortcuts are conveniently listed at the bottom. The syntax for each is a combination of two keys:

  • ^K means press Ctrl+K
  • M-U means press Alt+U

The editor will update automatically when resizing the window. Press Ctrl+G for a list of all shortcuts.


If unsaved changes are present, the editor will add an asterisk * to the filename at the top:

The changes can then be saved by pressing Ctrl+O, prompting for a filename to write to:

To close the editor, press Ctrl+X. If unsaved changes are present in the file, you will be prompted to save or discard them:

Navigating through files

The cursor can be moved with the arrow keys to move through the file contents. For faster movement, you can hold Ctrl while moving with the arrow keys, jumping between words left/right or multiple lines up/down.

If you want to quickly move up/down through a file, you can jump an entire editor height with the Page up and Page down keys. How many lines exactly your cursor moves when pressing those keys depends on the current height of the editor.

The Home and End keys will jump to the beginning or end of the current line, respectively; holding Ctrl while pressing them jumps to the beginning or end of the entire file instead.

Text manipulation shortcuts

Nano does not use the same clipboard that graphical interfaces use, meaning if you copy text within nano, you can only paste it within the same nano instance.

To copy or cut a piece of text, you need to select it first. Set a mark at one end of the text to mark by pressing Alt+A, then move the cursor to the end of the text to mark it:

A message at the bottom will say [ Mark Set ] while in marking mode. Press Alt+A again to go back to normal file editing.

Alternatively, you can hold Shift and navigate the cursor through the file with the arrow keys to mark text.

All text transfer operations are handled by non-standard shortcuts in nano:

  • Cut: Ctrl+K
  • Copy: Alt+6
  • Paste: Ctrl+U

The choice of shortcuts prevents conflicts with other software, for example from the gnome desktop environment.

If you are using a graphical environment, you can alternatively copy/paste text like any other terminal application: mark the text to copy with the mouse, then right-click and choose "Copy". To paste, right click and select "Paste" or press Ctrl+Shift+V.

Undo, redo and backup copies

To recover from errors and typos, nano offers the common undo and redo features of every text editor. Pressing Alt+U reverts the last change, and Alt+E recreates it.

This may help catch problems while editing, but nano provides another feature to safeguard against accidental data loss: backup files. Opening nano with the -B flag will create a backup copy of the file you are editing:

nano -B app.conf

The backup only comes into play when saving changes to the file. When saving, the old contents are written to the filename with a tilde ~ appended. In the example above, saving changes will create a file named app.conf~ with the original file contents, while writing the new content to app.conf.

Searching for text

Nano can search for text both forwards or backwards relative to the cursor position. Ctrl+W searches forward, Ctrl+Q backwards.

Starting a search opens a new line for the search term and changes the shortcuts at the bottom, indicating options to alter the way search is done:

Search behavior can be adjusted with the shortcuts below the search input prompt. Most common are Alt+C to toggle case sensitive search, Alt+R for regular expression matching and Ctrl+R for search and replace. Using one will change the input prompt to reflect the way search will be done:

Found matches are highlighted one at a time:

You can jump to the next match with Alt+W or the previous one with Alt+Q.

Replacing text

Text replacement is done either through the search by pressing Ctrl+W and then Ctrl+R, or directly with Alt+R. After providing the search term, nano will prompt for a text to replace found matches with:

Once both search and replacement inputs are given, nano will prompt you for each match found in the file, asking if you want to replace it:

Pressing Y will replace the current match, N will skip it. Pressing A replaces all matches at once without asking.

Inserting external data

Nano provides a feature to paste external text at the current cursor position. The simplest way is to paste another file's contents with Ctrl+R:

Or by executing a command with Ctrl+T:

Executing a command uses the user's default shell (as defined in env $SHELL). If the user uses bash for example, executing a command in nano will support full bash syntax.

Customizing the editor

Most of the editing behavior of nano can be adjusted to fit your personal needs. Some changes can be made while editing, like pressing Alt+# to toggle line numbers; others need to be enabled with flags.

To persist altered behavior for future nano instances, you can define them in a file named .nanorc in your home directory:

~/.nanorc

set minibar
set autoindent
set linenumbers
set tabstospaces
set tabsize 2

When running nano with the example config above, it will behave more like a common IDE by automatically maintaining indentation, converting tabs to spaces, setting tab size to 2 spaces, showing line numbers and showing the status bar at the bottom.

If you edit the .nanorc file in nano itself, it will automatically spellcheck the configuration for you, marking valid lines in green and invalid ones in red:

The many options available for customization are available in their own manual page called nanorc:

man nanorc

Alternatively, an online version is available on the nano website.

More articles

Understanding bash stream redirection

Controlling the flow of data in and out of programs

Working with tar archives

...and tar.gz, tar.bz2 and tar.xz

Understanding the linux tee command

Copying stdin to multiple outputs

Managing users on linux

A guide on users, groups and authentication

Forwarding docker container logs to Grafana Loki

Advanced log processing for local container environments

Brute-forcing logins with hydra: Attack and defense

How an attacker would crack a login, and how to protect against it