In this tutorial, you will learn how to install and use Nano text editor. Learning Nano will enable you to edit text files in your VPS on the command line when you are logged in using SSH.
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 this tutorial, we will learn how to install nano on Ubuntu and CentOS. You will also learn some basic nano commands for text editing.
Table of Contents
What you’ll need
Before you begin this guide you’ll need the following:
- Access to a machine running Ubuntu or CentOS (remote ssh access to a machine running one of these operating systems will also work )
Step 1 — Installing Nano Text Editor
It depends on what OS you have, in particular, some of them already come with the Nano text editor installed. If that is your case, you can click here to skip this step and head over to the Step 2 part of this tutorial.
To install Nano text editor, you need to have any kind of package installer on your system.
Install Nano On Debian / Ubuntu
sudo apt-get install nano
IMPORTANT: Sudo part is meant for running this command as a root user, so you may be prompted to enter a password in case you are not logged into as a root user at the moment.
Install Nano On CentOS / RHEL
yum install nano
That is basically is for installation, you should be able to use nano text editor now. You can test it out by writing this command:
It should open a new text editor window and if you make any changes to this file, they will be saved under the name test.txt. To exit Nano, for now, press CTRL + X. It will you ask whether to save changes in the file. Enter y for yes and then press ENTER.
Step 2 — Using Nano text editor
IMPORTANT: If you are Mac user, change CTRL to CMD.
Opening and closing the Nano editor
The main command to open the editor is:
IMPORTANT: Instead of the filename you can enter your desired new file name or an existing one. Keep in mind that if you want to edit an existing file, you must be in the directory where the file is. Alternatively, you can specify the full path to the file.
For example, to open a file in the current working directory
To open a file in another directory
Note : Nano will create a new file of the given filename if it is not present in the directory. If no filename has been specified it will create an empty untitled file and will ask for a filename at the time of exiting editor if any changes are made.
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 see some of the shortcuts to use the 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. Enter y if you want to save changes and then press ENTER.
BONUS TIP: whenever you are opening a config file with nano use -w option. As 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:
1 sudo nano -w /etc/apache2/apache2.conf
To select text go to the beginning of the text to be selected and press ALT +A. This will set a mark for selecting, then move over the text to be selected with the arrow keys. This will mark the text selected. To copy the selected text press ALT + 6 . This will copy text to the clipboard. To cut text press CTRL + K. To paste text press CTRL + U. If no next 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 search text and press ENTER. To search again for the same text press ALT + W .
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. It will take you to the first instance of the text and ask you whether to replace it or all of the occurrences.
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.
In this tutorial, you have learned how to install and use the Nano text editor. For more bash, SSH and VPS related tutorials, visit our tutorial page here.