Nano is a simple yet powerful command line-based text editor, very popular among beginner Linux users for its simple-to-use interface. As a command-line editor, it offers a lightweight alternative to more complex graphical text editors. In this article, we will explain how you can use the nano text editor in Linux, right from installing it to editing documents with it.
How to Install Nano in Linux
Generally, the nano editor comes preinstalled on most Linux distros. If you are not sure, you can check using the following command:
After executing the command, if you see the nano version number in the Linux Terminal, this means it is installed, and you can proceed to the next section wherein we’ve described how to use the nano editor in Linux. If you get an error like “nano: command not found,” then use the commands below to install nano depending on your distro:
- For Debian-based distros:
sudo apt install -y nano
- Install on Cent OS/RHEL-based distro:
sudo yum install -y nano
- For Arch-based distros:
sudo pacman -S nano
- For Fedora-based distros:
sudo dnf install nano
Nano Command: Syntax and Options
Using the nano command line editor in Linux is pretty straightforward. The basic syntax to use nano text editor is:
nano <options> <file_name>
In the above syntax, the <file_name> stands for the name of the file you want to create or edit via the Nano command line editor. And for the options, some of the common ones you can pair with the nano command are:
|To save a file and back up the previous version of it by appending a tilde (~) to the current filename.
|To convert the typed tabs into spaces.
|To stop adding a new line after the original text.
|Disables automatic conversion of files from Mac/DOS format to Unix format
|To match a part of the text using regular expressions
|Displays the line number to the left of the text area
|To save the file in Unix format by default.
How to Open/ Create a New File with Nano
To open a file with the nano command in Linux, use this syntax:
nano <options> <file_name>
For example, to open the file “test.txt”, use the following command:
To create a new file with nano, use this command:
nano <options> <file_name>
For example, to create a new file with the name “test.py”, use this command:
When the nano command is executed, it first looks for the given file name in the mentioned directory. If the file is found, it opens the file, else creates a new file with the given file name.
How to Edit a File with the Nano Editor
In the Nano command line editor, you can directly start typing to modify your file, unlike Vim where you have to take care of different modes. You can easily perform operations like cut, copy, paste, and more using the nano command in Linux.
Cut Selected Text
- To select the text, head over to the beginning of the word which you want to select and press “ALT + A.” Use the arrow keys to select the text as you need.
- Once you have selected the desired text, press “CTRL + K” on the keyboard to cut the selected portion of the text.
Copy Selected Text
- First, select the text using “ALT + A” and the arrow keys.
- Once selected, use “ALT + 6” to copy the selected text to the clipboard.
Paste Selected Text
- First, navigate to the place where you want to paste the selected text using the arrow keys.
- Now, press “CTRL + U” on the keyboard to paste the text from the clipboard.
Search and Replace Text in Nano
Sometimes you need to search for some specific text inside a huge document and scrolling through it is not a viable option. Fortunately, the nano command comes with a search-and-replace utility, which can work on documents formatted with Linux, Windows, macOS, etc.
To search for a specific text, press “CTRL + W,” type the text you want to search in the new search bar at the bottom of the screen and hit the Enter key. If found, the cursor will get placed at the beginning of the searched word. To move on to the next occurrence of the searched term, use “ALT + W.”
To search and replace a specific text, press “CTRL + \” on the keyboard. Enter the search term and press enter. Then, enter the term you want to replace it with on the next prompt. The cursor will move to the first position it found the search term.
You can press either ‘y’ to replace this text or ‘n’ to skip the current match and move on to the next one.
To replace all occurrences of the search term with the replace term, press ‘a’ on the keyboard.
The default nano command keybindings are different from the common keybindings, which the users may be accustomed to while working on other Linux GUI apps. To make things easier for beginners, the Nano text editor has a small cheat sheet right at the bottom of the screen (more on it later).
How to Save Files and Exit Nano Editor
How to exit Vim is a question that has been wreaking havoc in the lives of Linux users since its inception, as you don’t get any prompt even for basic tasks such as saving or exiting the editor. However, the nano command provides a small prompt to help users get around.
To save and exit the nano command screen and return back to the Linux shell prompt, simply press “CTRL + X” on your keyboard. If you have made any changes to the file, press ‘y’ to save the changes or press ‘n’ to discard them. You will then be asked if you want to keep the same file name or use a new name. Enter the new file name or press Enter to use the same file name.
To simply save and continue editing the document in Nano editor, press “CTRL + O” on the keyboard. Press the Enter key to use the same file name or enter the new name and then hit enter.
Nano Command in Linux: Keyboard Shortcuts
The nano command in Linux comes with some nifty keyboard shortcuts that you can use to make your workflow much easier. You even get a smaller version of help at the bottom of the screen. Here, the caret symbol ‘^’ is read as CTRL, and the ‘M’ character is read as the Meta key or the ALT key.
Here are some of the common shortcuts to use with the nano command that works on all operating systems, including Linux, Windows, macOS, etc:
Keyboard Shortcut Description CTRL + A Moves the cursor at the beginning of the line CTRL + E Moves the cursor at the end of the line CTRL + Y Scrolls the screen up by a page CTRL + V Scrolls the screen down by a page CTRL + G Opens nano command help window CTRL + O Saves the current file CTRL + W Searches for a specific text in the entire document CTRL + K Cuts the entire selected portion of text to the clipboard CTRL + U Pastes the text portion from the clipboard into the document ALT + 6 Copies the selected text to the clipboard CTRL + X Exits the editor CTRL + _ Lets you go to the specified line and column number. ALT + A Used to select text CTRL + G Open help menu for nano
Frequently Asked Questions
The nano editor is a simple easy-to-use command-line text editor, which can be handy for editing text documents on Linux distros.
Compared to Vim, the nano command line editor in Linux has a much easier learning curve and is easy for beginners to use in their daily workflow.