How to use and install Nano Text Editor on Linux
You will require a command-line environment in order to create or modify text files. For Unix and Linux operating systems, Nano is a popular text editor. Nano is a strong piece of software that allows you to modify and generate a variety of files.
For those seeking simplicity, Nano is an excellent option. It has all of the fundamental capabilities of other text editors, such as UTF-8 encoding, syntax highlighting, search and replace using regular expressions, multiple buffers, and spellchecking.
How to install Nano Text Editor on Linux
Nano is a text editor that comes pre-installed on macOS and most Linux variants. Type the following to see whether it’s installed:
Nano -- version
As a result, you will see something like this:
GNU nano, version 2.9.3
(C) 1999-2011, 2013-2018 Free Software Foundation, Inc.
(C) 2014-2018 the contributors to nano
Email: [email protected] Web: https://nano-editor.org/
Alternatively, the process can be carried out in two stages if necessary.
- Start by updating the apt repositories using the following command:
sudo apt update
- After that, install Nano using these commands:
Debian/Ubuntu:
sudo apt install nano
CentOS/Fedora:
yum install nano
The text editor has been successfully installed.
Keep in mind that if you have specific files in each directory and you do not want your virtual server to be attacked repeatedly, changing the Linux VPS password once a month is a priority.
How to Use Nano Text Editor on Linux
Create and open files
To create or open a file, type nano followed by the filename.
nano filename
Make sure you’re in the right directory if you wish to make changes to an existing file. You may also enter the entire path of the file you’re looking for. In order to open a file, it must first have read rights.
nano /path/to/filename
You can write or edit it in the new editor window that opens. The cursor can be moved around the text with the arrow keys on your keyboard. A few shortcuts can be found at the bottom of this window. The ^ symbol means you have to press CTRL + [Key] (CMD + [Key] for Mac users) to execute the command you have chosen.
The file can be saved and edited by pressing CTRL + O. Typing Ctrl + G will also display a list of all commands.
You can exit the editor by pressing CTRL + X. It will ask you whether to save any changes you have made to the file currently open, otherwise it will end the session. Enter Y to indicate yes or N to indicate no, and then press ENTER.
Edit files
Nano is a modeless editor, so you can start editing as soon as you open the file. Press ALT + A at the beginning of the text to select it. By doing this, the text will be marked for selection, and you can then move the cursor over it with the arrow keys.
The selected text can be copied by pressing ALT + 6. The text will be copied to the clipboard. ALT + K can be used to cut text. CTRL + U is the shortcut for pasting text. It will copy or cut the entire line if there is no text selected before copying/pasting.
The selected text can be copied by pressing ALT + 6. The text will be copied to the clipboard. ALT + K can be used to cut text. CTRL + U is the shortcut for pasting text. It will copy or cut the entire line if there is no text selected before copying/pasting.
How to Save File in Nano Editor
After edits, using the Ctrl+O (^O) keyboard combination allows you to save any modifications in the document.
Subsequently, you will be prompted to confirm the file name and apply your recent changes.
By pressing the Enter key, if you are saving for the first time, you can specify and verify the required file name. You can also overwrite the existing file by confirming the displayed file name. Also, At this step, you can change the current file name.
In the modified file buffer, the nano text editor displays an asterisk (*) next to the file name to indicate changes. This helps you recall the modifications you have made to the file and have not yet saved.
How to Exit From Nano editor
After saving the file, to exit Nano, you must press the combination keyboard Ctrl+X, then quit Nano editor and return to the terminal.
If you press the Ctrl+X (^X) keyboard combination without saving the modifications, Nano will ask whether you want to save your changes before exiting. To confirm and save, press the Y key, or to exit without saving, press N, and to cancel the exit operation, press Ctrl+C.
Hence, you can rest assured that your modifications will be saved if you accidentally exit the editor.
Note: You must have the appropriate permissions to edit and save files in Nano.
Select, Copy, Cut, and Paste Text
You can select parts of a file by navigating to the beginning of it, pressing Alt+A (M-A), and moving it over using the arrow keys. This will highlight the selection. Press Ctrl+6 to cancel the selection.
Copy the selected text using Alt+6 or the combination (M-6) and cut it with Ctrl+K (^K). The entire line of text will be copied or cut if you don’t select any text prior to using these shortcuts.
Press Ctrl+u when you want to paste the text where you want it.
Replacing and searching text
Use the “where-is” feature of the editor with Ctrl+W (^W) to find a specific word or part of a text. Enter the text you are looking for in the search box. Use Alt+W (M-W) to move on to the next result.
Searching by line number is also possible. You can then find a particular line by pressing Ctrl+T (^T) while in it.
A regex (regular expression) search is also possible. Regex search patterns are defined by character sequences. You can do this by pressing Alt+R (M-R) on your keyboard.
Use CTRL+W and CTRL+R to find and replace text. Type the text you want to find and the text you want to replace it with. Once the text has been removed, you will be taken to the first instance of the text and asked whether you would like to replace the entire instance or just the first instance.
Navigation Shortcuts
Navigating through a large file quickly is important when working with a large file. Using the arrow keys or keyboard shortcuts is one way to do this in Nano.
You can navigate using the following keyboard shortcuts:
- Ctrl+F (^F) to move one character forward
- Ctrl+B (^B) to move one character back
- Ctrl+Space (^Space) to move one word forward
- Alt+Space (M-Space) to move one word back
- Ctrl+P (^P) to move to the previous line
- Ctrl+N (^N) to move to the next line
- Ctrl+V (^V) to move to the next page
- Ctrl+Y (^Y) to return to the previous page
- Ctrl+A (^A) to move to the beginning of the line
- Ctrl+E (^E) to move to the end of the line
Nano Spell Check
Nano’s spell-checking feature is also available. This feature requires the spell package to be installed.
Run the following command in the terminal to install the spell package:
sudo apt install spell
Press Ctrl+T (^T) to spell check in the text editor after installing the package. A misspelled word will be highlighted and asked to be replaced.
Make Nano your default text editor
Vi is the default text editor for most Linux systems. Changing VISUAL and EDITOR environment variables will make nano the default text editor.
For users of the Bash shell, variables can be exported in the ~/.bashrc file:
export VISUAL=nano
export EDITOR="$VISUAL"
FAQ
How do I make a .nanorc file that will be read when I start nano?
copy the sample.nanorc from the doc/ directory in the nano source package (or from /usr/doc/nano on your system) to .nanorc in your home directory, and then edit it. If you didn't get a sample nanorc, the syntax of the file is simple: features are turned on and off by using the words set and unset followed by the long option name of the feature (see man nanorc for the full list of options). For example, "set quickblank" or "set smarthome". Of course, for this to work, your nano must not have been compiled with --disable-nanorc.
Conclusion
It’s now time for you to install Nano on Linux! It was important for you to learn how to create, edit, and save files using basic text commands.
For additional commands, you can always use Ctrl+G (^G) to access the Help text.