Linux Applications

How to Use Nano in Linux

From time to time, you find yourself having to write shell scripts or make changes to configuration files of running services. Vim (VI improved) is a widely used command-line editor. While it ships with numerous enhancements including syntax color coding, it has a steep learning curve and new users might find it difficult to use.

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:

$ nano --version

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.

$ sudo apt install nano

For RHEL/CentOS

For RedHat and CentOS based systems, run the command:

$ sudo yum install nano (For older versions)

$ sudo dnf install nano (For newer versions)

For Fedora

$ sudo dnf install nano

 

How to Create a File Using Nano Editor

$ nano file-name

For example, to create a simple text file called file1.txt, execute the command:

$ nano file1.txt

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.

Conclusion

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.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.