Using the Nano Text Editor on Linux
Nano is a terminal-based text editor designed for editing files within Linux environments. It features an intuitive interface that incorporates a function-based menu and keyboard shortcuts for file modifications. With its basic yet powerful editing tools, Nano is well-suited for handling all kinds of file editing tasks on Linux systems.
This guide demonstrates how to work with the Nano text editor in Linux to create, open, and alter files.
Prerequisites
Before proceeding, ensure the following requirements are met:
- Access to a Linux environment using a non-root account with sudo privileges.
Installing Nano
Nano typically comes pre-installed on most Linux systems. If it’s not available on your machine, you can install it using the following steps for Debian-based systems.
Step 1: Refresh the Package Index
$ sudo apt update
Step 2: Install Nano
$ sudo apt install nano -y
Launching the Nano Editor
To open Nano, follow the steps below.
Type nano
into your terminal to launch the editor.
$ nano
Once opened, you will see the Nano interface. The top shows the version number and file name, or New Buffer if no file is open. The bottom area displays available keyboard shortcuts.
Understanding Nano Shortcuts
Nano uses a specific notation to represent keyboard combinations:
^
denotes the Ctrl keyM
stands for the Alt (Meta) key
Examples:
^G
equals Ctrl + G, which opens the help menuM-A
equals Alt + A
Press Ctrl + G
to view Nano’s help documentation. Use the arrow keys to navigate through the list of commands. Press Ctrl + X
to close the help screen and return to the editor.
Creating, Editing, and Saving Files in Nano
To create or modify a file, use the nano
command followed by the file name.
$ nano test.text
If the specified file doesn’t exist, Nano will create it and open it for editing. You can immediately begin typing content.
To save changes, press Ctrl + O
, then confirm the filename.
After confirming, hit Enter
to save. You may continue editing and press Ctrl + O
again to save additional changes.
Opening System Files
To edit system-level files, prepend the command with sudo
using the structure below:
$ sudo nano /<systempath>/filename.txt
For instance, to open the /etc/hosts
file:
$ sudo nano /etc/hosts
Searching and Replacing Text in Nano
Searching
Press Ctrl + W
, then type your search query when prompted. Press Enter
to locate the first match.
To find additional matches, repeat Ctrl + W
and press Enter
again.
Replacing
Initiate replacement by pressing Alt + R
.
Enter the text you wish to find and press Enter
.
Then enter the replacement text and confirm by pressing Enter
again.
Once Nano highlights the first instance, you can choose:
- Y: Replace current match
- N: Skip this occurrence
- A: Replace all matches
- ^C: Cancel (where
^
means Ctrl)
Cutting, Copying, and Pasting Text in Nano
Use the steps below to perform cut, copy, and paste operations in the Nano text editor on Linux.
To copy content, press Ctrl + 6
to set a marker at the desired starting line.
Use the Left and Right arrow keys to select the text you want to copy.
Press Alt + 6
to copy the marked section.
To cut content, press Ctrl + 6
at the start of the line you wish to cut.
Again, use the Left and Right arrow keys to select the portion you want to cut.
Then press Ctrl + K
to cut the highlighted text.
To paste, navigate to the desired position using the cursor.
Press Ctrl + U
to paste the copied or cut text at the cursor location.
Deleting Text in Nano
Use the following commands to delete characters, words, or full lines in Nano:
- Ctrl + H: Remove the character before the cursor
- Ctrl + D: Remove the character after the cursor
- Alt + Backspace: Delete the word to the left of the cursor
- Ctrl + Delete: Delete the word to the right
- Alt + Delete: Remove the entire line
- Ctrl + 6 followed by Alt + Delete: Select and delete multiple lines
Undoing and Redoing Changes in Nano
To reverse or reapply actions in Nano, use the commands below:
- Alt + U: Undo the most recent change
- Alt + E: Redo the previously undone action
Customizing the Nano Text Editor
This section explains how to adjust Nano’s settings to enhance usability. Nano reads settings from /etc/nanorc
for global configuration, and from ~/.config/nano/nanorc
or ~/.nanorc
for user-specific settings. User-level settings override global ones.
Local Configuration
Create a .nanorc
file in your home directory:
$ nano ~/.nanorc
Add the lines below to enable automatic indentation and display line numbers:
set autoindent
set linenumbers
Save and exit the file. When you launch Nano, you should see indentation and line numbering activated.
$ nano
Global Configuration
To set Nano options system-wide, open the /etc/nanorc
configuration file:
$ sudo nano /etc/nanorc
In this file, you can enable options by uncommenting lines (remove the #
at the beginning).
Save and close the file. Any changes you make here apply to all users on the system. Open Nano again to confirm the settings are active.
Exiting Nano
When you’re finished editing, press Ctrl + X
to leave Nano. If changes haven’t been saved, you’ll see a prompt asking if you’d like to save the modified buffer.
- Press Y to save the changes
- Press N to discard changes
- Press Enter to confirm your selection
Conclusion
You’ve learned how to use Nano in Linux to create, edit, search, replace, cut, copy, delete, and configure text files. Nano is a versatile editor that makes managing configurations, scripts, and files straightforward on a Linux system. For additional details and configuration settings, refer to the official Nano documentation.