Nano Text Editor in Linux - The Beginner's Guide

How to Use Vi and Vim Editor in Linux

What is Nano Editor?

Nano is a text editor designed for the linux platform and it doesn't even need a GUI at all because it's a command line text editor and that means you can use it right over SSH and it's very easy to use. If you are absolute beginner in file editing in Linux then Nano would be essentially one of the most easiest to use text editors available for the linux platform and we're going to take a look at it today.

Table of Contents

Installing Nano Editor

Before we can use nano we have to make sure that it's already installed. On quite a few distributions of linux it's installed by default so most of the time you will already have it.

How to check if nano is already installed or not?

You can check if nano is already installed or not by typing which nano on the command line.

1[root@ifixlinux ~]# which nano
2/usr/bin/which: no nano in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
3[root@ifixlinux ~]#

The which command allows us to check and see whether another command is available in our system or not. If you see something similar to the above output that means nano isn't installed on your system.

Installing Nano Editor on CentOS/RHEL/AlmaLinux

Login to the server as root or administrative privilege and type the following -

1yum install nano

Installing Nano Editor on Ubuntu/Debian

To install Nano Editor on Ubuntu/Debian based systems, login to the server as root or administrative privilege and type the following -

1sudo apt-get install nano

You will be presented with something like the following on your terminal. Type Y when you will be prompted to allow downloading the package.

 1[root@ifixlinux ~]# yum install nano
 2Last metadata expiration check: 1 day, 1:53:32 ago on Sat 30 Dec 2023 06:42:32 AM EST.
 3Dependencies resolved.
 4================================================================================
 5 Package        Architecture     Version                 Repository        Size
 6================================================================================
 7Installing:
 8 nano           x86_64           2.9.8-1.el8             baseos           580 k
 9
10Transaction Summary
11================================================================================
12Install  1 Package
13
14Total download size: 580 k
15Installed size: 2.2 M
16Is this ok [y/N]: y
17Downloading Packages:
18nano-2.9.8-1.el8.x86_64.rpm                     3.1 MB/s | 580 kB     00:00
19--------------------------------------------------------------------------------
20Total                                           386 kB/s | 580 kB     00:01
21Running transaction check
22Transaction check succeeded.
23Running transaction test
24Transaction test succeeded.
25Running transaction
26  Preparing        :                                                        1/1
27  Installing       : nano-2.9.8-1.el8.x86_64                                1/1
28  Running scriptlet: nano-2.9.8-1.el8.x86_64                                1/1
29  Verifying        : nano-2.9.8-1.el8.x86_64                                1/1
30
31Installed:
32  nano-2.9.8-1.el8.x86_64
33
34Complete!
35[root@ifixlinux ~]#

Now type which nano on the terminal to check if it's installed or not.

1[root@ifixlinux ~]# which nano
2/usr/bin/nano
3[root@ifixlinux ~]#

Here's a video demonstration showing how to install nano on an AlmaLinux system. Installing Nano Editor in Linux System

How to Use Nano Editor?

The simplest way to use nano is to type nano followed by the file name. For example, nano test.txt and then press Enter. If the file is present on your file system, nano will open that file and you can start editing the file right away. If there's no such file, you'll notice a blank canvas has opened with some options at the bottom. On the blank canvas you can freely type, edit text, and navigate through the texts and lines.

Using nano is straightforward, unlike Vi Editor you don't need to enter a specific mode to start typing; you can simply begin typing immediately. You can navigate through the text using the arrow keys to position the cursor wherever you want to make changes.

Once you are done making changes to the file, press Ctrl + X on keyboard. You will be prompted to save the modified file. Type Y to save the file. Then you will be asked to enter the file name. If you don't want to change the file name just hit enter. The modified file will be saved.

Here's a video demonstration on basic usage of Nano text editor.

Crate and Save File Using Nano Editor

How to Delete Lines in Nano Editor

To delete a line in Nano editor, place your cursor on the line that you want to delete/cut, then press Ctrl + K That specific line will be cut/deleted. To delete multiple lines, keep pressing K Button holding Ctrl on your keyboard.

Deleting Lines Nano Editor

Show Line Numbers in Nano Editor

Showing line number is one of the most important features in any text editor as sometimes we need to quickly jump to the line where our coding error is. Nano Text Editor has this feature integrated. By default, when you open a file in Nano, it won't show you the line number. To enable line numbers on Nano, press Alt + SHIFT + 3 on your keyboard or press ALT + # if your keyboard has a dedicated # key. If you are on MacOS, press ESC + SHIFT + 3 instead. Now you will see the line numbers at the left column. You have to redo the keystrokes everytime you open a file as line number feature gets enabled only for that specific session. To make this permanent, add the following to your ~/.nanorc file.

1set linenumbers

Showing Numbers in Nano Editor

Go to a Specifc Line in Nano

Let's assume you have opened a file in Nano and want to go to a specific line number. To do so press Ctrl + / or CTRL + SHIFT + _ on your keyboard. You will be prompted to enter the line number. Type the line number you wish to go and press Enter. The cursor position will be placed at the line number you typed.

Go to a Specifc Line in Nano Editor

Open Nano Editor Pointing Cursor to a Specific Line

Sometimes you may need to jump directly to a specific line number that you already know. You can do this easily while opening the Nano Editor. Open the file by typing -

1nano +n -l filename.ext

Replace n with the line number you wish to go immediately, and filename.ext with your actual file name. Nano Editor will open pointing cursor to that specific line.

Open Nano Editor Pointing Cursor to a Specific Line

Cut and Paste in Nano Editor

Cutting a line is similar to deleting a line in Nano editor. To cut a line place your cursor on the line that you want cut, then press Ctrl + K That specific line will be cut. Now move to the place where you want to insert the text and press Ctrl + U Your text will be inserted there. If you want to paste the same text in another location, navigate to that position and press Ctrl + U again. You can repeat the steps as necessary.

Cut and Paste in Nano Editor

Search for Specific Text or String in Nano Editor

To search for a specific word, text or string in Nano editor, press Ctrl + W on your Keyboard. You will be prompted to input your search query. Enter the text or string that you want to search for and then press the Enter. The cursor will jump forward to the first occurrance of the text that you are searching for. If there are no matches, you will see a message at the bottom telling you that the thing you searched for was not found. To search for the same thing again, press Crtl + W again.

Searching in Nano Editor

Search and Replace in Nano Editor

To initiate search and replace in Nano, press ALT + R on your keyboard first. You will be prompted what to search for. Type the string or text or anything you want to search for and press Enter. You will be then prompted to input the replacement text. Type the replacement text and press Enter agin. Finally, you will be asked whether you want to replace just the first occurrence, or all occurrences. Type Y to replace the first occurrence only. Or if you want to replace all occurrences, type A and press Enter. All occurrences will be replaced.

Search and Replace in Nano Editor

How to Save Changes Without Exiting Nano Editor

To save your work at any time, press Ctrl + O (Please note, that’s the letter O, not zero) Once you press the key combination, your changes on the file you are working with will be saved without exiting Nano editor.

Saving Changes Without Closing Nano Editor

How to Save and Exit in Nano Editor

Once you are done making changes to the file, press Ctrl + X on keyboard. You will be prompted to save the modified file. Type Y to save the file. Then you will be asked to enter the file name. If you don't want to change the file name just hit enter. The modified file will be saved and Nano Editor will close.

Save and Exit in Nano Editor

Nano Editor Cheat Sheet

Syntax Description
CTRL + A Allows you to move the cursor to the beginning of the line.
CTRL + E To navigate the cursor to the end of the line.
CTRL + Y Scrolls down the page.
CTRL + V Scrolls up the page.
CTRL + G Opens a Help window displaying available commands.
CTRL + O To save the file. Nano will prompt you to rename or confirm the desired file name.
CTRL + W Search for a specified phrase in your text. Press ALT + W to search for the same phrase again.
CTRL + K Cuts the entire selected line to the cut buffer, similar to the clipboard.
CTRL + U Paste text from the cut buffer into the selected line.
CTRL + J Justifies the current paragraph.
CTRL + C Shows the current cursor position in the text (line, character).
CTRL + R Opens and inserts a file at the current cursor position.
CTRL + X Exit the Nano text editor prompting a save request if changes were made.
CTRL + \ Replace a string or regular expression.
CTRL + T Invoke the spell checker, if available.
CTRL + _ Navigate to a specified line and column number
ALT + A To select text, and combine it with CTRL + K to cut a specific part of the text to the cut buffer.

Conclusion

Among the text editors available for Unix based systems, Nano Editor is one of the most popular because of simple yet powerful user interface and integrated features. It's accessibility makes it an excellent choice for both beginners and experienced users seeking a quick and straightforward tool for text editing.