How to Work with Nano Editor on Linux

What is Nano editor

The Nano editor is a simple, display-oriented and free text editor that comes by default with all Linux operating systems. It is a good alternative to the non-free Pico which comes by default with the Pine package. The programs such as Vi and emacs might be more powerful, but the smart feature set of Nano makes it easy to learn and use for beginners. Text insertion and navigation through the files is pretty straightforward and involves only typing the text and basic cursor movement. Here, we will learn all this and the special key combinations that will make you a Nano guru.

Nano vs Pico

A user might choose Nano over Pico for the following reasons:

  • The Nano editor is a very simple program and has the bare minimum functionality required to edit files.
  • It has a built-in cheat sheet for shortcuts that are displayed at the bottom of the editor itself.
  • It is available on most Linux systems.
  • Nano Editor is a very stable program that hardly ever hangs, leaks or crashes.
  • Syntax coloring can be used for many programming languages.
  • It is free!

Running Nano Editor on Linux

Nano for Linux is a Terminal-based text editor so first, you will need to open the Terminal window. Once you have opened the Linux Terminal you can open Nano by using the following command:

$ nano

You will see the following screen with a new buffer/file open:

Nano Editor

As you can see above, the editor is divided into four main sections:

  • The first line at the top displays version number of Nano, the filename( in this case no filename has been specified yet) and whether the file has been modified or not.
  • A line above the shortcuts, displayed at the bottom, shows system messages such as status information from the system.
  • The two shortcut lines at the bottom provide the user with a set of most commonly used shortcuts.

Creating a File

In order to launch Nano and create a new file to work on, use the following command:

$ nano filename

The image below shows a file named filename.txt that we have created to work around for practice.

Create a file with nano editor

Note: If you have opened a new file/buffer without naming it by using only the command “nano”, you can name it while saving the file. Please refer to Saving a file section for more information.

Opening a File

You can open an already existing file by using the following command:

$ nano [file-name]

This command will open the file if it exists in your current directory. You will have to specify the entire location path if the required path exists elsewhere in your storage.

Working with files

Navigating through the file

You can move around your file by using the arrow keys but a quicker way is to use shortcuts with keyboard letters that are usually available in all environments.

Displaying cursor position

Sometimes you will need to know the current position of the cursor within your file. The shortcut ctrl+c will not only highlight the cursor area but also display location details in the status line above shortcuts.

Placing the cursor on specific line and column at startup

You can set up the Nano editor for opening an already existing file with the cursor set at a specific line and column. You can use the following command to specify the line and column of the file while launching Nano:

$ nano +line,column [filename]

Copying, cutting and pasting text

Let us start with writing some sample text that we can select, cut or copy and paste in our file. Selecting the text is very simple in Nano; bring the cursor to that text and select through keyboard or mouse controls.

Copying, cutting and pasting text

In order to cut the selected text, press ctrl+k and then position the cursor where you want to paste the text. Now press ctrl+u; you will be able to see the text pasted to this position.

In this example, we have selected the text “This is some more sample text.” and copied it down the document through ctrl+u.

Note: Use the Alt+^ shortcut to copy the text instead of cutting in.

shortcut to copy the text instead of cutting in

Cutting a string of text

The Nano editor also enables you to cut a string of text instead of the entire line. In the following example, we will cut the string “sample” from our text. Please select the string that you want to cut by placing the cursor at the start of the string and pressing ctrl+6. This will mark the string as selected; the status bar will show the “mark set” message.

Cutting a string of text

Now place the cursor on the position where you want to paste the string and press ctrl+u. You can see the string has been pasted to a new location in the following image:

Paste text string

Spell checking your text

You can spell check your text by using the control: ctrl+t. Although Nano is not a word processor, this added feature helps you have an error-free file when it comes to spellings.

Saving a file

In order to save a file, please use the ctrl+o shortcut. Nano will ask you to enter a file name or re-confirm the file name if you have already specified it. Make the required changes and hit enter; your file will be saved to the system.

Save a file in Nano editor

The status bar will show the number of lines saved in your file as follows:

The file has been written

Inserting one file into another

With Nano, you can merge the contents of one text file to the other. Open a file and press ctrl+R. The Editor will ask you the file name whose text you want to insert in the current file. You can see that in the following image we have entered the filename filnamet.txt.

Inserting one file to another

In the following image, the selected line has been inserted from the specified file.

Text inserted from a second file

Useful Switches in Nano

The Nano editor comes with a number of powerful switches. The most common and useful ones are mentioned below for your use; others can be found in the Nano Editor’s manual:

Switch Purpose
nano -B Use this switch in order to back-up the file before editing it.
nano -E Use this switch in order to convert tabs to spaces when editing your text.
nano -c Use this switch in order to view the cursor position stats continuously.
nano -i Use this switch in order to automatically indent new line(s) with the same position as the previous line.
nano -k Use this switch in order to toggle cut (It cuts from the cursor position instead of cutting the entire line of text.
nano -m Use this switch in order to get mouse support.
nano -v Use this switch in order to open files in read-only mode.

You are now able to create, edit and save Nano files after practicing along with this article. With the help of Nano’s power ful shortcuts, you can easily edit your files such as cut, copy and paste text, spell check your file, copy text from another file etc. So after all, it is not too hard to be a Nano guru!