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.
How to Install GNU nano on Linux
Depending on your Linux distro, nano might already be pre-installed. Open the terminal and run the nano --version command 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 the reasons why Linux loves to use the command line).
On Debian/Ubuntu
If you're using a Debian or Ubuntu system, open the Linux terminal and run sudo apt update. After that, run:
sudo apt install nano
Running sudo apt update before 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.
On RHEL/CentOS
Run sudo yum install nano to install GNU nano on RPM-based Linux distros. On newer systems with YUM replaced by the DNF package manager, run:
sudo dnf install nano
The latter also works for Fedora.
On Arch Linux
To install GNU nano on Arch Linux-based distros, run:
sudo pacman -S nano
How to Open and Exit GNU nano
The nano text editor is now installed, but how do you open it?
Open the Linux terminal and run nano. nano will open in the terminal, allowing you to create a new file.
To exit, press Ctrl + 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, press N to exit the editor immediately. Otherwise, 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 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 from the Linux command line where the file is located. After that, execute this command to open a file:
nano filename.extension
For example, if you want to open example.txt, run:
nano example.txt
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 the nano command 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 filename.extension
nano will open with a blank text file. Alternatively, run the nano command to open nano in blank mode first.
Editing files in nano is not that complicated, either. 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, hit Ctrl + A or Ctrl + E, respectively.
Another way to navigate easily through a file in nano is by using Ctrl + V to go to the end of the file or Ctrl + Y to jump to the beginning. If you have a large file that spans several pages, Ctrl + Y will scroll one page up and Ctrl + V one page down.
Searching Specific Terms in a File
To search for specific text within a file, use Ctrl + W, then enter the search term. By default, nano will only take you to the first matching result, which will be highlighted. However, you can comb through the entire file to find other instances of the term using Alt + W or Option + W (if you're running 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, press Ctrl + \. nano will ask you to provide a word you'd like to replace. Enter the term, then press Enter.
Next, provide a replacement term and press Enter. 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. Press Y to replace the first instance or A to 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, hit 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'd like to copy is covered with a white background.
Press Alt + 6 to copy the text or Ctrl + K to cut. Navigate to the location you'd like to place your copied or cut text and hit Ctrl + U to paste.
Save File Changes and Exit nano
Once you're done working on a file, save it by hitting Ctrl + O. Confirm that you're saving to the correct file by pressing Enter.
If you'd like to save the changes to a different file, change the file name to something else before hitting Enter. After that, exit nano by pressing Ctrl + 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.
FAQ
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 the which or whereis commands. The output will display the exact path of the GNU nano binary.
whereis nano
which nano
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:
nano filename.txt &
Another way is to run the nano command first, hit Ctrl + Z to stop the process, and then type bg in the terminal to send nano to the background. Other ways to run Linux commands in the background also exist, and they'll work for GNU nano too.