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

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

Linux Commands

Walid Al Asad

March 1, 2023

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.

Table of Contents Expand

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 represents 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 thisThe “nano” Command in Linux [13 Practical Examples] (2)

➋ 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.The “nano” Command in Linux [13 Practical Examples] (3)

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.Download this file to work with the “nano” command in Linux.

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.The “nano” Command in Linux [13 Practical Examples] (4)

Similar Readings

  • The “grep” Command in Linux [10+ Practical Examples]
  • The “wc” Command in Linux [15 Practical Examples]
  • The “sort” Command in Linux [16 Practical Examples]

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.The “nano” Command in Linux [13 Practical Examples] (5)

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.The “nano” Command in Linux [13 Practical Examples] (6)If I press ALT + → (right arrow) I will move to the second tab where the welcome file is.The “nano” Command in Linux [13 Practical Examples] (7)

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:The “nano” Command in Linux [13 Practical Examples] (8)

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.The “nano” Command in Linux [13 Practical Examples] (9)

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

Similar Readings

  • The “cut” Command in Linux [8 Practical Examples]
  • The “jed” Command in Linux [8 Practical Examples]

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.The “nano” Command in Linux [13 Practical Examples] (10)And 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:The “nano” Command in Linux [13 Practical Examples] (11)

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.The “nano” Command in Linux [13 Practical Examples] (12)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.The “nano” Command in Linux [13 Practical Examples] (13)After pressing ENTER, the output will be as below. You can see the cursor is at the beginning of the word cat.The “nano” Command in Linux [13 Practical Examples] (14)Now, 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 thisThe “nano” Command in Linux [13 Practical Examples] (15)To 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.The “nano” Command in Linux [13 Practical Examples] (16)

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.The “nano” Command in Linux [13 Practical Examples] (17)To 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.The “nano” Command in Linux [13 Practical Examples] (18)

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.The “nano” Command in Linux [13 Practical Examples] (19)

Similar Readings

  • The “vi” Command in Linux [6 Practical Examples]
  • The “vim” Command in Linux [8 Practical Examples]
  • The “egrep” Command in Linux [10+ Practical Examples]

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.The “nano” Command in Linux [13 Practical Examples] (20)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.The “nano” Command in Linux [13 Practical Examples] (21)Here, I typed the word bat as it is the new word.The “nano” Command in Linux [13 Practical Examples] (22)Finally, I confirmed that I only want to replace this word, not all.The “nano” Command in Linux [13 Practical Examples] (23)Now, as you can see, the word CAT is replaced with the word bat.The “nano” Command in Linux [13 Practical Examples] (24)

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.The “nano” Command in Linux [13 Practical Examples] (25)As you can see, there is another line exported from the file welcome.The “nano” Command in Linux [13 Practical Examples] (26)

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.The “nano” Command in Linux [13 Practical Examples] (27)Now 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.The “nano” Command in Linux [13 Practical Examples] (28)And this image shows the output after redoing. You see a notification here as well.The “nano” Command in Linux [13 Practical Examples] (29)

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 autoindentset casesensitiveset historylogSet linenumbersset mouseset titlecolor red,blue

❹ Save the file and exit

Output:

I have also attached an image for you.The “nano” Command in Linux [13 Practical Examples] (30)Now see, some changes are apparent already.The “nano” Command in Linux [13 Practical Examples] (31)

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

  • The “paste” Command in Linux [6 Practical Examples]
  • The “split” Command in Linux [6 Practical Examples]
  • The “sed” Command in Linux [7 Practical Examples]
  • The “tr” Command in Linux [6 Practical Examples]
  • The “uniq” Command in Linux [6 Practical Examples]

Rate this post

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated: 22/07/2023

Views: 6336

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.