The GNU nano text editor is a user-friendly, free and open-source text editor that usually comes pre-installed in modern Linux systems. It packs with the very basic functionality that any command-line text editor should have such as search and replace, undo and redo, scrolling, & syntax highlighting.
How to Install Nano Text Editor
As we pointed out earlier, nano editor comes pre-installed in modern Linux systems. To verify if nano is installed, run:
You should get the output as shown below:
However, if nano is not yet installed, here are some ways on how you can get it installed in your system:
For Ubuntu / Debian:
Run the command below to install nano.
For RedHat and CentOS based systems, run the command:
$ sudo dnf install nano (For newer versions)
How to Create a File Using Nano Editor
For example, to create a simple text file called file1.txt, execute the command:
This opens up a blank nano editor with the file name at the top and a set of keyboard shortcuts for use with the command-line editor at the bottom of the console.
Commands are prefixed by the caret symbol (^) which represents the Ctrl key. For example, ^O implies that you press the Ctrl and O keys.
To get a list of all the keyboard shortcuts you can use with nano editor, press ^g or Ctrl + g.
Searching and Replacing Text
To search for a string in a text file, just hit Ctrl + w and thereafter, type the search keyword and press ENTER. In the example below, I’m searching for the string Allow in the /etc/ssh/sshd_config configuration file.
To proceed to the next matching string, just press Alt + w. To search and replace a string in a text file, hit Ctrl + \. You will be required to provide the string to be searched and replaced as shown below.
So, provide the keyword and press ENTER. In our example, we’re searching for the string UNIX and replacing it with the string Linux.
Once you hit ENTER, you will be prompted to provide the string to replace the search keyword as provided. Here, the string Linux is the string that will replace UNIX. Once again, hit ENTER.
Press ‘Y’ to confirm and press ENTER.
And finally, the keyword will be replaced.
Copying, Cutting and Pasting
To start copying text, move the cursor to the start of the text and hit Alt + a. Nano sets a selection mark from that point. Here, the cursor is positioned at the beginning of the first line.
Next, press the arrow forward key until the end of the text you want to copy. This will have the text highlighted as shown. Here, I have selected the entire line. If you want to cancel and start all over again, just hit Ctrl + 6.
To copy the text, press Alt + 6. To cut it, press Ctrl + k. Finally, to paste the text, move the cursor to your preferred location on the editor and hit Ctrl + u.
Saving & Exiting a File
To save a file, simply press the combination Ctrl + O. As you do so, make sure you have the write permissions which allow you to modify the file. Nano will require you to verify the file that you are saving the changes on.
Press ENTER to save the changes. You will get some information on the number of lines saved.
To exit the file press Ctrl + X.
And that’s it for this guide on the Nano text editor. We hope you are enlightened and you can easily navigate through the editor.