In this tutorial, you will learn how to install and use Nano text editor. It will let you conveniently edit text files on a web server via the command line when logged in using SSH. By the end, you’ll know how to set up Nano on the most popular Linux distributions and posses the knowledge of basic nano commands for text editing.
While Vim and Emacs can be overwhelming for novice Linux users, Nano is straightforward and easy to use. That’s why it’s one of the most popular text editors for Unix systems! In fact, it’s included by default on all our products, including web hosting, business hosting, and virtual private servers.
Without further ado, let’s dive in!
Table of Contents
1. How to Install Nano Text Editor
The process depends on the operating system you are running. In particular, some of them already come with the Nano text editor pre-installed. If that is your case, you can go ahead to skip this section to start learning the basic Nano text editing commands.
Installing Nano on Debian/Ubuntu
To install Nano text editor on Debian or Ubuntu machines, execute the following command:
sudo apt-get install nano
Wait for all the processes to finish and the Nano setup will be complete.
IMPORTANT: sudo part is meant for running this command as a root user. This means you may be prompted to enter a password in case you are not logged into as a root user at the moment.
Installing Nano on CentOS/RHEL
To install the Nano text editor on CentOS or RHEL based platforms, run this command:
yum install nano
That is basically it for the installation. As soon as the setup ends, you’ll be ready to use it!
2. How to Use Nano Text Editor
In this part, we’ll dive into the usage of Nano text editor. We will cover:
- How to open and close files.
- Text editing tips and tricks.
- Searching and replacing text.
- Basic commands.
Opening and Closing the Nano Editor
The main command to open the editor is:
Instead of the filename value, you can enter any desired value. Keep in mind that if you want to edit an existing file, you must be in the directory where the file is located. Alternatively, you can specify the full path to the file.
For example, to open a file in the current working directory:
It will open a new text editor window and if you make any changes, they will be saved under the same file name. To open a file in another directory use:
Nano will create a new file with the given name if it is not present in the specified directory. If no name has been specified, it will create an empty untitled file and will ask for a name when exiting the editor.
After running the command, you will be taken to the editor window where you can now freely edit the file with the Nano text editor. Use arrow keys on your keyboard to move the cursor around the text.
At the bottom of this window, you can find some shortcuts to use with the Nano editor. Symbol ^ means that you must press CTRL + [Key] (CMD + [Key] for Mac users) to use the chosen command.
To save the changes made in the file and continue editing press CTRL + O.
To exit from the editor press CTRL + X. If any changes have been made to the currently open file, it will ask whether to save them or not otherwise it will exit right away. Input y for yes, or n for no, and then press ENTER.
BONUS TIP: whenever you are opening an important configuration file, use the -w option. By default, nano wraps the file text to fit the display. This may break some configuration files and may cause problems. For example when opening a config file use:
sudo nano -w /etc/apache2/apache2.conf
In order to select text go to the beginning of the text and press ALT + A. This will set a mark for selecting, then move over the text to be selected with the arrow keys.
To copy the selected text press ALT + 6. This will copy text to the clipboard. To cut text press CTRL + K.
If you want to paste text press CTRL + U. If no text is selected before copying or pasting, it will copy/cut the entire line.
Searching / Replacing Text
To search in the text press CTRL + W. It will ask you for the text to be searched. Insert your value and press ENTER. To continue searching for the same text string, ALT + W.
If your goal is to find and replace text, press CTRL+W and then CTRL+R. It will ask for the search text and the text to be replaced by. Afterward, the editor will take you to the first instance of the text and ask you whether to replace it or all of the occurrences.
Basic Nano Text Editor Commands
Here are some basic nano commands that come in handy while using the editor.
|CTRL + A||Jump to the beginning of the line.|
|CTRL + E||Jump to the end of the line.|
|CTRL + Y||Scroll page down.|
|CTRL + V||Scroll page up.|
|CTRL + G||This command will bring up a Help window which has information regarding all the commands you can use with the Nano editor.|
|CTRL + O||Basically a save command. When used, you will be prompted to edit or verify the desired file name and after pressing enter, it will save your file.|
|CTRL + W||One of the most useful commands. It’s used to search for a specified phrase in your text. It works much like the usual CTRL+F command on other apps and platforms. To search for the same phrase again press ALT + W.|
|CTRL + K||Cuts the entire selected line to the “cut buffer”.|
|CTRL + U||Pastes the text from the “cut buffer” into the selected line.|
|CTRL + J||Justifies the current paragraph.|
|CTRL + C||Shows the current cursor position in the text (line/column/character).|
|CTRL + X||Exits Nano text editor. In case you made any changes to the file, it will additionally prompt a save request.|
|CTRL + R||Open a file with “Read File” command. Inserts file from disk at the current cursor position.|
|CTRL + \||Replace string or a regular expression.|
|CTRL + T||Invoke the spell checker, if available.|
|CTRL + _||Go to specified line and column number.|
|ALT + A||Select text. First, move the cursor to the beginning of a specific part of the text which you want to select, then press ALT + A. Now move the cursor with arrow keys to the right, this will start marking/selecting text. You can combine this command with CTRL + K to cut a specific part of the text to the “cut buffer”.|
Those are the basic, most useful commands to use with the Nano text editor. For more commands, don’t forget that you can use CTRL + G to bring up the Help window.
Looking to learn more server management basics? Here are a few interesting articles:
- A List of Basic SSH Commands
- What is Wget and How to Use It?
- CentOS vs Ubuntu – Which is the Best for a Web Server?
- How to Set Up a TeamSpeak 3 Server
- How to Make a Minecraft Server
In this tutorial, you have learned how to install Nano text editor on the most popular Linux distributions. You also know basic text commands among other useful tips and tricks. Now, you can effortlessly perform text editing tasks without having to download and re-upload the whole file.