GNU Nano is a simple, user-friendly, and command-line text editor that is preinstalled in most Linux distributions. It provides various features like other text editors such as multiple buffers, syntax highlighting, regular expression support, finds and replace, spell checker, and all basic functions.
Most beginners prefer to use nano as compared to vim and other command-line editors. Nano improved the friendliness of the UW Pico text editor.
In this tutorial, we learn how to install and use nano text editor on Linux.
Install Nano Editor
By default, the Nano text editor is installed in most Linux distributions. To verify the installation of nano, display the installed running nano version by using the following command in the command line tool terminal:
But, if in any case it is not installed on your Linux system then, you can easily install Nano by using the appropriate package manager of your distribution.
To install nano on Ubuntu/Debian distros, use the following command:
sudo apt install nano
Install on Redhat, CentOS Stream, AlmaLinux, Kali Linux, and Rocky Linux distributions by typing the following command:
sudo yum install nano
sudo dnf install nano
Set Nano as the default editor
The nano is usually not set as the default text editor in most of the Linux distros. The visudo and crontab by default open in vi editor. However, to use the nano as the default editor, you are required to change the Visual and Editor environment variables.
Open the ~./bashrc file using a text editor and do the following changes:
export VISUAL=nano export EDITOR="$VISUAL"
How to use Nano Editor
To use nano simply type nano from terminal followed by a new filename or an existing filename. You can also open nano without filename and later provide a filename when saving the file.
nano filename or existing_filename
The command will create a new file or open an existing file named
new_file. You can edit the text file and use the shortcuts that display at the bottom of the nano text editor to save, exit, cancel, paste, replace, and more.
The symbol ^ (caret) is the control key (Ctrl) key. Unlike Vim, nano is a modeless text editor where you don't need to enter INSERT to write text to the file.
Once you are done editing a file, you can save the file using pressing
Ctrl+O from the keyboard. If you want to make changes in the current file then press Enter key. You can also assign a new file name and press Enter key. Make sure you have proper write permissions on the file or directory to save changes.
Use Ctrl+x to exit from nano editor, if any changes are made in the file then it prompts to save Yes or No.
Nano command accepts flags command line flags. For example to set line numbers in a text, open file as:
nano --linenumbers file1.txt
Alternatively, you can add the following line in the
Nano editor save and exit
To save and exit the file from nano editor, press
Ctrl+X from the keyboard. You will be prompted to Y (Yes) to save and exit, N (No) to exit without saving, or
Ctrl+C to cancel the screen, continue editing.
Basic Nano Operations
Let check some of the common operations of nano editor such as cut, copy and paste.
To delete a single line, move the cursor to the line and press
Ctrl+K, this will delete the whole line.
To delete selected lines in the nano text editor, first mark the start of your text by pressing
Alt+A. The current cursor position will be marked set as the start position of the selection.
Now, move the cursor using arrows keys up to where you need to delete. That area will get selected. To cancel the selection press Ctrl+6. Finally, press ‘Ctrl+K’ that will delete the whole block.
Cut and paste
First, select the text you wish to cut. Press Alt+A or Ctrl+Shift+6 to mark selection as mentioned in the previous section.
Now press Ctrl+K from the keyboard to cut the text.
To paste the text, navigate into a file where you want, and then press
First, select the text that you want to copy.
Then, to copy the selected text block, press the ‘Alt+6’ command.
To search a specific string press
Ctrl+W from the keyboard. In the prompt type the string that you want to search. This performs a forward search. To move to the next result, press
For backward search press
Ctrl+Q and for checking the next occurrence backward press
To search for a specific line number, press
Ctrl+_ and then enter the line number that you want to search.
Find and Replace
If you want to find a word or line and replace it with another then, press
Ctrl+\. Type the search term that you want to find and also enter the text to be replaced with and press Enter key.
After executing the above action, the editor will move the cursor to the first match and ask you whether you want to replace this match.
N and hit Enter to move towards the next match. Press
A from the keyboard to replace all matches.
If you make some changes by mistake then, you can also undo the action by using the
Alt+U. Use the
Alt+E to redo the action.
Useful Nano Commands
Here you find some of the useful nano editor commands:
|Write to file or save the file as|
|Save the current file|
|Select text, move the cursor to select a block of text|
|Cut the current line or selected text|
|Copy current line or selected text to clipboard|
|Paste the content of the clipboard|
|Undo the action|
|Redo the undone action|
|Forward string search|
|Backward string search|
|Check next occurrence of string forward|
|Check next occurrence of string backward|
|Prompt to jump to a specific line number|
|Display the current line number, column number, character position|
|Toggle line numbers|
|To move the cursor one character forward|
|Move cursor one character back|
|Move the cursor to the next line|
|Move the cursor back to the previous line|
|Move cursor one word forward|
|Move cursor one word back|
|Move the cursor at the beginning of the line|
|Move the cursor at the end of the line|
You can Press Ctrl+G to open the help menu if you forget any nano commands.
GNU Nano text editor is a perfect editor for beginners and professionals for editing text.
In this tutorial, we learned how to install and use nano text editor in Linux. We have listed some of the useful nano commands to improve your productivity.