7 most useful nano editor commands that you should know

Endrit Qerreti

Endrit Qerreti

If you are a new Linux user, and have never heard about nano, nano is a free and open source editor that allows you to create and edit text files on your Linux machine, directly from the terminal.

GNU nano is a command line app, which means you can use nano only via the terminal.

In this tutorial, you will learn how to open, create, save, read, replace and search text files with nano editor on Linux.

Open a text file with nano

To open a text file by using nano text editor, is quite simple. And it can be done via two different methods.

Method 1

To open a text file via nano text editor, simply navigate to the directory where this file is located, and then open the text file using nano from the terminal.

Navigate to the directory where the text file is located.

cd text-directory

Next, open the text file via the command below.

nano file.txt

Method 2

You can also open the text file with nano text editor, without having to navigate to the directory where your file is located.

You can easily open the text file, by simply specifying the directory where this file is located.

Let's say we are trying to open a text file located in Desktop.

Then to read this file, we can do so by running the command below.

nano ~/Desktop/file.txt

Create a file using nano

Creating a text file by using nano text editor is quite simple. All you need to do is run nano editor and specify the name of the file that you will be creating.

For example, to create a file by specifying the name of the file, simply execute the command below.

nano file.txt

The command above will create an empty text file called file.txt, once this file is opened in your terminal, proceed to write your content, once done you need to save changes by pressing CTRL + S.

To exit, press CTRL + X.

Save a file using nano

If you've made changes to a file with nano text editor, and want to save these changes, all you need to do is press CTRL + S.

If you are editing an existing file, then changes will be saved automatically.

In cases when the file doesn't exist, saving changes will create a new file, and you will be prompted to choose a name for this file, once you enter the name, you need to press enter to proceed saving changes to the new file.

Read a file using nano

If you are not planning to make changes to a file, and want to open it in the read-only mode, you can do so by using the CTRL + R shortuct.

1) Open nano editor

nano

2) Open file in the read-only mode

To open a file in the read-only mode using nano editor, is very easy. All you need to do is press CTRL + R, and then enter the name of the file that you want to open in the read-only mode.

Let's say we want to open file.txt in read-only mode. To do so, simply enter file.txt when asked

Search for a text using nano

nano also allows you to search for a text in a text file that you have opened with nano editor.

To search for a text using nano editor, is quite simple, and it can be done by pressing CTRL + W shortcut.

Once you press CTRL + W, you will be asked to enter the text that you want to search, enter your text and then simply press enter to search.

Replace a text using nano

Replace option, allows you to replace anything you want in a text file.

To do so, simply press CTRL +\.

Once you press CTRL +\, you will be asked to enter the text/term that you want to replace, type the text that you want to replace, and then press enter, next you need to type the new text that will replace the old text, and finally press Y to replace that text.

Get the full list of commands

To view the full list of commands that you can use on nano editor, launch nano editor, and then simply press CTRL + g to view the full list of commands.

1) Launch nano text editor

nano

2) Press CTRL + g.

Conclusion

By now you should know how to open, read, create and do a lot more with nano text editor on Linux.