If you’re starting your Linux journey, creating and editing files is one of the critical things you’ll be doing, especially if you’re planning to get your hands dirty with shell scripting. One of the most common command-line text editors on Linux is GNU nano, which comes pre-installed on most modern Linux distros.

nano supports vital features like syntax highlighting, completion, spell checking, and more 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.

GNU nano version displaying in the terminal

How to Install GNU nano on Linux

Depending on your Linux distro, nano might already be pre-installed. Open the terminal and run thenano –versioncommand to check if it is. If the command line responds with a version number, you have nano installed.

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

GNU nano text editor open

On Debian/Ubuntu

If you’re using a Debian or Ubuntu system, open the Linux terminal and runsudo apt update. After that, run:

Runningsudo apt updatebefore installing packages is highly recommended but not mandatory. It ensures that your local package index is up-to-date and synchronized with the remote repositories. This helps prevent potential issues such as installing outdated or incompatible packages.

A bash script open in GNU nano

On RHEL/CentOS

Runsudo yum install nanoto install GNU nano on RPM-based Linux distros. On newer systems with YUM replaced by the DNF package manager, run:

The latter also works for Fedora.

On Arch Linux

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

How to Open and Exit GNU nano

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

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

Search text feature in GNU nano

To exit, pressCtrl + X. The editor will close immediately. However, if you enter some text into the blank file, nano will ask if you’d like to save the changes first.

If you don’t want to save the changes, pressNto exit the editor immediately. Otherwise, pressY, enter a file name (for example,example.txt), and pressEnterto exit.

Searching and replacing text in a file using GNU nano text editor

How to Work With Files in GNU nano

Working with files is the key function that you’ll be using nano for. You need to know how to open files, create new ones, copy, cut, paste, and even save changes. Once you’ve mastered these basics, you’ll have it easier using nano moving forward.

Opening Files in nano

To open an existing file in nano, you must know the file’s name and extension. This might sound obvious, but it’s crucial because nano will assume you’d like 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 fromthe Linux command linewhere the file is located. After that, execute this command to open a file:

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

Remember, file names are case-sensitive.

Creating, Editing, and Navigating Files

In nano, you can create a file in two ways. You can specify a file name with thenanocommand or begin by opening the editor first, then save your changes and specify a file name. It doesn’t matter the method you choose to use because you’ll still confirm the file name while saving the changes.

To create a file in nano, run:

nano will open with a blank text file. Alternatively, run thenanocommand to open nano in blank mode first.

Editing files in nano is not that complicated, either. Type whatever you want, then pressEnterto start a new line. If you need to jump at the beginning or end of a line, hitCtrl + AorCtrl + E, respectively.

Another way to navigate easily through a file in nano is by usingCtrl + Vto go to the end of the file orCtrl + Yto jump to the beginning. If you have a large file that spans several pages,Ctrl + Ywill scroll one page up andCtrl + Vone page down.

Searching Specific Terms in a File

To search for specific text within a file, useCtrl + W, then enter the search term. By default, nano will only take you to the first matching result, which will be highlighted. However, you may comb through the entire file to find other instances of the term usingAlt + WorOption + W(if you’rerunning Linux on a Mac).

Search and Replace Text

You can search and replace text in nano as well, which comes in handy when you need to replace several instances of a specific term without manually going through the entire file. To do so, pressCtrl + . nano will ask you to provide a word you’d like to replace. Enter the term, then pressEnter.

Next, provide a replacement term and pressEnter. If there’s more than one occurrence of the term to be replaced in the file, you must specify whether you’d like to replace the first or all instances. PressYto replace the first instance orAto replace all occurrences of the searched term.

Copy, Cut, and Paste Text in nano

To copy the text in nano, go to the specific text you’d like to copy and place the cursor at the beginning. Next, hitAlt + AorOption + Ato launch nano’s text selection tool. Then, press the forward arrow key (>) on your keyboard until all the text you’d like to copy is covered with a white background.

PressAlt + 6to copy the text orCtrl + Kto cut. Navigate to the location you’d like to place your copied or cut text and hitCtrl + Uto paste.

Save File Changes and Exit nano

Once you’re done working on a file, save it by hittingCtrl + O. Confirm that you’re saving to the correct file by pressingEnter.

If you’d like to save the changes to a different file, change the file name to something else before hittingEnter. After that, exit nano by pressingCtrl + X.

Efficiently Work With Linux Files Using GNU nano

GNU nano is a simple and beginner-friendly command-line text editor on Linux. It packs enough features to make working with files easier from the command line without overwhelming you with tons of functionality that you don’t need as a beginner.

By mastering the tips highlighted above, using GNU nano will make your Linux experience better when working with files from the command line.

Q: Why Is the nano Command Not Working on Linux?

If you bump into errors while running GNU nano, chances are that it’s either not properly installed on your machine or not installed at all. Try going through the installation steps again and see if the error persists. Also, make sure you’re not making any typos while issuing the command.

Q: Where Is GNU nano Installed on Linux?

To view the path where the GNU nano binary is stored, use thewhich or whereis commands. The output will display the exact path of the GNU nano binary.

If you don’t get an output, it means you don’t have GNU nano installed on your Linux PC.

Q: How to Run GNU nano in the Background in Linux?

To run GNU nano in the background, simply add an&at the end of the nano command. For instance:

Another way is to run the nano command first, hitCtrl + Zto stop the process, and then typebgin the terminal to send nano to the background. Otherways to run Linux commands in the backgroundalso exist, and they’ll work for GNU nano too.