How to Install and Use Nano Text Editor in Linux

Last updated: March 13, 2024 | Linuxopsys

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:

nano --version
check nano version

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

On Fedora:

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 

For example:

nano new_file
nano open a file

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.

create a 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 ~/.nanorc file:

set linenumbers

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.

nano save and exit

Basic Nano Operations

Let check some of the common operations of nano editor such as cut, copy and paste.

Delete Lines

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 Ctrl+Shift+6 or 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 Ctrl+u.

Copy

First, select the text that you want to copy.

Then, to copy the selected text block, press the ‘Alt+6’ command.

Search

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 Alt+W.

For backward search press Ctrl+Q and for checking the next occurrence backward press Alt+Q.

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. 

Press Y or N and hit Enter to move towards the next match. Press A from the keyboard to replace all matches.

Undo

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:

Commands Functions
Ctrl+OWrite to file or save the file as
Ctrl+SSave the current file
Alt+ASelect text, move the cursor to select a block of text
Ctrl+KCut the current line or selected text
Alt+6Copy current line or selected text to clipboard
Ctrl+UPaste the content of the clipboard
Alt+UUndo the action
Alt+ERedo the undone action
Ctrl+WForward string search
Ctrl+QBackward string search
Alt+WCheck next occurrence of string forward
Alt+QCheck next occurrence of string backward
Alt+GPrompt to jump to a specific line number
Ctrl+C Display the current line number, column number, character position
Alt+NToggle line numbers
Ctrl+FTo move the cursor one character forward
Ctrl+BMove cursor one character back 
Ctrl+NMove the cursor to the next line
Ctrl+PMove the cursor back to the previous line
Ctrl+SpaceMove cursor one word forward
Alt+SpaceMove cursor one word back
Ctrl+AMove the cursor at the beginning of the line
Ctrl+EMove the cursor at the end of the line

You can Press Ctrl+G to open the help menu if you forget any nano commands.

Conclusion

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.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment