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.
Create a New File in Nano
Creating new file is simple as running 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.
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
$ nano +3,2 ~/.bashrc
Will open your .bashrc file and the cursor will be located on the third line, second column.
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.
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:
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).
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.
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.