Newsletter Subscribe
Enter your email address below and subscribe to our newsletter
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.
Table of Contents
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:
Shortcut | Description |
---|---|
Ctrl + G | Display help text. |
Ctrl + O | Save the current file. |
Ctrl + X | Exit Nano. |
Ctrl + W | Search for a string. |
Ctrl + K | Cut the current line. |
Ctrl + U | Paste the cut text. |
Ctrl + J | Justify the current paragraph. |
Ctrl + C | Display the current cursor position/ |
Ctrl + _ | Go to a specific line and column. |
Alt + A | Select text. |
Alt + 6 | Copy the selected text. |
Alt + U | Undo the last action. |
Alt + E | Redo the last undone action. |
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:
Now, let’s take a look at some basics of using the nano editor.
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
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
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:
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:
copy
option. Alternatively, you can press Ctrl + Shift + C to copy selected text using the mouse.paste
option. Alternatively, you can press Ctrl + Shift + V
to paste copied text. Looks confusing? Here’s a simple animation explaining all the steps at once:
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:
Alt + A
to set a marker.Alt + 6
to copy the selected areaCtrl + U
to paste the copied text.Sounds confusing? Here’s how to do it:
Like copying and pasting text, there are two ways you can cut and paste text in the nano editor:
Both methods use keyboard shortcuts as you can not cut text using the mouse.
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:
Ctrl + K
to cut the line.Ctrl + U
.Sounds complex? Here’s how you do it:
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:
Alt + A
to set a marker.Ctrl + K
to cut the selected text. Ctrl + U
to paste the text.In case, you are still confused, here’s how I did it:
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:
Action | Linux/Unix Shortcut | macOS Shortcut |
---|---|---|
Undo | Alt + U | Option + U |
Redo | Alt + E | Option + E |
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:
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:
If you want to search and replace in the nano editor, it can be done in three easy steps:
Ctrl + \
or Alt + r
to launch the search and replace prompt.Enter
key.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.
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:
Ctrl + \
or Alt + r
to launch the search and replace prompt and enter the term you want to search:N
:Y
for one or more results and it will replace the keywords (I replaced two keywords here):Still, looks confusing? Here’s a simple animation showing all the steps I performed to replace two terms:
To search and replace all the terms in the file, all you have to do is refer to the following steps:
Ctrl + \
or Alt + r
to launch the search and replace prompt and enter the term you want to search (and later be replaced):A
key to replace all the search results at once. In my case, it replaced all 4 terms at once:Here’s a summary of all 3 steps in the form of animation:
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:
Ctrl+s
to save changes in the current fileCtrl + O
and then press the Enter
key (also allows you to save files with different names)Ctrl + X
to exit from the nano editor.Here’s a detailed explanation of how you can save and exit from the nano editor.
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:
That’s it! Once you save changes, it will show you the number of lines written to the file:
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:
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!
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.