Nano: 7 tips & tricks to the alternative to vim

Nano is a text editor I learn in college. I use it daily, and I think it’s a very friendly alternative to vi/vim. So we are going to learn how to use it and some hidden tricks.


We are going to check if we have it install with:

$ nano --version
 GNU nano, version 5.6.1
 (C) 1999-2011, 2013-2021 Free Software Foundation, Inc.
 (C) 2014-2021 the contributors to nano
 Compiled options: --disable-libmagic --enable-utf8

If we don’t have installed, we are going to use on Debian base OS:

$ sudo apt install nano

And this on CentOS base distros:

$ sudo yum install nano

Now we are going to create a new file to edited with nano

$ nano test

We can see on top the name of the file with the version of nano we are using. On bottom, [New File] telling us that we are not editing an existing file, we are creating and editing a new file.

Also, we can see the different commands we can use, for example, if we want to save the file, we use “Write Out” in this case say ^O, this means Ctrl+O.

NOTE: Any command prefixed with a caret symbol (^) means to use the Ctrl key (e.g., ^G means to press the Ctrl+G keys at the same time). Any command prefixed with the letter M means to press the Alt key (e.g., M-R means to press the Alt+R keys together).

Typing Ctrl+G (^G) or F1 we can see the help menu:

The help text continues

Tips & Tricks

  1. Search and replace:

With ^W you can forward search and with ^Q make a backward search. This is very useful for editing big conf files. Also, if the result isn’t what you’re looking for, instead of doing another ^W/^Q you can type Alt+W or Alt+Q to find the next result


  1. Enable line number:

Many times we have an error in our yaml or conf files, and the server throws an exception with the line number where it could be the mistake. We can see the line number with Alt+Shift+3


  1. Go to a specific line:

If you need to go to a specific line, just press ^/ and type the number of line


  1. Replace one or many words:

First you are going to type ^\ then write the word to replace

Then you need to write the replacement, nano is going to ask what instance you want to replace, you can select the marked word or just type T and change all the instances.


  1. Copy, Cut & Paste:

To copy a text, you first need to mark it. To start a mark press Alt+Shift+A, then use the arrow keys to select and highlight the text. Then press Alt+6 to copy, to place it where you want press Ctrl+U

Mark, Cut and Paste

If you want to cut a text press Ctrl+K:

foo
bar
line3

Cut foo:


bar
line3

Uncut foo with Ctrl+U at the end:


bar
line3
foo

  1. Create a backup of the file we are going to edit:

If we may want to keep temporary copies of the same file just in case, we can use nano’s -B option, which will create backup of the file we are editing. This can be use it in combination with the -C option to tell nano where to save those backups like this:

$ nano -BC ~/backupFolder file.txt

  1. Editing nanorc, the conf file of nano:

We can create a configuration file for nano, first we need to create the file just typing

$ touch ~/.nanorc

Now we’re going to edit it just using nano

$ nano ~/.nanorc

If we want to display line number like in the tip 3, instead of typing Alt+Shift+3 we’re going to set:

set linenumbers

Save the file and open an if you open a file with nano, the line numbers will appear at start. You can check if the configuration it’s correct looking at the words colors, if is red, the setting is wrong, but if it green it’s right.

There are several configurations for nanorc, you can check the full list here. Some interesting ones are:

# When saving a file, create a backup file by adding a tilde (~) to the
# file’s name.

set backup

# Make and keep not just one backup file, but make and keep a uniquely
# numbered one every time a file is saved -- when backups are enabled with
# set backup or −−backup or −B. The uniquely numbered files are stored in
# the specified directory.

set backupdir directory

# Do case-sensitive searches by default.

set casesensitive

# Save the last hundred search strings and replacement strings and
# executed commands, so they can be easily reused in later sessions.

set historylog

# Use the blank line below the title bar as extra editing space.

set morespace

# Enable mouse support, if available for your system. When enabled, mouse
# clicks can be used to place the cursor, set the mark (with a double
# click), and execute shortcuts. The mouse will work in the X Window
# System, and on the console when gpm is running. Text can still be
# selected through dragging by holding down the Shift key.

set mouse

# Use smooth scrolling by default.

set smooth

# Allow nano to be suspended.

set suspend

# Save automatically on exit, don’t prompt.

set tempfile

Leave a Comment

Your email address will not be published. Required fields are marked *