A Beginner’s Guide on How to Use Nano Text Editor in Linux

Nano is a command line text editor, that comes preinstalled in almost every Linux distribution. It is often preferred by new users because of its simplicity, compared to other command line text editors such as vi/vim and emacs. It has plenty of useful features such as syntax coloring, line numbering, easy search and many others.

Install Nano Editor in Linux

If for any reason nano is not already installed on your Linux distro, you should be able to easily install it with the following commands:

# apt install nano [For Ubuntu/Debian]
# yum install nano [For CentOS/Fedora]

Nano Keyboard Shortcuts

Nano uses keyboard combinations for different functions, such as to find text in a file, justify text etc. Those combinations are really easy and are visible while you edit your file. They change automatically depending on what action you are taking.

One thing you should know is that a keyboard shortcut represented with ^ and a symbol (for example ^W) is a combination of the Ctrl key and that symbol (Ctrl+W in our example).

A combination that is shown to start with M means that it needs to be completed by pressing Alt key and the following symbol.

Below are listed the options that you will see when you first open nano:

  • G Get Help
  • ^O Write Out
  • ^W Where Is
  • ^K Cut Text
  • ^J Justify
  • ^C Cur Pos
  • M-U Undo
  • ^X Exit
  • ^R Read File
  • ^\ Replace
  • ^U Uncut Text
  • ^T To Spell
  • ^_ Go To Line
  • M-E Redo

You don’t need to remember each option as it is always in front of you. You can get the full list of keyboard combinations by pressing ^G (or press F1) which will open nano’s help menu. You will notice that some shortcuts can be used with single key.

For example F1 key to get help or F2 to exit nano.

Nano Editor Shortcut Keys
Nano Editor Shortcut Keys

Create a New File in Nano

Creating new file is simple as running nano:

$ nano

This will open the editor and upon saving the file, it will ask you to give it a name with which the new file will be saved.

Create New File in Nano
Create New File in Nano

Open a File in Nano

To open a file you can run:

$ nano ~/my_text_file.txt

The above command will try to open the file “my_text_file.txt” from your home directory. If the file does not exist, nano will try to create it.

Sometimes, you may need to open a file and go at exact line or column. Nano allows you to do this with:

$ nano +line,columns file

For example:

$ nano +3,2 ~/.bashrc

Will open your .bashrc file and the cursor will be located on the third line, second column.

Open File in Nano
Open File in Nano

Editing Files in Nano

Upon opening or creating files you can start editing/writing immediately. Unlike vim, there is no need to switch to edit mode in nano. To move the cursor around the file, you can use the arrow keys on your keyboard.

Edit Files in Nano
Edit Files in Nano

Search for Text in Nano

You can search for text inside a file by using ^W, which represents the “where is” option. This will open a search input above the menu, where you can input the text you are search for:

Search Text in Nano
Search Text in Nano

You will also see that the bottom menu will change and will show some additional options. They are pretty much self explanatory, so we will review the more important ones.

  • Search with regular expressions – press M-R (Alt + R keys) and input your search with the regular expressions you wish to use.
  • Go to line – press ^T (Ctrl + T) followed by the line to which you want to move the cursor to.
  • Replace text – press ^R (Ctrl +T) in search mode, or ^\ in regular mode. You will be asked to enter your search, after pressing Enter, you will be asked to input the text which will be used for the replacement. Finally you will be asked if you wish to replace a matched instance of your search, or all matches. If you choose “No”, the cursor will be moved towards the next match.
  • Go to first line – press ^Y (Ctrl + Y).
  • Go to last line – press ^V (Ctrl +V).

Copy/Paste/Cut Text in Nano

Nano’s interface is very similar to a GUI text editors. If you wish to copy or cut a text in GUI editor, you will first have to select it. Same thing goes in nano. To mark a text press Ctrl + ^ then move the cursors with the arrow keys.

  • To copy the marked text press Alt + ^.
  • To cut the marked text press ^K (Ctrl +K).
  • To paste the marked text, move the cursor to a suitable position and press ^U (Ctrl + U).
Copy and Paste Text in Nano
Copy and Paste Text in Nano

Save File in Nano

If you wish to save your current changes to the file, press the ^O (Ctrl + O) combination. If you are editing a new file, you will be asked to give that file a name. This will save your current changes and nano will remain opened so you can continue making changes to the file.

Save File with Backup

Sometimes when editing a file, you may want to keep temporary copies of the same file just in case. You can use nano’s -B option, which will create backup of the file you are editing. You can use it in combination with the -C option to tell nano where to save those backups like this:

$ nano -BC ~/backups myfile.txt

The above will make backup copies of the file myfile.txt in the folder “backups” located in the user’s home directory. Note that the backup directory should be existing, otherwise, nano will tell you that the directory is invalid.

Exit Nano Editor

To exit nano, simply press ^X (Ctrl +X keys). If the file has not been previously saved, you will be asked to save the changes with yes/no or cancel the exit.

Conclusion

Nano is an easy to use command line text editor, that attracts users with its simplicity. Its interface is similar to those of GUI editors which makes it perfect for Linux newcomers.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Marin Todorov
I am a bachelor in computer science and a Linux Foundation Certified System Administrator. Currently working as a Senior Technical support in the hosting industry. In my free time I like testing new software and inline skating.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

6 thoughts on “A Beginner’s Guide on How to Use Nano Text Editor in Linux”

  1. Thanks for Your article, that said I would suggest that “nano” is NOT a suitable “text editor” for “normal Linux users” ((as should be evident by other comments here… and other “nano threads”))

    Rather nano is imo a “rudimentary code editor” that is highly targeted towards people that think that “relearning default use patterns” is worth wile for the “efficiency gains” that this can give.

    Though I believe it can be argued that there are better programs for that as well. Of course “to each their own” but in my opinion the best that can be said for nano is that “it’s there”…

    Best regards

    Reply
  2. Hi,

    In LMDE I can use shit + Arrow up/down to select text.

    In Linux mint 20 I can not shift-Arrow to mark text.

    Anyone knows a solution.

    It makes me hate Linux more and more every day, this unpredictable behaviors without any way to set defaults for cross Distro behavior.

    Best regards in advance

    Reply
  3. Hi,

    I am new to Linux and nano. I recently wanted to edit an html file for which I came across nano. However, when I use the nano command on my bash terminal and edit the file, save and exists, everything is good. But when I open it in the browser, whatever I had edited in the html file, it doesn’t reflect on my apache or my webpage. what should I do?

    Reply
  4. I love and like this post heartly.

    Sir I am a student of BS Mathematics, but I like to learn Linux and Cryptography and Black Hat Hacking.

    I am from Pakistan.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.