The “nano” Command in Linux [13 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Nano is the simplest text editor, especially for beginners. However, some advanced features make it suitable for advanced users as well. There are other text editors like Vim and Emacs which are hard to use and it takes a lot of time to get used to them. You can access “nano” text editor using the nano command in Linux. Here I will show you some practical examples of the nano command which may help you with your day-to-day needs.

A. Description

Nano is a common tool in Unix and Unix-like operating systems. Whether you are a beginner or an advanced user, you need a text editor on daily basis. The nano command in Linux is suitable for simple tasks like creating a new file, quickly picking into a file and editing it. Features like copying, pasting, searching, replacing, and spell-checking make it super useful. Moreover, nano command in Linux can be used for a lot of types of files such as txt, html, sh, css, js, py etc.

B. Syntax

The nano command in Linux is simple to use. You can provide it with multiple options, and open multiple files. If you can put the cursor on a specific line(or column) by adding the line number(or column) with a plus(+) sign. You can also make the string case insensitive by adding “c” or “r” followed by a plus(+) sign.

nano [OPTION]... [FILE]...
nano [options] [[+line[,column]] file]...
nano [options] [[+[crCR](/|?)string] file]...
Note: In the above syntax OPTION and FILE  enclosed by square brackets and followed by 3 dots represent that multiple options and arguments can be utilized at the same time.

C. Options

There are some options available for the nano command in Linux. Even though you will do most of your tasks inside the nano text editor, those options are useful sometimes. You can check those by yourself by typing the command in your command prompt below:

man nano

Useful Options

  • -l, –linenumbers (shows line numbers on the left)
  • -m, –mouse (enables mouse support)
  • -v, –view (open a file in read-only mode)
  • -i, –autoindent (indtents a new line automatically)
  • -e, –emptyline (keeps line below title bar blank)
  • -g, –showcursor (makes the cursor visible)
  • -v, –version (shows current version number)
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Install Nano Text Editor

The nano command in Linux is a default in many distributions of Linux. However, you can install it manually as well.

➊ First check if you have nano or not

nano --version

You will get an output like thisChecking version of the nano command in Linux

➋ If NOT, type the following command in the command prompt:

sudo apt-get install nano

You will see an output like this, wait until the process is completed.Installing nano command in Ubuntu

Practical Examples of the “nano” Command in Linux

Here I will show you some practical examples of the nano command in Linux. I think it will be most effective if you practice them with me. I have also attached the files I worked with down below.

Example 1: Open Single File Using the “nano” Command in Linux

Now I will open a file using the nano command in Linux. Let’s say I have a directory named walid/folder in my home directory. Now I want to open myfile.txt from there. Here is an important note. If the file already exists, nano will just open it. If the file doesn’t exist, nano will create a file of similar name and open it. Follow the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano walid/myfolder/myfile.txt

➌ Now, press the ENTER button.

Output >

After typing the command, you will see a window like this with the cursor at the beginning.Opening a file using the nano command in Linux


Similar Readings


Example 2: Saving and Exiting

In my previous example, you learned how to open a file. Now you will learn how to save the file and exit after editing the file. Here I have used the myfile.txt file to demonstrate the procedures. Now, follow the steps below:

Steps to Follow >

➊ At first, launch the Ubuntu Terminal.

➋ Type the following command to open a file

nano walid/myfolder/myfile.txt

➌  Press ENTER key and edit it as you wish.

➍ Type the following line in your nano text editor:

Welcome to Linuxsimply

➎ Press CTRL + O.

➏ Now, press the ENTER button. The file is saved.

➐ Now to exit, press CTRL + X.

Note: You can save and exit at the same time. Type CTRL + X, then Y and now the ENTER button.

Output >

Here I wanted to save some text. After pressing CTRL + O, my output was like below.Saving a file and exiting

Example 3: Open Multiple Files Using the “nano” Command in Linux

Let’s say you need to work with multiple files and go back and forth. I will use two files hello and welcome for this example. You can open multiple by following the steps below:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

nano hello welcome

➌ Now, press the ENTER button.

Output >

You will see, the files are opened in different tabs. Press ALT + → (right arrow) to go to the right tab and ALT+ ← (left arrow) to go to the left tab. Here, the hello file is opened in the first tab.opening multiple files using the nano command in linuxIf I press ALT + → (right arrow) I will move to the second tab where the welcome file is.Switch to second file

Example 4: Getting Help

The nano text editor has a lot of features. Covering all those features is beyond the scope of this article. That’s why I am going to show how you can find and use them on your own. I will use the hello file here. When you open nano, you see a list of keyboard shortcuts like below:Shortcuts in the nano command in Linux

Note: The caret sign ^ means CTRL and M means ALT in Windows.

To know what is the task of a  shortcut, follow the steps below:

Steps to Follow >

➊ At first open an Ubuntu Terminal by pressing CTRL + ALT + T.

➋ Type the following command in your command prompt:

nano hello

➌ Now press CTRL + G

Output >

You will see a long text like this where every shortcut is explained. To search for anything here, press CTRL + W.Details of all the shortcuts

Note: To search on the page press CTRL + W.


Similar Readings


Example 5: Copy, Cut and Paste in the “nano” Command in Linux

It is possible to copy, cut and paste in the nano command in Linux. Here I will use the file hello here to show how a copy (or cut) a line and paste it.Showing the second line which is emptyAnd I want to copy the first line to the second line. I can do that by doing the following:

Steps to Follow >

➊ Af first open your Ubuntu terminal

➋ Type the following command in your command prompt:

nano hello

➌ Mark text using the mouse pointer or SHIFT + Arrow keys.

To copy press ALT + 6, To cut press CTRL + K.

➎ Take your cursor where you want to paste.

➏ Press CTRL + K to paste.

Output >

I followed the steps above and copied the first line to the second line. The output is like the below:First line is copied line to second line using the nano command in Linux

Example 6: Search a Word

It is possible to search for a particular thing in the nano command in Linux. I have a file named file which looks like the one below. Now I want to search for the word cat.Showing the words which will be searched using the nano command in Linux Now follow the steps below:

Steps to Follow >

➊ At first open an Ubuntu terminal

➋ Type the following command in the command prompt:

nano file

➌ Press CTRL + W to search

➍ Include extra feature if you want ( press ALT + C for case-sensitive search, CTRL + T for search by line and column)

➎ Type what you want to search. Here I typed the following word:

cat

➏ Then press ENTER

Output >

After pressing CTRL + W, the window will be like below. Here I searched cat. Also, I could have searched using those extra features.Window after pressing CTRL + WAfter pressing ENTER, the output will be as below. You can see the cursor is at the beginning of the word cat.Result after searching using the nano command in linuxNow, if I search “cat” again, the cursor will be on the word CAT as our search was case-insensitive.

Example 7: Enable the Spell Checker Feature in the “nano” Command in Linux

If the spell checker isn’t enabled you will get an error message like thisMessage if spell checker is enabledTo solve this follow the steps below:

Steps to Follow >

➊ At first launch the Ubuntu Terminal

➋ Type the following command in the command prompt:

sudo nano /etc/nanorc

➌ Provide password (if necessary)

➍ Now find the line # set speller “aspell -x -c” and remove the hash “#” symbol from the front.

➎ Then save the file and exit.

Output >

I have attached an image that may help you. You just need to remove the hash “#” symbol. This will enable the spell checker option.Showing the line which is needed to be changed to enable the spell checker

Example 8: Spell Check in the “nano” Command in Linux

Spell checker is a handy tool. Nowadays most of the text has this feature. The nano command in Linux comes with this feature as well.  Let’s say there are some lines in the file below and I want to check the spelling. I used the file word here.Showing words whose spelling will be checked using the nano command in LinuxTo do that, I will follow the steps below:

Steps to Follow >

➊ Open an Ubuntu terminal

➋ Type the following command in your command prompt:

nano word

➌ Press the key F12

➍ For correction, type the number of the correct word

➎ To abort, type b

Output >

In my case, it is showing a list of suggestions for the word haman, and the current world will be human which is at 2. So I will type number 2 and the word will be corrected.Correcting the spelling

Example 9: Open a File on A Specific Line and Column

I can go directly on a particular line and a particular column using the nano command in Linux. I will use a file named file for this example. Follow the steps below:

Steps to Follow >

➊ At first press CTRL + ALT + T to open an Ubuntu terminal

➋ Type the following command in the command prompt:

nano +2,3 file
Note: The first number indicates the line number and the second one indicates the column number.

➌ Now press ENTER.

Output >

As you can see, the cursor is on the second line and column three.Going directly on a line and a particular column using the nano command in Linux


Similar Readings


Example 10: Search and Replace in the “nano” Command in Linux

Searching and Replacing are helpful for any type of user. Good thing nano is also capable of doing those things. For instance, I want to replace the word CAT with bat in the file named file. First I will do a case-sensitive search and then replace the word.Showing the word which will be replaced You can do this with me by following the steps below:

Steps to Follow >

➊ At first open a file in the Ubuntu terminal

➋ Type the following command in the command prompt:

nano file

➌ Press CTRL + \

➍ Then press ALT + C to enable case-sensitive search (this step is optional)

➎ Now type a new word and press ENTER.

➏ If you want to replace a single word type Y or for all the words type A.

Output >

Here you can see, I did a case-sensitive search.Case-sensitive search and typing the word which will be replacedHere, I typed the word bat as it is the new word.Typing new wordFinally, I confirmed that I only want to replace this word, not all.Confirming whether you want to replace a single word or all wordsNow, as you can see, the word CAT is replaced with the word bat.Replace new word using the nano command in Linux

Example 11: Insert Another File

Now I want to copy some text in a file from another file. For this example, I will open a file named hello in my terminal and import text from another file named welcome. You can also practice with me by following the steps below:

Steps to Follow >

➊ At first open your Ubuntu terminal

➋ Type the following command in the command prompt:

nano hello

➌ Adjust the cursor

➍ Press CTRL + R

➎ Now enter the file name from which you want to export

Output >

Here, I opened the file hello at the beginning. Then followed step 4 and entered the other filename.Inserting new file in an existing fileAs you can see, there is another line exported from the file welcome.A new line is inserting from another file using the nano command in Linux

Example 12: Undo and Redo

Undoing and redoing is one of the most useful tools in typing. Everyone uses them. Let’s say I made a mistake while typing, like below. I used the file named file here.Showing mistakes while typingNow I will show how I can use undoing and redoing features of the nano command in Linux here. Follow the steps below:

Steps to Follow >

➊ At first press CTRL + ALT + T to open an Ubuntu terminal

➋ Type the following command in your command prompt:

nano hello

➌ For undoing press ALT  + U

➍ For redoing press ALT + E

Output >

This image shows the output of undoing. You will also see a notification on the screen like below.Undoing a taskAnd this image shows the output after redoing. You see a notification here as well.Redoing a task in the nano command in Linux

Example 13: Customization of the “nano” Command in Linux

The nano command in Linux is highly customizable. A lot of features aren’t enabled by default. To see the features and enable them, you can always visit the file /etc/nanorc. However, to edit this file, you will need sudo privileges. In this example, I will create a .nanorc file in my home directory and set the following features there

  • autoindent – it will automatically indent your text
  • casesensitive – it will enable case-insensitive search by default
  • historylog – it will keep track of every change
  • linenumbers – it will show line number on the left
  • mouse – it will enable certain mouse actions
  • titlecolor – it will set the title color

Steps to Follow >

➊ Open a terminal in your Ubuntu

➋ Type the following command:

nano ~/.nanorc

➌ Now add the following lines:

set autoindent

set casesensitive

set historylog

Set linenumbers

set mouse

set titlecolor red,blue

➍ Save the file and exit

Output >

I have also attached an image for you.Customizing the nano command in LinuxNow see, some changes are apparent already.Nano text editor after customization

Conclusion

Having some basic skills on a text editor is a must for any type of Linux user. And the nano command in Linux is the most easier text editor with a lot of useful features. Hopefully, the example above will help you master the nano command.


Similar Readings

4.3/5 - (6 votes)
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment