How to Use the Nano Text Editor in Linux

Nano is a command-line text editor that allows you to open and manipulate files in Linux. It includes a user-friendly interface with a function-based navigation menu with keyboard shortcuts to modify files. Nano provides essential editing capabilities, making it ideal for editing all types of files on Linux.
This article explains how to use the Nano text editor in Linux and use it to create, open, and modify files.
Prerequisites
Before you begin, you need to:
- Have access to a Linux instance as a non-root user with sudo privileges.
Install Nano
Nano is pre-installed and available on most Linux distributions by default. Follow the steps below to install it if it's unavailable on your workstation.
Follow the steps below to install Nano on Debian-based distributions.
Update the
apt
package index.console$ sudo apt update
Install Nano.
console$ sudo apt install nano -y
Follow the steps below to install Nano on RHEL distributions.
Install Nano on RHEL.
console$ sudo dnf install nano
Follow the steps below to install Nano on FreeBSD.
Update the
pkg
package index.console$ sudo pkg update
Install Nano on FreeBSD.
console$ sudo pkg install nano
Follow the steps below to install Nano on OpenBSD.
Update the
pkg
package index.console$ sudo pkg_add -u
Install Nano on OpenBSD.
console$ sudo pkg_add nano
Follow the steps below to install Nano on Alpine Linux.
Update the
apk
package index.console$ sudo apk update
Install Nano on Alpine Linux.
console$ sudo apk add --upgrade nano
Follow the steps below to install Nano on Arch Linux.
Install Nano on Arch Linux.
console$ sudo pacman -Syu nano
Follow the steps below to install Nano on openSUSE.
Install Nano on openSUSE.
console$ sudo zypper install nano
Open the Nano Text Editor
Follow the steps below to open the Nano text editor in Linux.
Enter
nano
in your terminal session to open the Nano text editor.console$ nano
The Nano Text editor should open in your session similar to the following graphic.
The top bar displays the Nano version and the filename. If it shows
New Buffer
, it means no file is open while the bottom section lists commonly used shortcut keys.
Nano Keyboard Shortcuts
Nano uses the special key notation standard for keyboard shortcuts.
^
represents the Ctrl key.M
represents the Alt (Meta) key.
For example:
^G
means Ctrl + G (opens the help menu).M-A
means Alt + A.
Press Ctrl + G to access the Nano help menu.
You can scroll through the help menu to view all available options. This menu provides a list of commands to navigate, edit, search, and customize the Nano CLI editor. Use the Up and Down arrow keys to explore more commands. Press Ctrl + X to exit the help menu and return to the editor.
Create, Edit, and Save Files Using Nano
Follow the steps below to create, edit, and save files using the Nano text editor on Linux.
Enter the
nano
command followed by the filename to create or edit the file.console$ nano test.text
If the file does not exist, Nano creates it and opens it for editing. You can start typing directly in the editor.
To save a file in Nano, press Ctrl + O and confirm the filename when prompted.
Press Enter to confirm. You can continue editing and press Ctrl + O again to save changes.
To open system files using Nano, use the
sudo
command with the following syntax.console$ sudo nano /<systempath>/filename.txt
For example, run the following command to open the
/etc/hosts
file.console$ sudo nano /etc/hosts
Search and Replace Text Using Nano
Follow the sections below to search or replace text using the Nano text editor.
Search for Text
Press Ctrl + W and enter a search term when prompted.
Press Enter and verify that the cursor moves to the beginning of the first match.
Press Ctrl + W to find the next match and press Enter.
Replace Text
Press Alt + R to define the text to replace.
Enter a search term to replace.
Press Enter to define the replacement text.
Enter the replacement text and press Enter.
Verify that the Nano text editor highlights the first instance of the text and choose any of the following options to apply.
- Y - Yes (Replace this instance)
- N - No (Skip this instance)
- A - All (Replace all instances)
- ^C - Cancel ( ^ refers to the Ctrl key)
Cut, Copy, and Paste Text Using Nano
Follow the steps below to cut, copy, and paste text using the Nano text editor in Linux.
To copy text, press Ctrl + 6 to set a mark on a target line to copy.
Use the Left and Right arrow keys to highlight the text to copy.
Press Alt + 6 to copy the selected text.
To cut text, press Ctrl + 6 to mark the target line with text to cut.
Use the Left and Right arrow keys to select the text to cut.
Press Ctrl + K to cut the text.
To paste text, move your cursor to your desired location to paste the text.
Press Ctrl + U to paste the text.
Delete Text Using Nano
Follow the steps below to delete characters, words, and entire lines in a file using Nano.
- Press Ctrl + H to delete a character before the cursor.
- Press Ctrl + D to delete a character after the cursor.
- Press Alt + Backspace to delete a word to the left.
- Press Ctrl + Delete to delete a word to the right.
- Press Alt + Delete to delete an entire line.
- To delete multiple lines, select them using Ctrl + 6, then press Alt + Delete.
Undo and Redo Changes Using Nano
Follow the steps below to undo and redo changes in Nano.
- Press Alt + U to undo the last action.
- Press Alt + E to redo the last undone action.
Customize the Nano Text Editor
In this section, you will learn how to configure Nano to enhance your editing experience. Nano reads its configuration parameters from the system-wide file /etc/nanorc
and from user-specific files ~/.config/nano/nanorc
and ~/.nanorc
. Options specified in the user files take precedence over the global options.
Configure Nano Locally
Create the
.nanorc
file in your home folder.console$ nano ~/.nanorc
Add the following lines to enable auto-indentation and line numbers.
iniset autoindent set linenumbers
Save and close the file.
Verify that the text and line numbers are indented when you open Nano.
console$ nano
Configure Nano Globally
Open the
/etc/nanorc
file to apply system-wide changes.console$ sudo nano /etc/nanorc
Uncomment directives in the file by removing the
#
symbol to enable a feature.Save and close the file.
All changes to the global
nanorc
configuration apply to all users on your system. Open the Nano text editor and verify that the new features are available.
Exit Nano
Follow the steps below when exiting the Nano text editor.
- Press Ctrl + X to exit the Nano text editor. If there are unsaved changes, you will receive a
Save modified buffer
prompt to verify the changes. Then:- Press Y to save.
- Press N to discard changes.
- Press Enter to apply the file changes.
Conclusion
You have used the Nano text editor in Linux to create, edit, search, replace, cut, copy, delete, and configure files. Nano allows you to open and manipulate files in Linux, allowing you to modify configurations, files, and scripts on your workstation. For more information and configuration options, visit the Nano Documentaiton