How to Use Nano: the Linux Command Line Text Editor 2024

Learn Basic Usage of Nano Command Line Text Editor for Unix and Linux | Syntax Highlighting, Search and Replace, Cut and Paste

How to Use Nano, the Linux Command Line Text Editor

When working on the command line, creating or editing text files is a common task. Vim and Emacs are two of the most popular and powerful command-line editors, but their steep learning curve can be daunting for new users. For those who need a simpler alternative, GNU nano is an easy-to-use command-line text editor designed for Unix and Linux operating systems.

This guide will explain the basic usage of the nano editor, including how to create and open a file, edit a file, save a file, search and replace text with regular expression support, cut and paste text, and more. With nano, you can enjoy all the essential features you’d expect from a regular text editor, including syntax highlighting, multiple buffers, spellchecking, UTF-8 encoding, and more. Start using the nano editor today!

Installing Nano

The Nano text editor comes pre-installed on macOS and many Linux distributions. To verify whether it is installed on your system, enter the following command:

ShellScript
nano --version

The resulting display will appear in a similar fashion as follows:

Output:

GNU nano, version 2.9.3
(C) 1999-2011, 2013-2018 Free Software Foundation, Inc.
(C) 2014-2018 the contributors to nano
Email: [email protected] Web: https://nano-editor.org/

If nano is not currently installed on your system, it is possible to install it using the package manager specific to your distribution.

Install Nano on Ubuntu and Debian

ShellScript
sudo apt install nano

Install Nano on CentOS and Fedora

ShellScript
sudo yum install nano

Opening and Creating Files

Type “nano” followed by the name of the file to either create a new file or access an existing file:

ShellScript
nano filename

Nano text editor

This opens a fresh editor window, you gain the ability to commence modifying the file.

The nano editor displays a list of fundamental command shortcuts at the bottom of the window for easy access.

Commands in this system are denoted with either the caret (^) or M character prefix. The caret symbol represents the Ctrl key, so ^J commands would involve pressing the Ctrl and J keys together. Meanwhile, the letter M indicates the Alt key.

To retrieve a comprehensive index of commands, simply press the key combination Ctrl+g.

To open a file with the cursor positioned on a particular line and character, utilize the following syntax:

ShellScript
nano +line_number,character_number filename

In case the character_number is not included, the cursor will be placed at the beginning of the first character.

Editing Files

Nano is different from vi in that it is a modeless editor, allowing you to begin typing and editing text right after opening a file.

If you need to navigate to a particular line and character position, you can do so by executing the Ctrl+_ command, which will modify the menu displayed at the bottom of the screen. Simply input the desired line and column numbers into the “Enter line number, column number:” field, and press the Enter key to proceed.

Searching and replacing

To look for a specific text, you can follow these steps: Press the “Ctrl” key and the “w” key simultaneously, type in the search term you’re looking for, and then hit “Enter“. This will bring you to the first instance of the search term. If you need to move to the next match, press “Alt” and “w” keys together.

To perform a search and replace operation, use the keyboard shortcut Ctrl+\ and input the search term along with the replacement text. The editor will then locate the first instance of the search term and prompt you to replace it. To proceed with the replacement, hit Y, otherwise press N to skip. Use A to replace all instances of the search term throughout the document.

Copping, cutting, and pasting

To choose a portion of text, start by positioning the cursor at the start of the desired text and pressing Alt+a, which will set a marker. Then, navigate with the arrow keys to the end of the text you wish to select. The selected text will be visibly marked. In case you want to deselect the text, press Ctrl+6.

To copy the chosen text to the clipboard, utilize the Alt+6 command. On the other hand, if you intend to cut the selected text, you can press Ctrl+k.

To remove entire lines, position the cursor on the desired line and press Ctrl+k. Repeating the Ctrl+k command allows you to remove multiple lines simultaneously.

To insert the text, position the cursor at the desired location and use the keyboard shortcut Ctrl+u.

Saving and Exiting

To retain the modifications made to the file, use the keyboard shortcut Ctrl+o. If the file doesn’t exist already, it will be formed upon saving.

To exit nano, use Ctrl+x. If you have unsaved changes, you will be prompted to decide whether to save them.

In order to save the file, you must possess write permissions for it. When creating a new file, you must have write permission for the directory in which the file is being generated.

Customizing Nano (nanorc)

Upon launching, nano retrieves its configuration settings from three files: the system-wide configuration file located at /etc/nanorc, and the user-specific files found at ~/.config/nano/nanorc and ~/.nanorc (if they exist).

In the event that there are conflicting options between the global and user files, the user-specified options will take precedence.

For a comprehensive inventory of all available options, refer to the nanorc webpage.

Syntax Highlighting

Syntax highlighting rules for the majority of popular file types come pre-installed with Nano. These syntax files are typically stored in the /usr/share/nano directory on most Linux systems and are included in the default /etc/nanorc configuration file.

ShellScript
include "/usr/share/nano/*.nanorc"

To facilitate highlighting for a new file type, the simplest approach is to duplicate the file that has the syntax highlighting rules and place it in the /usr/share/nano directory.

Set Nano as the Default Text Editor

On most Linux systems, the text editor set by default for commands like visudo and crontab is vi. To make nano the default editor, you must modify the environment variables for VISUAL and EDITOR.

If you’re a Bash user, you can export these variables in the ~/.bashrc file:

ShellScript
export VISUAL=nano
export EDITOR="$VISUAL"

Basic Nano Usage

Here are the essential instructions to begin using nano:

  1. Type “nano” on the command prompt, followed by the filename.
  2. Modify the file as necessary.
  3. Save and exit the text editor by using the Ctrl-x command.

Conclusion

This tutorial has demonstrated the usage of Gnu nano text editor, which is widely used by Linux users and has a user-friendly learning curve. For additional details on Gnu Nano, you can refer to the official nano documentation page. Should you have any queries, please feel free to drop a comment.

Alex

Alex

Hey there! My name is Alex and I'm a professional content writer. I'm also lucky enough to be part of an amazing trio team! I absolutely love what I do and I'm passionate about creating content that engages, informs, and entertains. Whether it's writing blog posts, website copy, or social media content, I always strive to deliver high-quality work that exceeds expectations.

We will be happy to hear your thoughts

Leave a reply

TrioGuide
Logo