Nano is a simple and reliable command-line text editor included in Unix and Linux operating systems. It packs with all the essential features in a command-line text editor such as UTF-8 encoding, syntax highlighting, search and replace, support for regular expressions, and more.
Nano is best for simple edits and a great editor for beginners.
In this tutorial, we learn the basic usage of Nano editor such as opening and creating a file, copy/cut/paste, search/replace, saving, and exiting.
Install Nano in Linux
The Nano editor comes preinstalled in newer versions of Linux Distributions. To confirm that Nano is installed, run the command:
$ nano --version
If Nano is not installed, here’s how to install it.
Install Nano on Debian/Ubuntu
For Debian and Ubuntu distributions, execute the following command:
$ sudo apt install nano
Install Nano on CentOS/Fedora/RHEL
For RedHat distributions such as CentOS & Fedora, run the command:
$ sudo dnf install nano
With Nano installed, let’s check basic usages.
|1||Open file||nano <file name>|
|2||Edit the file|
|3||Save File||Ctrl + O and press Enter key|
|4||To Exit||Ctrl + X|
How to open a file
To open a file containing text, simply invoke the
nano command, followed by the name of the file as shown.
$ nano filename
How to create a file
Nano command followed by a new filename will create a new file.
For example, to create a new file named greetings.txt, execute:
$ nano greetings.txt
This opens a blank GNU Nano editor. The Nano editor screen will show the version of Nano and the name of the file displayed just below the menu bar.
At the very bottom is a set of keyboard shortcuts to use with Nano. All shortcuts are prefixed by the caret symbol ( ^ ) which denotes the Ctrl key on the keyboard.
For instance, ^O means to press Ctrl + O.
To get a list of all the command shortcuts, press Ctrl + g. This opens Nano’s main help text.
How to edit a file with Nano
Unlike Vim editor, Nano is modeless, which implies that you can simply start typing and modifying text right after opening the file.
NOTE: To open a file using Nano, ensure that you have the right permissions to do so, otherwise, you will bump into a notification below informing that the file cannot be written. As a workaround, simply assign the necessary permissions to the user or assign sudo permissions to the user.
Copying , cutting and pasting text
With Nano, one can easily copy/cut and paste text. To better demonstrate this, We have created a simple text file called
greetings.txt and added the content shown.
$ sudo vim greetings.txt
To start copying text, place the cursor at the beginning of your preferred text and hit
Alt + a. This creates a selection marker and a ‘Mark Set’ message is indicated at the bottom.
The next step is to highlight the text to be copied. So, press the Arrow forward key up to the end of the text to highlight the text. In the example shown, we have highlighted the first sentence. To deselect or cancel the selection, press
Ctrl + 6.
To copy the highlighted text to the clipboard , press
Alt + 6.
To cut the highlighted text, instead of copying, press
Ctrl + k. Cutting completely removes the text.
Once the selected text is copied or cut, navigate to the desired location where you want to paste the text and press
Ctrl + u.
Search and replace
Nano editor allows to perform search and replace text. To search for text, just press
Ctrl + w then type in your search keyword. Here, we are searching for the string “Linux”.
This takes you to the first instance of the matching keyword.
To proceed to the next match, press
Alt + w.
To search and replace a string, press
Ctrl + . This prompts you to provide the search keyword. Here, we want to search for the string “Linux” and replace it with “Unix”. Once you have typed the string, press ENTER
Provide the string to replace the search keyword. In this case, we have provided “Unix” as the replacement.
Then press ENTER once more. You will be asked to confirm the changes. Press ‘Y’ to confirm, and if in doubt, simply hit ‘N’.
Finally, the string will be replaced.
Saving and Exiting text
To save changes made to a text file, simply press
Ctrl + O and will confirm the Filename to Write then press Enter key. If the file didn’t exist, it will be automatically created.
To exit, simply press
Ctrl + X.
If attempt to exit without saving the changes, Nano will remind you that there are some unsaved changes and will require you to take action. Pressing ‘Y‘ will save the changes while ‘N‘ will ignore saving the changes and exit Nano.
Configure Nano as the default text editor
On most major systems, the default command-line text editor for utilities such as crontab is set to vi. To make GNU Nano editor as the default text editor, define the following environment variables in the ~/.bashrc file.
Then save the changes made and exit the ~/.bashrc file.
Syntax Highlighting function helps highlight components source code according to their syntax in a different color or font. By default, Nano includes syntax highlighting rules for popular file types such as bash scripts, HTML, Perl, and python files to mention a few. The syntax files are located in the
These are also included by default in the /
etc/nanorc configuration file.
In this tutorial we learned how to use the Nano editor. You feedback will be much appreciated.