When working with Linux systems, you will come across the need to create or edit text files. The most popular command-line editors are Vim and Emacs, but they have a steep learning curve. For an easy-to-use, simple text editor, Nano is the way to go.

GNU nano is a command-line text editor made for Unix and Linux operating systems, including all the basic functionalities of a regular text editor. You have the ability for syntax highlighting, editing text, multiple buffers, search and replace, spell checking and more.

It has a simple and easy-to-use interface, and throughout this tutorial, we will cover all the basics (and a bit more) of using the nano text editor.

In this tutorial, we will be using screenshots taken from Ubuntu 20.04.

Prerequisites

  • Basic knowledge of using the terminal
  • buy linux vps with your preferred operating system

How to install Nano?

We will start the guide by installing and setting up Nano on your Linux system. In most cases, the Nano-text editor is pre-installed on macOS and most Linux distros. You can check it by entering:

nano --version

If you don’t have Nano installed on your system, you can install it using the following commands.

  • For Ubuntu and Debian:

sudo apt install nano

  • For CentOS and Fedora:

sudo yum install nano

  • For Arch Linux and derivatives:

sudo pacman -S nano

  • For openSUSE, SUSE Linux and derivatives:

sudo zypper install nano

How to use nano

Once Nano is installed, you can test it by entering the following command:

nano

You will see a window similar to the one seen above.

All commands are prefixed with either ^ or character. The represents the Ctrl key, and represents the Alt key. To display the complete list of commands, you can press Ctrl + g. 

To exit the nano editor it’s simple. Press CTRL + x and press enter.

How to create a text file?

To create a text file using the nano command, enter the following (Note that instead of filename, enter the name of your file):

nano filename

You can enter all the text you need immediately in the window. Once you have finished the document or if you have edited the document, save it and exit the editor.

How to save the text file?

There are two ways to save the text file.

  1. Save it and keep editing.
  2. Save it and exit the nano editor.

To save the file’s current workings, you can press Ctrl + O. Nano will ask for the file’s name, so you can just hit enter. If you provide a name to the file, Nano will save a new file with the new name and the newly edited content.

Once the file is saved, you will see a success message “[Wrote 9 lines]”.

The second way to save the file is by exiting the file. Press Ctrl + x, and when prompted to save the modified content, press and click enter. 

Moving around the text editor

You can use the HomeEndPage UpPage Down, and the arrow keys to move around the file.

Instead of using the above-mentioned keys, you can use some of the key combinations that are mentioned below:

  • Type Ctrl + F and Ctrl + B to move the cursor forward or backwards.
  • Type Ctrl + P and Ctrl + N to simultaneously move up and down one line.
  • Instead of the Home and End keys, you can use Ctrl + A and Ctrl + E, respectively.
  • To move a page, use Ctrl + V.To move up a page, use Ctrl + Y.
  • To see where your cursor is currently, press Ctrl + C.

Editing an existing file?

To edit an existing file, you can use the following commands.

  • If the file you want to edit is in the current directory (the folder you are in), you can enter the file’s name after Nano. The command will look something similar to this:

nano filename.txt

  • You can use the following command structure to edit a file not in the current directory.

nano /home/user/folder1/file.php

  • To open a file with the cursor on a specific line and character, you can use the following syntax:

nano +line_number,character_number filename

Here’s an example where the editor is requested to put the cursor on the 3rd line, 15th character;

Searching a text

This is a common feature among most text editors, and Nano handles this in the simplest way possible. To search for a particular word inside the text editor, use the “where is” option by pressing Ctrl + w.

This will open a search prompt where you can type in the text you want. To move to the next result, press Alt + w.

Replacing a text file

To replace a word, you should first open the search bar using Ctrl + w and then press Ctrl + R. Enter the word you want to replace, then enter the word that should be replaced with it.

When making the replacements, you can make them for all the instances in the text or for specific ones by choosing them individually.

Here’s an example. I want to replace the word ‘dummy’ with ‘normal’ in all instances. Check the screenshots below:

To learn more about other Linux Commands, Read more

Copy and paste in Nano

In Nano text editor, you can copy and paste content from one place to another. However, the keys for copying and pasting are different from the Windows shortcut.

You can use the Shift + arrow keys to select the text needed. In the Nano command line text editor, to copy a text, use Alt + 6 and paste the text using Ctrl + U.

Instead of copying a piece of text, you can use Ctrl + K to cut it and to use the Ctrl + U command, you can paste it to the needed place.

Undo and Redo

Did you delete a line by accident? Have you pasted the content in the wrong place? Here’s how you can undo an action and redo it.

To undo an action, use Alt + U.

To redo an action, use Alt + E.

Spell check in Nano

Nano text editor does not have an in-built spell check feature. However, to enable this feature, you can install the spell package. Run the following command in the terminal to install the spell package:

sudo apt install spell

Once installed, you can spell-check by entering Ctrl + T inside the text editor. It will select the misspelt word and ask for the correct replacement.

Check the picture below:

Conclusion

We have mentioned all the usages of the nano command line text editor. It is a popular text editor among most Linux users. The most basic use of nano text editor comes in 3 simple steps:

  1. Enter Nano followed by the filename
  2. Edit the file as needed
  3. Use Ctrl + x to save and exit Nano

You can always use the Ctrl + G command to check for all the additional commands.

People also read: