How to Use Nano Command Line Text Editor in Linux

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:

nano --version

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:

OptionsDescription
-BTo save a file and back up the previous version of it by appending a tilde (~) to the current filename.
-ETo convert the typed tabs into spaces.
-LTo stop adding a new line after the original text.
-NDisables automatic conversion of files from Mac/DOS format to Unix format
-QTo match a part of the text using regular expressions
-lDisplays the line number to the left of the text area
-uTo 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:

nano test.txt

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:

nano test1.txt

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 ShortcutDescription
CTRL + AMoves the cursor at the beginning of the line
CTRL + EMoves the cursor at the end of the line
CTRL + YScrolls the screen up by a page
CTRL + VScrolls the screen down by a page
CTRL + GOpens nano command help window
CTRL + OSaves the current file
CTRL + WSearches for a specific text in the entire document
CTRL + KCuts the entire selected portion of text to the clipboard
CTRL + UPastes the text portion from the clipboard into the document
ALT + 6Copies the selected text to the clipboard
CTRL + XExits the editor
CTRL + _Lets you go to the specified line and column number.
ALT + AUsed to select text
CTRL + GOpen help menu for nano

Frequently Asked Questions

Why do we use nano in Linux?

The nano editor is a simple easy-to-use command-line text editor, which can be handy for editing text documents on Linux distros.

Is nano easy to use?

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.

Comments 0
Leave a Reply