Text Editing with Nano

Learn via video courses
Topics Covered

Overview

When you are working on the command line, you might need to create or edit text files. Two well-known editors for this are Vim and Emacs, but they can be tough for beginners. If you want something easier, there's Nano. Nano is a versatile and easy-to-use text editor that is widely used in command-line environments, particularly on Unix-based systems. It is well-known for its ease of use, making it a good choice for beginners as well as professionals who need to edit text files in a terminal quickly.

Introduction to Nano Text Editor

Introduction

Nano is a versatile and easy-to-use text editor that is widely used in command-line environments, particularly on Unix-based systems. It is well-known for its simplicity, making it an excellent alternative for both beginners and professionals who need to edit text files in a terminal quickly. This editor operates on a WYSIWYG(What You See Is What You Get) principle. It builds upon the features of UW Pico, enhancing its ease of use. In contrast to editors like Vim, Nano doesn't have different modes. It offers a simple Graphical User Interface (GUI) that enables users to directly interact with the text, eliminating the need to switch between modes like in Vim.

Installing Nano Text Editor

In many Linux distributions, Nano is pre-installed as the default text editor. To check this you can use the following command:

When you run the above command, the Nano text editor's version information will be displayed. If it displays an error, you need to install the Nano text editor.

Installing Nano on Debian/Ubuntu:

To install the Nano text editor on Debian or Ubuntu machines, run the following command:

Installing Nano on CentOS/RHEL:

To install the Nano text editor on CentOS or RHEL machines, run the following command:

Working with Nano Text Editor

To Create and Open a New File

To open an existing file or create a new one, type nano followed by the file name:

to create and open a new file

The above command will create a new file with the name shown in the output. If the file already exists, it will be opened; if it does not exist in the current directory, it will be created. Additionally, at the bottom of the Nano window, you'll find a list of shortcut keys for your reference. All commands in Nano are indicated with either the caret symbol (^) for the Ctrl key or the letter M for the Alt key. For example, when you see ^J, it means you should simultaneously press the Ctrl and J keys. To access a complete list of commands, simply type Ctrl+G.

To Save a File

to save a file

After pressing the Ctrl+o, it will ask you to enter a filename. If you want to save your changes in a new file or create a new one, you should provide a different name. However, if you wish to retain the existing file, you can keep the filename as it is.

to save a file

Once you hit the Enter key, if you've changed the filename, it will save the file with the new name. If you haven't made any changes to the filename, it will save the modifications to the existing file.

To Cut and Paste into a File

In the nano text editor, Ctrl+K is used for cutting text, while Ctrl+U is used for pasting it.

  • To cut and paste a whole line:

    In the nano text editor, navigate to the line you want to cut and press Ctrl+k. The line is then copied to the clipboard. To paste it, move to the desired location and press Ctrl+u.

    to cut and paste into a file

    to cut and paste into a file

  • To cut and paste the selected text:

    In the nano text editor, select the text that you want to cut and press Ctrl+k. The text is now copied to the clipboard. To paste it, move to the desired location where you want to paste it and press Ctrl+u.

    to cut and paste the selected text

    to cut and paste the selected text

To Search a Specific Word in a File

To search for a specific word in a file using nano, you can use Ctrl+W. When you press Ctrl+W, the editor will prompt you to enter the word you want to find. After entering the word, nano will search for it in the file and position the cursor at the beginning of the first occurrence of that word.

to search a specific word in a file

to search a specific word in a file

To Enable Spell Check in Nano

To use spell check in Nano, you must first install the spell check package. You can do this by running the following command:

to enable spell check in nano

After entering this command, you'll be prompted to enter your password. Type your password and then press the 'y' key followed by the 'Enter' key to confirm the installation.

to enable spell check in nano

to enable spell chekc in nano

To perform a spell check, first press Ctrl+t. It will ask you to enter the correct word to replace the incorrect one. After you enter the replacement word, simply press the Enter key.

Basic Nano Text Editor Commands

We've collected a list of the most useful commands to help you utilize the Nano text editor more effectively.

CommandExplanation
CTRL + AJump to the beginning of the line.
CTRL + EJump to the end of the line.
CTRL + YScroll the page down.
CTRL + VScroll the page up.
CTRL + GOpens a Help window with a list of available commands.
CTRL + OSave the file. Nano will ask you to confirm or specify the filename.
CTRL + WSearch for a specific phrase in your text. Press ALT + W to search for the same phrase again.
CTRL + ROpen a file and insert its contents at the current cursor position.
CTRL + UPaste the text from the cut buffer into the selected line.
CTRL + KCut the entire selected line to the cut buffer (similar to the clipboard).
CTRL + JJustifies the current paragraph.
CTRL + TActivate the spell checker if available.
CTRL + CDisplay the current cursor position in the text (line/column/character).
CTRL + XExit the Nano text editor. It prompts a save request if you've made any changes to the file.
CTRL + _Go to the specified line and column number.
CTRL + \Replace a string or regular expression.
ALT + ASelect text. You can combine this command with CTRL + K to cut a specific part of the text to the cut buffer.

Conclusion

  • Nano is a versatile and easy-to-use text editor that is widely used in command-line environments, particularly on Unix-based systems.
  • Nano offers a simple Graphical User Interface (GUI) that enables users to directly interact with the text, eliminating the need to switch between modes like in Vim.
  • To install the Nano text editor on Debian or Ubuntu machines, run the $sudo apt install nano command.
  • To install the Nano text editor on CentOS or RHEL machines, run the $yum install nano command.
  • To open an existing file or create a new one, type nano followed by the file name.
  • The key combination Ctrl+O is used to save the file you are currently working on. When you press Ctrl+O, Nano will prompt you to confirm or specify the filename under which you want to save your changes.
  • To search for a particular word in a file using Nano, press Ctrl+W. This command prompts you to enter the word you want to locate. After entering the word, Nano will search for it in the file and position the cursor at the start of the first occurrence of that word.