Using Nano Command In Linux With Examples

using nano command line editor in linux

The term ‘Nano’ in Linux is a command based text editor helping users to create, edit, and do even more with text files. It’s quite user friendly and simple to use. In compare to VIM editor, the nano is often considered by users as it provides easy GUI (Graphical User Interface) and needs no any modes. To sum up, it offer users to interact with their text files easily. In addition, this text editor also includes various features or functions as well like syntax highlighting buffering, find and replace, UTF-8 encoding, and even more.

In this article, we are going to discuss how to use nano, the command line text editor to create, open, edit, and do even more easily.

Installing Nano (Command Line Text Editor)

In most of the Linux distributions, the nano text editor comes preinstalled, and it can be checked by running the following command:

nano --version

Here comes the output you should receive.

check version of nano

However, if you notice the text editor is not installed on your Linux distribution, you can easily install it by running below mentioned commands:

For Ubuntu/Debian users:

sudo apt install nano

For Centos/Fedora users:

sudo yum install nano

Starting with nano to create/open files

Create/Open a new file, with the command below:

nano servo.txt ( Replace servo.txt with your preferred file name)

create a new file

Here’s the output:

opened new file with various options

As seen in the output, a file named servo.txt is created and opened in active directory. If the specified file name already exists there, will open the same file and you can edit its content according to your needs. At the bottom of screen, you can see various options which we will discuss ahead.

Saving a file with nano

In order to save the file, you can use the following key combination.

CTRL + o

The key combination will ask you to specify a file name. Since we have already named the file as servo.txt, we can keep the same name by pressing Enter key. If you prefer to save the file with a new name, just change the name as mentioned in the image below:

saving a file

Cut/Paste a line with shortcuts

In order to cut or paste according to your requirements you can use CTRL + k and CTRL + u key combinations respectively.

Means, if you need to cut a line or selected text, just select them and press CTRL + k. and obviously you can press CTRL + u combination to paste the text to position where you want. Don’t forget to move the cursor at the position where you need to paste it.

Note: In case you need to copy selected text or line, you can use Alt + 6 key combination and paste it back using CTRL + u.

Search for a word

While working with a text editor, you often require to search for a specific word. Nano also allows you to accomplish this task easily. To do this, you can use CTRL + w key combination.

The pressed combination with search the entire file content for specified word and show you the same if existing in the file. For example, We are searching for a word ‘template’ (existing word) within servo.txt, and it will point to the first occurrence of word.

search for a word

search wrapped the word

However, if your preferred word is not found, it will display “searched_word not found”. For example, we have searched for ‘keyboard’ within servo.txt, and here comes the output.

word not found

Spell check in nano

Spell check is a term which is often used in text editors to check for incorrect spelling of words while working with text editor. By default, nano text editor lacks this feature, however the users can enable this according to their requirements. To accomplish this task, you first need to install a Linux app named spell. To install it, run the following command:

sudo apt install spell

install spell in linux

Running the command will ask you feed your password to start the installation, and will take a few moments to complete.

To do spell check and replacement using spell, you can press Ctrl + t. this will ask you to replace the incorrect word with right word, so just feed the word and press Enter key.

Save and Exit

Here comes the key combination which can be used for saving and exiting the file editor according to your needs.

Ctrl + s : Used for saving the file as we use normally with other text editors.
Ctrl + o : It actually also saves the file but works as ‘Save as’ and will ask you to a new file name. You can simply keep the same file name and press Enter to save it.
Ctrl + x : This key combination is used to exit the text editor, however, in case if there’s some unsaved changes, it will ask you to consider, otherwise exit nano.

Basic use of nano for editing files in Linux

In the above sections, we have discussed in detail usage of nano file editor, however the Linux users often prefer to use nano to edit their certain files and exit. And this is quite simple as well. All it needs to follow three steps below mentioned:

Step 1: Run the terminal and use nano File_name to open a file.
Step 2: Edit your file according to your needs, and press Ctrl + s to save the edits.
Step 3: Press Ctrl + x key combination to close the editor.

Final Thoughts

Nano is quite useful and simple text editor in compare to other options for a Linux users. We have discussed how you can use this text editor on your Linux device to open, edit, save and do even more with nano. Hope this article helped you learning how to use nano command line text editor easily. Do share this tutorial with your friends. If you have any question, you can click to Visit Our Discussion Board.