How to Use Nano, the Linux Command Line Text Editor

Introduction

Before we begin talking about how to use Nano, the Linux command line text editor, let's briefly understand – What is Nano?

Nano is a simple editor for those that require it. Nano is a lightweight and user-friendly text editor designed for command-line interfaces. With its simplicity and efficiency, Nano makes editing files on Linux and Unix systems a breeze. Vim and Emacs are two of the most capable and widely used command-line editors. Both have a steep learning curve that novice users may find intimidating.

For Unix and Linux operating systems, GNU nano is a simple command line text editor. It has all the standard text editor features, such as syntax highlighting, multiple buffers, search and replace with regular expression support, spellchecking, and UTF-8 encoding, among others.

In this tutorial, you will use Nano, the Linux command line text editor. We will also address a few FAQs on how to use Nano, the Linux command line text editor.

Advantages of Nano

  1. User-friendly: Nano's intuitive interface makes it easy for beginners to navigate and edit files.
  2. Lightweight: With minimal system resource usage, Nano is lightning-fast and ideal for low-powered devices.
  3. Simple commands: Nano boasts straightforward commands for tasks like searching, replacing, and undoing changes.
  4. Customizable key bindings: Users can personalize Nano by assigning their preferred key combinations for various actions.
  5. Syntax highlighting: Nano supports syntax highlighting for multiple programming languages, offering clarity and ease of coding.

Installing Nano

On macOS and most Linux distributions, the Nano text editor comes pre-installed. Type the below command to see if it's installed on your system.

nano --version

You will get an output like below:

Output

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

If you don't already have nano installed on your system, you can do so through your distribution's package manager.

Install Nano on Ubuntu and Debian

sudo apt install nano

Install Nano on CentOS and Fedora

sudo yum install nano

Opening and Creating Files

Type nano followed by the file name to open an existing file or create a new one:

nano filename

You can now begin editing the file in a new editor window.

A list of the most basic command shortcuts for the nano editor may be found at the bottom of the window.

The characters ^ or M are prefixed to all commands. The Ctrl key is represented by the caret symbol (^). The J instructions, for example, mean pressing the Ctrl and J keys at the same time. The Alt key is represented by the letter M.

By using Ctrl+g, you can get a list of all commands.

You must have read permissions to open a file in order to open it.

Use the following syntax to open a file with the cursor on a certain line and character:

nano +line_number,character_number filename

If the character_number is not specified, the cursor will be placed on the first character.

Editing Files

Nano, unlike vi, is a modeless editor, which means you can begin typing and altering the text as soon as you open the file.

Use the Ctrl+_ command to shift the pointer to a given line and character number. The bottom-of-the-screen menu will alter. Hit Enter after entering the number(s) in the "Enter line number, column number:" area.

Searching and replacing

To find a text, press Ctrl+w, then type in the search word and hit Enter. The pointer will automatically go to the first match. Press Alt+w to advance to the next match.

Ctrl+\ is the shortcut for searching and replacing. Enter the search keyword as well as the text that should be substituted. The editor will then move on to the first match, asking if you want to replace it. It will go to the next match if you press Y or N. All matches will be replaced if you press A.

Copping, cutting, and pasting

Move the cursor to the beginning of the text and press Alt+a to select it. A selection mark will be created as a result of this action. Using the arrow keys, move the cursor to the end of the text you wish to select. The text you've chosen will be highlighted. Ctrl+6 can be used to cancel the selection.

Using the Alt+6 command, copy the selected text to the clipboard. Ctrl+k will cut the text you've selected.

Simply move the cursor on the line and hit Ctrl+k to cut the entire line. By repeatedly pressing Ctrl+k, you can cut numerous lines.

To paste the text, move your cursor to the desired location and hit Ctrl+u.

Saving and Exiting

Ctrl+o will save the modifications you've made to the file. Once you save the file, it will be generated if it does not already exist.

Ctrl+x will close nano. If there are any changes that haven't been saved, you'll be asked if you wish to preserve them.

You must have write permissions on the file in order to save it. You must have write permission to the directory where the file will be created if you are creating a new file.

Customizing Nano (nanorc)

If the user-specific files /.config/nano/nanorc and /.nanorc are present, nano receives their configuration settings from the system-wide configuration file /etc/nanorc and the user-specific files /.config/nano/nanorc and /.nanorc.

The global options take precedence over the settings specified in the user files.

For a complete list of all available options, go to the nanorc website.

Syntax Highlighting

For the most common file types, Nano has syntax highlighting rules. The syntax files are located in the /usr/share/nano directory on most Linux systems and are included by default in the /etc/nanorc configuration file.

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

Copying the file containing the syntax highlighting rules to the /usr/share/nano directory is the simplest way to enable highlighting for a new file type.

Set Nano as the Default Text Editor

The default text editor for tools like visudo and crontab is vi on most Linux systems by default. Change the VISUAL and EDITOR environment variables to use nano as the default text editor.

The variables in the ~/.bashrc file can be exported by Bash users:

export VISUAL=nano
export EDITOR="$VISUAL"

Basic Nano Usage

The following are the most fundamental steps for getting started with nanotechnology:

  1. Type nano followed by the filename at the command prompt.
  2. Make any necessary changes to the file.
  3. To save and quit the text editor, press the Ctrl-x key combination.

FAQs to Use Nano, the Linux Command Line Text Editor

How do I navigate within a file in Nano? 

You can use the arrow keys to move the cursor or the page up/down keys to navigate through the text.

How can I save a file in Nano? 

To save a file, press Ctrl + O, confirm the filename, and then press Enter.

How do I exit Nano without saving changes? 

Press Ctrl + X to exit Nano. If there are any unsaved changes, it will prompt you to save before exiting; press N to discard changes.

How can I search for a specific word or phrase in Nano? 

Press Ctrl + W, enter the search term, and press Enter. Nano will find the next occurrence of the term.

Can I customize the key bindings in Nano? 

Yes, you can customize the key bindings by creating a ~/.nanorc file and specifying your preferred key combinations.

How do I enable syntax highlighting in Nano? 

Syntax highlighting is enabled by default in Nano. However, if it's not working, ensure that you are editing a file with a supported programming language extension (e.g., .py for Python).

How do I get help while using Nano? 

Press Ctrl + G to access Nano's built-in help menu, where you can find useful information and shortcuts for various commands.

Conclusion

We've shown you how to use the Gnu nano text editor in this article. It's a popular text editor among Linux users, and it's easy to learn.

Visit the official Gnu Nano documentation page for further details.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.