Introduction
Nano is a simple command-line text editor commonly found in Unix-based operating systems. It allows users to quickly edit text files directly from the command line.
The editor's intuitive interface and keyboard shortcuts make it convenient for editing configuration files, scripts, and other text-based documents.
In this tutorial, learn how to install and use the Nano text editor by following simple examples.
Prerequisites
- A Linux system (this tutorial uses Ubuntu 22.04).
- sudo or root privileges.
- Access to the terminal.
How to Install Nano Text Editor
Nano is included in most Linux distributions by default. However, if it's not present on your system, install Nano using the following steps:
1. Update the apt repositories with the apt update
command:
sudo apt update
2. Install Nano with:
sudo apt install nano
Note: If you're running CentOS or Fedora, execute yum install nano
to complete this step.
How to Use Nano Text Editor
Nano is a versatile text editor primarily used to edit files directly from the command line of Unix-based systems. Its straightforward interface and keyboard shortcuts make it a convenient tool for quick text editing tasks in the terminal environment.
How to Open and Create Files
Nano allows users to create and edit text files in different ways. The following sections elaborate on how to create a new file and open an existing one.
Create a New File
There are two ways to create new text files with Nano. One way is:
nano [file-name]
Replace [file-name]
with the actual name of the file. For example, create a Nano file called text1 with:
nano text1
Another way is to open a new blank Nano file. To do so, enter:
nano
The program opens a blank page without any specified name. If changes were made, when you decide to exit the file (Ctrl+X), Nano asks whether to save or discard the file.
If you press y to save the file, Nano prompts you to name it. Type in a name and press Enter.
In this example, the name is file1.
Open an Existing File
To open an existing file, append the file name to the command. For example, if the file is called file1, the command is:
nano file1
However, to open a file from another directory, include the path in which the file is located. For example:
nano /home/documents/file1
It is also possible to open a file and directly go to a specific line or column.
nano +line,column [file-name]
For instance, go to column 2 with:
nano +2 file1
Nano Command Keyboard Shortcuts
What makes Nano so attractive is an easy graphical user interface (GUI) that allows users to interact directly with the text. There is no need to switch to an edit mode, like in Vim.
Users directly write, edit, and navigate through content using different keyboard shortcuts.
Navigating Text in Nano
In Nano, users move through text using the arrow keys. Additionally, keyboard shortcuts let you navigate through lines or pages.
Control Characters and Keys
There are two keyboard combination types for each function in Nano.
- Control shortcuts. Used with the Ctrl button. Represented by a carat (^) followed by a symbol in nano documentation.
For example, the shortcut to exit out of the Nano text editor is Ctrl+X (displayed as ^X).
- Meta shortcuts. Require the Meta key (usually the Alt button). Represented by the letter M followed by a symbol.
For example, the shortcut to Undo an action is Alt+U (displayed as M-U).
The two bottom lines in the text editor display the most commonly used shortcuts, as seen in the image below.
To see all valid shortcuts, press Ctrl+G (displayed as ^G) or F1. This opens Nano's help text and lists all possible keystrokes.
Note: Do not use the Shift button in Nano. All shortcuts use unmodified numbers and lowercase letters.
Shortcuts for Navigation
When dealing with a large file, it is helpful to know how to navigate through the text quickly. Nano allows you to do this using the arrow keys or keyboard shortcuts.
Useful keyboard shortcuts for navigating include:
- Ctrl+F (^F). Move forward one character.
- Ctrl+B (^B). Move back one character.
- Ctrl+Space (^Space). Go one word forward.
- Alt+Space (M-Space). Go one word backward.
- Ctrl+P (^P). Navigate to the previous line.
- Ctrl+N (^N). Navigate to the next line.
- Ctrl+V (^V). Go to the next page.
- Ctrl+Y (^Y). Move to the previous page.
- Ctrl+A (^A). Go to the beginning of the line.
- Ctrl+E (^E). Move to the end of the line.
Search a Text File
To search for a particular word or part of a text inside the editor, use the where is option with the Ctrl+W shortcut (^W).
This opens a search prompt to type in the text you want to find. In the following example, the search word is and.
Hit Enter. Nano highlights the matching result.
Continue to the next result with Alt+W (M-W).
Replace Text
To replace text in the file, follow these steps:
1. Open the search bar with Ctrl+W (^W) and type the search item. For example and.
2. Press Ctrl+R (^R) to open a new bar and type in the replacement term. For example, or, like in the image below:
3. Hit Enter and choose whether to replace the word or not, replace all instances of the word, or cancel the operation.
Select, Copy, Cut, and Paste Text
To select a part of a file, navigate to the beginning of the part, press the Alt+A shortcut (M-A), and use the arrow keys to move over the text you wish to select.
Next, copy the selected text with the Alt+6 combination (M-6) or cut with Ctrl+K (^K). If you use these shortcuts without selecting any text first, they copy or cut the entire line of text.
To paste the text, use Ctrl+U (^U).
Insert Another File Into the Current One
While editing a file in Nano, it is possible to insert the entire contents of another file into the current one with the Ctrl+R (^R) shortcut.
The command opens the bottom bar to write the path to the file you want to import.
Spell Check in Nano
Nano also offers a spell-checking feature. However, to enable it, first install the spell package.
To install the spell package, run the following command:
sudo apt install spell
After installing the package, spell check in the text editor by pressing Ctrl+T (^T). It selects the misspelled word and asks for a correct replacement.
In the image below, the word command is misspelled.
Save a File
To save a file, use the Ctrl+O (^O) keyboard combination. Nano asks for a file name or to confirm an existing file name.
Once you hit Enter, the status bar indicates the number of lines that the file has.
Nano is also able to create a file backup. To do this, use the Alt+B command (M-B). Pressing the keys once enables backup, and doing it again disables it.
Exit Nano
To exit out of Nano, press Ctrl+X (^X). If changes were made, Nano asks whether to save them or not. Type y
or n
to save or discard changes or Ctrl+C to cancel exiting the file.
Advanced Nano Commands
Advanced commands in Nano offer specialized functionality beyond basic features. These arguments modify the command's behavior and customize how Nano operates.
Relevant Nano arguments are listed in the table below:
Option | Description |
---|---|
-c | Constantly displays cursor position. |
-i | Enables automatic indentation based on the previous line's indentation. |
-k | Enables cutting from the cursor position. |
-m | Enables mouse support. |
-B | Creates a file backup before saving. |
-X | Stops creating backup files when saving. |
-w | Prevents word wrapping. |
-U | Turns on Undo functionality |
-T | Sets tab size to the given number of spaces. |
-o | Enables viewing large files. |
-R | Enables restricted mode in which certain commands are not available. |
-v | Prints Nano version info. |
The following text elaborates on common advanced Nano options.
Enable Undo Function
Using the -U
option enables the undo functionality in Nano. This allows users to revert to previous changes made during the editing session, which helps correct mistakes or experiment with different edits. For instance, to enable Undo in file1, run:
nano -U file1
Once you enable the undo functionality in Nano, use the -U
option to undo changes by pressing the Ctrl+Shift+\ keyboard shortcut. This command undos the most recent change made in the editing session.
Enable Constant Cursor Position Display
Use the -c
option with Nano to show the cursor position (line and column numbers) at the bottom of the editor window. This helps keep track of the current position within the file. For example, to constantly show the cursor position in file1, run:
nano -c file1
Enable Mouse Support
Use the -m
argument to enable mouse support. The option allows users to use the mouse to navigate and select text in addition to using the keyboard shortcuts. To enable mouse support for file1, execute:
nano -m file1
Enable Automatic Indentation
The -i
option automatically indents new lines based on the indentation style of the previous line. This is particularly useful for maintaining consistent indentation in code or structured text files. To enable this functionality in file1, run:
nano -i file1
Conclusion
After reading this article, you know how to install the Nano text editor on Linux. You also learned how to navigate a Nano file as well as the basic commands and shortcuts for creating, editing, and saving files.
Next, learn other important Linux commands.