Nano Text Editor in Linux

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Nano, ANOther text editor in Linux, is a simple yet powerful command line-based text editor used for editing text files. It is very popular among beginners for its simple-to-use interface. As a command-line editor, nano offers a lightweight alternative to more complex graphical text editors.

In this article, you will learn how to use the nano text editor in Linux, right from installing it to editing documents with it.

What is the Role of Nano in Linux Text Editor?

The role of nano in Linux text editor besides basic text editing is that it offers features like undo/redo, syntax coloring, auto-indentation & line numbers, interactive search-and-replace, word completion, backup files, file locking, and internationalization support.

The original role of nano is to be a complete bug-for-bug emulation of another text editor: Pico. However, nano strives to provide additional functionality beyond what Pico offers. Nano does that by:

  • Being a truly free software.
  • Emulating Pico functionality reasonably.
  • Including extra functionality by default.

Compared to other text editors, Nano is more simple and friendly for beginners while providing the necessary features. That’s why it matters to beginners and professionals looking for a complete text editor tool.

How to Install Nano in Linux?

Nano is typically available in many Linux distributions by default. However, if it is not installed, you can install nano manually using simple commands.

To install nano, in Debian/Ubuntu, run the following commands:

sudo apt update
sudo apt install nano

For CentOS/Fedora, use the below command:

sudo yum update
sudo yum install nano

Once installed, you can now get started with nano in Linux.

Getting Started With Nano Text Editor in Linux

In this part, you will learn how to open and close files. Also, how to search and replace text. Then, I’ll show you some editing tips and other basic commands that you can use in the nano text editor.

1. Opening a File with Nano

To use the nano command in Linux, first, open your terminal. Then, type the following command and press ENTER:

nano demo.txt

Note: After running the nano command, a new window pops up where you can access your file. Also, if you only execute the nano command without specifying the file name, nano will create an empty untitled file and ask for a name when you exit the editor.

Upon running, the command opens the specified file in the nano text editor. You can create various file types, like .txt, .html, .php, and so on:1-Creating a text file using nano in linux

Remember that if you want to open an existing file, you need to be in the directory where the file is located. To open a file from another folder, run the below command:

nano /home/yousuf/demo.txt

2-Opening a file from another folder

Note: To exit nano, press CTRL+X on your keyboard.

Here is a list of useful commands to use in the nano text editor:

Command Function
CTRL+A Jumps to the beginning of the line.
CTRL+E Jumps to the end of the line.
CTRL+Y Scrolls the page down.
CTRL+V Scrolls the page up.
CTRL+G Shows all the available commands in Help window.
CTRL+O Saves the file with a confirmation message.
CTRL+W Looks up a specific phrase in the text.
CTRL+K Cuts the entire selected line to the cut buffer.

Note: Avoid using the SHIFT key in Nano. All shortcuts use unmodified numbers and lowercase letters.

2. Basic Navigation and Editing in Nano

For intuitive and easy navigation and editing in Nano, use the below essential commands:

  1. To move the cursor up, down, left, or right, use the arrow keys.

  2. To move to the next page, press Ctrl+V. To go to the previous page, tap Ctrl+Y.

  3. To edit a file in Nano, you can directly start typing to edit your file. Except in Vim, where you have to consider different modes. In nano, you can also perform operations like cut, copy, paste, and more.

  4. To select texts in nano, go to the beginning of the word and press ALT+A. Once selected, press ALT+6 to copy the selected text to the clipboard: 3-Selecting text using keyboard shortcut

  5. To cut texts in the nano text editor, use CTRL+K keys. Pressing CTRL+U or SHIFT+INSERT pastes the contents from the clipboard to nano.

3. Searching and Replacing Text

The nano command provides search and replace utility to search and replace specific texts inside a huge document. The default search mode is case-insensitive and forward.

Here are some commands to search and replace specific texts when scrolling is not a viable option:

To search for a text in nano:

  1. First, press CTRL+W >> enter the text you want to search in the search bar at the bottom of the screen: 4-Entering text in the search bar
  2. Tap the ENTER key. Now, the cursor moves to the beginning of the searched item in the document: 5-Searched item appears
  3. To go to the next occurrence of the searched item, press ALT+W.

To search and replace a text in the nano editor, use the below steps:

  1. Press CTRL+ >> type the text you want to replace >> press ENTER: 6-Entering text to be replaced with
  2. In the next prompt, type the text you want to replace it with. Hence, the cursor moves to the first occurrence of the searched item: 7-Entering text to replace another text
  3. Either press Y to replace this text or N to skip the current match and move on to the next one: 8-Using replacement options
  4. To replace all the matching items, simply press A: 9-Replacement text appears

4. Saving a File

You can save a file in nano in two ways: save and exit, save and continue editing. Here are the steps:

To save a file and exit nano text editor:

  1. Press CTRL+X on your keyboard.
  2. Press Y to save the changes or N to discard any changes to the file.Thus, the prompt asks if you want to keep the same file name or use a new name.
  3. Either enter a new file name or press the ENTER button to use the same file name: 10-Saving options in nano

To save and continue editing, simply press CTRL+O on your keyboard: 11-Saving and editing in nano

5. Customizing Options in Nano

To customize nano’s default configuration to suit your preferences, you can modify the nanorc file located in /etc/nanorc or ~/.nanorc. You can modify the file to change nano’s behavior to enable syntax highlighting, define keyboard shortcuts, or change default options. However, to edit this file, you will need sudo privileges.

Here are some features you can add to the .nanorc file:

Feature Description
smooth Enables smooth scrolling when navigating through the file.
softwrap Enables word wrapping to fit long lines within the editor window.
casesensitive Enables case-insensitive search by default.
linenumbers Shows the line number on the left.
titlecolor Sets title color.
mouse  Enables mouse support, if available for your system.

To customize nano by creating a .nanorc file in the home directory with various features, follow these steps:

  1. Run the following command:
    nano ~/.nanorc
  2. Now add the following lines in the .nanorc file:
    set smooth
    set softwrap
    set casesensitive
    set linenumbers
    set mouse
    set titlecolor cyan

    12-Adding features in the .nanorc file

  3. Save the file and exit:13-Changes appear in nano in Linux

These changes enable smooth scrolling, soft line wrapping, case-sensitive searching, display of line numbers, mouse support, and setting the title bar color to cyan.

Examples of Using Advanced Commands in Nano

Now, let’s see a few advanced commands in nano to dive deeper:

Option Function
-B Creates a backup for the current file.
-U Accepts undo functionality.
-Y Enables syntax highlighting.
-I Enables automatic indentation.
-c Shows the cursor position.
-m Enables mouse support.

Here are some examples using nano options in Linux:

1. Creating File Backups

To create file backups before saving changes, use the nano command with the -B option. If you need to change your file significantly, this command allows you to keep a copy of the original file.

Run the below command to create a backup of a file:

nano -B states.txt

14-Creating file backups

As a result, a backup file (states.txt~) is created before saving the changes.

2. Enabling Undo Functionality

To enable the undo functionality, you can use the nano command followed by the -U option. This function allows you to return to previous changes you made while editing. Here is the command:

nano -U states.txt

15-Pressing keyboard shortcut to undo deletion

3. Enabling Syntax Highlighting

To enable syntax highlighting of texts, use the nano command with the -Y option. For example, I have a text file named demo.txt containing some code lines:

16-Text file with Python code

To enable syntax highlighting to the specified file, use the command below:

nano -Y python demo.txt

17-Syntax highlighting using -Y option

As a result, the command adds Python syntax to the file text.

4. Displaying Cursor Position Constantly

To display the cursor position constantly, you can use the nano command along with the -c option. Showing cursor position helps you keep track of where you are in documents and navigate easily.

Use the command below to show the cursor position:

nano -c states.txt

18-Displaying cursor position in nano

Upon running, nano shows the position of the cursor at the bottom of the screen.

5. Enabling Mouse Support

To enable mouse support along with the keyboard shortcuts, use the nano command and -m option. The mouse support allows you to navigate and select texts using a mouse. Here is  the command:

nano -m states.txt

19-Enabling mouse support in nano

Note: To select text for or paste text from the clipboard with the mouse support, hold down the SHIFT key and then select or paste the text as you normally would.

Exploring Nano Alternatives in Linux

Here are a few nano alternatives to explore based on factors such as learning curve, functionality, personal preference, etc:

Feature Nano Vi/Vim Emacs
Learning Curve Easiest Steep Steep
Mode Single mode Modal Single mode (extendable)
Speed Moderate Very fast Can be slow on startup
Functionality Basic editing Powerful editing, searching, macros Highly extensible
Customization Limited Extensive plugins and configurations Extensive plugins and configurations
Use Case Beginners, quick edits Programmers, experienced users Programmers, power users, heavy customization
Availability Most Linux distributions Most Linux distributions Most Linux distributions
File opening at once Single Multiple Multiple

How to Set Nano as Default Text Editor in Linux?

You can set nano as the default text editor in Linux by changing the VISUAL and EDITOR environment variables. On most Linux systems, the default text editor for commands such as visudo and crontab is set to vi.

To set nano as the default text editor for your user in Linux, follow the steps below:

  1. Run the below command to export the variables in the ~/.bashrc file as Bash users:
    ~/.bashrc
  2. Paste the following lines in the editor:
    export VISUAL=nano
    export EDITOR="$VISUAL"

    These lines set the environment variables VISUAL and EDITOR to nano. Many programs use these variables to determine the default text editor.

  3. Save the file and exit the text editor.
  4. To apply the changes, either restart your terminal session or run:
    source ~/.bashrc

Now, nano should be set as the default text editor for the user or system. You can test it by running EDITOR or trying to edit a file using the system’s default text editor.

Conclusion

To wrap up, nano in Linux is a popular and versatile terminal-based text editor. Now, you know how to install the nano text editor on Linux. You have also learned how to work with a nano file, the basic commands, and a few shortcuts for creating, opening, editing, searching, and saving files. If you have any questions or suggestions, let us know in the comment box below.

People Also Ask

What is nano in Linux?

The nano is an easy-to-use command-line text editor that allows you to edit text documents in Linux distros. By default, nano is installed into /usr/local. The default screen of Nano consists of four areas: a title bar, an edit window, a status bar, and two helplines.

Is nano easy to use among other Linux text editors?

Yes, the nano text editor is easier for beginners to use in their daily workflow, with a much simpler learning curve than other text editors (for example: vim) in Linux.

How do I make a .nanorc file that will be read when I start nano?

To make a .nanorc file that will be read while starting nano, simply copy the sample .nanorc file from the doc/ directory in the nano source package or /usr/doc/nano on your system >> paste it into a file named .nanorc in your home directory >> open the .nanorc file in a text editor >> edit the file as needed >> save the changes and exit the editor.

Why does my self-compiled nano not read /etc/nanorc?

By default, nano gets installed into /usr/local. This also indicates nano will read /usr/local/etc/nanorc instead of /etc/nanorc at startup. If you want your self-compiled nano to read the same nanorc as the system-installed nano, make a symlink from the former to the latter. Or you can configure your nano to overwrite the system nano.

What are the applications of nano in Linux?

Nano in Linux is primarily used as a text editor for editing files directly from the command line interface. The applications of nano also include writing scripts, editing text documents, system administration tasks, etc.

5/5 - (3 votes)
Avatar photo

Yousuf Shovon is a Linux Content Developer Executive (LCDE) at SOFTEKO. Previously, he worked for ExcelDemy, writing over 60+ articles on Excel. His goal is to write quality content for users to read and learn. In the future, he also wants to build various projects with LinuxSimply. He completed his graduation and post-graduation in Information Technology from Jahangirnagar University. Yousuf finds joy in solving diverse Linux-related problems to gain an all-around experience in Linux. Read Full Bio

2 thoughts on “Nano Text Editor in Linux”

Leave a Comment