Enter your email address below and subscribe to our newsletter

How to Use Nano Text Editor in Linux

Learn all the basic to get started with the nano editor staring from creating a new file to saving changes.

Share your love

Nano is a simple yet powerful command-line text editor that comes pre-installed on most Linux distributions. It offers an intuitive interface and a wide range of features, making it an excellent choice for both beginners and experienced users.

In this comprehensive guide, we’ll explore how to use the Nano text editor effectively in Linux.

Basic shortcuts for nano

Nano being a terminal-based editor, offers a variety of shortcuts here are some important shortcuts which will help you kick-start the editing process with Nano:

ShortcutDescription
Ctrl + GDisplay help text.
Ctrl + OSave the current file.
Ctrl + XExit Nano.
Ctrl + WSearch for a string.
Ctrl + KCut the current line.
Ctrl + UPaste the cut text.
Ctrl + JJustify the current paragraph.
Ctrl + CDisplay the current cursor position/
Ctrl + _Go to a specific line and column.
Alt + ASelect text.
Alt + 6Copy the selected text.
Alt + UUndo the last action.
Alt + ERedo the last undone action.
Nano editor basic shortcuts

I’ve only mentioned the basic shortcuts in the table. If you want to know more shortcuts, open the nano editor by typing nano in the terminal and press Ctrl + G. It will open the help section where you can find more shortcuts:

Shortcuts in the nano editor

Now, let’s take a look at some basics of using the nano editor.

How to create and open files

To open an existing file with the nano editor, all you have to do is append the filename to the nano command followed by space as shown here:

nano Filename

If I want to open the Hello.txt file, then I will use the following:

nano Hello.txt
Open files using the nano editor

If the file is located in a different directory, then you need to append the path to the file to the nano command:

nano /path/to/file

But how do you create files? The process is the same. When you try opening a file using the nano editor if it does not find any file with the given filename, then it will create a new file with the given filename.

For example, if I want to create a file named Kabir.txt, then all I have to do is execute the following command:

nano Kabir.txt
Create new file using the nano editor

How to copy and paste

As there’s no way to delete lines in the nano editor, you can refer to any of the two methods to cut lines to have similar effect to deleting lines.

There are two ways you can copy and paste into the nano editor:

  • Using mouse (easy)
  • Using keyboard shortcut

1. Copy and paste using a mouse

If you’re new to the terminal, then using keyboard shortcuts might be overwhelming for you and in that case, you can use a mouse to simplify the process.

To copy and paste, all you have to do is follow two simple steps:

  1. Select text using the mouse
    Select text
  2. Right-click and select the copy option. Alternatively, you can press Ctrl + Shift + C to copy selected text using the mouse.
    Copy text using mouse in nano editor
  3. Use your keyboard’s arrow keys to move the cursor to where you want to paste text.
  4. Right-click and select the paste option. Alternatively, you can press Ctrl + Shift + V to paste copied text.
    Paste data in the nano editor

Looks confusing? Here’s a simple animation explaining all the steps at once:

Copy and paste text using mouse in the nano editor

2. Copy and paste using the keyboard

There’s one limitation of using a mouse to copy and paste in the nano editor – you can not copy anything beyond the visible screen. This means you can not select lines which are below the visible lines.

This method allows you to mark a point from where you can select text and use the nano shortcuts to copy and paste text. Here are the steps that you need to follow:

  1. Use the arrow keys from where you want to start the selection and press Alt + A to set a marker.
  2. Now, use the arrow keys to select text.
  3. Press Alt + 6 to copy the selected area
  4. Navigate to a line where you want to paste the text and then, press Ctrl + U to paste the copied text.

Sounds confusing? Here’s how to do it:

Copy and paste in nano using keyboard shortcut

How to cut and paste

Like copying and pasting text, there are two ways you can cut and paste text in the nano editor:

  • Cut and paste the entire line
  • Cut and paste selected text

Both methods use keyboard shortcuts as you can not cut text using the mouse.

1. Cut and paste the entire line

I personally use this method as I often cut multiple lines from files and it is quite easy compared to the other (I’m about to show you in a moment). To cut and paste the entire line, you’d have to follow the given steps:

  • Use arrow keys to navigate to the line which you want to cut and press Ctrl + K to cut the line.
  • Again use the arrow keys to navigate to where you want to paste the line and press Ctrl + U.

Sounds complex? Here’s how you do it:

How to cut and paste entie lines

2. Cut and paste specific part

The previous method is only useful when you want to cut the entire line but what if you want to cut a specific part of the file? Well, in GUI editors, you can select the portion using a mouse and cut it but here, you have to use keyboard shortcuts.

To cut and paste a specific portion of the file, refer to the following steps:

  1. Navigate to the part of the file from where you want to select the text and press Alt + A to set a marker.
    Set mark to cut text
  2. Now, use the arrow keys to select text.
    Select text to cut in nano
  3. Press the Ctrl + K to cut the selected text.
  4. Navigate to the line where you want to paste the text and press Ctrl + U to paste the text.
    Paste text in nano

In case, you are still confused, here’s how I did it:

Cut and paste specific part of the file using the nano editor

How to undo and redo

While editing, undoing and redoing are some of the most frequent actions a user will perform. Like any text editor, Nano also supports undoing and redoing actions.

To undo and redo in nano, you will have to use the given shortcuts:

ActionLinux/Unix ShortcutmacOS Shortcut
UndoAlt + UOption + U
RedoAlt + EOption + E
Shortcuts to undo and redo in nano

When you undo the operation, it will notify you saying “Undid <operation>” so if you undid the deletion, then it will read like “Undid deletion” as shown here:

Undo in the nano editor

When you redo the operation, it will notify you saying “Redid <operation>”. For example, if you deleted text and then redid the action, then, it will show “Redid deletion” as shown here:

Redo operation in the nano editor

How to search and replace

If you want to search and replace in the nano editor, it can be done in three easy steps:

  • Press Ctrl + \ or Alt + r to launch the search and replace prompt.
  • Enter the term you want to search and then press the Enter key.
  • Here you will have two choices: either press A to replace all the found results or keep on pressing N key till you find the target match and press Y to replace it.

Here, I will show you how you can replace all at once and replace selective terms.

1. Search and replace selective terms

When you search for a term, it will highlight all the matching results and you might not want to replace all the results. In that case, you can replace specific terms using the shown steps:

  1. First, press Ctrl + \ or Alt + r to launch the search and replace prompt and enter the term you want to search:
    Enter the search term
  2. Enter the term you want to replace with:
    Enter the term to replace with
  3. It will show you multiple search results. To ignore the highlighted search result, press N:
    Keep on pressing N key till you find the target term to replace with
  4. Once you find the target keyword, press Y for one or more results and it will replace the keywords (I replaced two keywords here):
    Replace selective terms in nano

Still, looks confusing? Here’s a simple animation showing all the steps I performed to replace two terms:

How to search and replace selective terms in nano
Search and replace selective terms in nano

2. Search and replace all the results

To search and replace all the terms in the file, all you have to do is refer to the following steps:

  1. Press Ctrl + \ or Alt + r to launch the search and replace prompt and enter the term you want to search (and later be replaced):
    Enter the search term
  2. Enter the term you want to replace with:
    Enter the term to replace with
  3. Press the A key to replace all the search results at once. In my case, it replaced all 4 terms at once:
    Search and replace all terms at once in nano

Here’s a summary of all 3 steps in the form of animation:

Search and replace all the terms at once in the nano editor

How to save and exit

After making changes to the file, the last step is to save changes and exit from the nano editor. And trust me, it is relatively easy compared to doing the same on Vim.

To save and exit from the nano editor, follow the given steps:

  • Press Ctrl+s to save changes in the current file
  • Press Ctrl + O and then press the Enter key (also allows you to save files with different names)
  • Press Ctrl + X to exit from the nano editor.

Here’s a detailed explanation of how you can save and exit from the nano editor.

1. Save the file in the nano editor (without exiting)

The easiest way to save file in the nano editor is to press Ctrl + s as it won’t prompt you for any options or confirmation and write changes to the current file.

To save the file, press Ctrl + O and it will ask you to verify the filename as you can save changes by the different filenames also. If you just want to save the file, then verify the filename and hit the Enter key:

Save changes in the nano editor

That’s it! Once you save changes, it will show you the number of lines written to the file:

Number of lines written to the file in nano

2. Exit from the nano editor

To quit the nano editor, press Ctrl + X key.

If you didn’t save the changes you made, it will prompt you to ask if you want to save changes before exiting the nano editor. Press Y to save changes and N to not to:

Exit from the nano edior

If you press Y, then it will ask you to enter the filename to write changes. Simply press the Enter key and it will save changes.

That’s it!

Conclusion

In this tutorial, I went through all the basics you need to get started with the nano editor starting from crating to saving and exiting the nano editor.

I hope you will find this guide helpful and if you have any queries, leave us a comment.

Share your love
Kabir
Kabir

A tech journalist whose life revolves around networks.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!