How to install and use GNU nano to edit files on Linux

One of the most popular command-line text editors on Linux is GNU nano, which comes pre-installed on most modern Linux distributions.

If you're starting your Linux journey, creating and editing files is one of the important things you'll have to do, especially if you're planning to get familiar with shell scripting. One of the most popular command-line text editors on Linux is GNU nano, which comes pre-installed on most modern Linux distributions.

nano supports important features like syntax highlighting, completion, spell check, etc., and is beginner-friendly, unlike feature-rich alternatives like Vim and Emacs. Here's how you can install and use GNU nano to work with files on Linux.

How to install GNU nano on Linux

Depending on your Linux distribution, nano may already be installed. Open terminal and run command nano --version to check if it is available. If the command line responds with a version number then you have nano installed.

How to install and use GNU nano to edit files on Linux Picture 1How to install and use GNU nano to edit files on Linux Picture 1

If not, you need to install nano. There are many different ways to do so. However, using the default command line package manager is by far the fastest and easiest way (one of the reasons why Linux prefers to use the command line).

 

On Debian/Ubuntu

If you are using a Debian or Ubuntu system, open a Linux terminal and run sudo apt update . Then run:

sudo apt install nano

Note : Running sudo apt update before installing packages is highly recommended but not required. It ensures that your local package index is up to date and in sync with remote repositories. This helps prevent potential problems such as installing outdated or incompatible packages.

On RHEL/CentOS

Run sudo yum install nano to install GNU nano on RPM-based Linux distributions. On newer systems with YUM replaced by the DNF package manager, run:

sudo dnf install nano

The following command also works with Fedora.

On Arch Linux

To install GNU nano on Arch Linux-based distributions, run:

sudo pacman -S nano

How to open and exit GNU nano

The nano text editor is now installed, but how do I open it?

Open Linux terminal and run nano . nano will open in the terminal, allowing you to create a new file.

How to install and use GNU nano to edit files on Linux Picture 2How to install and use GNU nano to edit files on Linux Picture 2

To exit, press Ctrl + X . The editor will close immediately. However, if you enter some text into an empty file, nano will ask if you want to save the changes first.

If you don't want to save your changes, press N to exit the editor immediately. If not, press Y , enter a file name (for example: example.txt ) and press Enter to exit.

 

How to work with files in GNU nano

Working with files is the main function that you will use nano. You need to know how to open files, create new files, copy, cut, paste, and even save changes. After mastering these basics, it will be easier for you to use nano in the future.

Open the file in nano

To open an existing file with nano, you must know the name and extension of the file. This may sound obvious but it's important because nano will assume you want to create a new file if you don't enter the correct name with the right extension.

Then you also need to navigate to the exact location from the Linux command line where the file is located. Then execute this command to open the file:

nano filename.extension

How to install and use GNU nano to edit files on Linux Picture 3How to install and use GNU nano to edit files on Linux Picture 3

For example, if you want to open example.txt, run:

nano example.txt

Note : Remember that filenames are case sensitive.

Create, edit and navigate files

In nano, you can create files in two ways. You can specify the filename with the nano command, or start by opening the editor first, then save your changes and specify the filename. It doesn't matter which method you choose to use as you will still confirm the filename while saving the changes.

To create a file in nano, run:

nano filename.extension

nano will open an empty text file. Also, run nano command first to open nano in blank mode.

Editing files in nano is not too complicated. Type whatever you want, then press Enter to start a new line. If you need to jump at the beginning or end of a line, press Ctrl + A or Ctrl + E respectively.

Another way to easily navigate through a file in nano is to use Ctrl + V to go to the end of the file or Ctrl + Y to go to the beginning. If you have a large file that spans multiple pages, Ctrl + Y will scroll up one page and Ctrl + V one page down.

Search for specific terms in a file

How to install and use GNU nano to edit files on Linux Picture 4How to install and use GNU nano to edit files on Linux Picture 4

 

To search for specific text in a file, use Ctrl + W , then enter the search term. By default, nano will only take you to the first match, which will be highlighted. However, you can browse the entire file to find other versions of the term using Alt + W or Option + W (if you are running Linux on a Mac).

Search and replace text

You can also search and replace text with nano, which is useful when you need to replace several instances of a specific term without looking at the entire file manually. To do so, press Ctrl + . nano will ask you to provide a word you want to replace. Type the term, then press Enter.

Next, provide an alternative term and press Enter. If there is more than one occurrence of the term that needs to be replaced in the file, you must specify whether you want to replace the first instance or all. Press Y to replace the first instance or A to replace all occurrences of the searched term.

How to install and use GNU nano to edit files on Linux Picture 5How to install and use GNU nano to edit files on Linux Picture 5

Copy, cut and paste text in nano

To copy text using nano, go to the specific text you want to copy and place the cursor at the beginning. Next, press Alt + A or Option + A to launch nano's text selection tool. Then, press the forward arrow key (>) on your keyboard until all the text you want to copy is covered with a white background.

How to install and use GNU nano to edit files on Linux Picture 6How to install and use GNU nano to edit files on Linux Picture 6

Press Alt + 6 to copy text or Ctrl + K to cut. Navigate to where you want to place the copied or cut text and press Ctrl + U to paste.

Save changes to the file and exit nano

After you finish working on a file, save it by pressing Ctrl + O . Confirm that you are saving to the correct file by pressing Enter.

If you want to save changes to another file, rename the file to another name before pressing Enter. Then, exit nano by pressing Ctrl + X .

4 ★ | 2 Vote