How to edit a file with Nano

Nano is a simple and reliable command line text editor included with Unix and Linux operating systems. It includes all the essentials of a command line text editor like UTF-8 encoding, syntax highlighting, find and replace, regular expression support, and more.

Nano is best for simple edits and a great editor for beginners.

In this tutorial we will learn the basic use of the nano editor like opening and creating a file, copy / cut / paste, find / replace, save and exit.

Install Nano on Linux

The nano editor is preinstalled in newer versions of Linux distributions. Run the command to confirm Nano is installed:

$ nano --version

Nano verification

If Nano isn’t installed, follow these steps to install it.

Install Nano on Debian / Ubuntu

For Debian and Ubuntu distributions, run the following command:

$ sudo apt install nano

Install Nano on CentOS / Fedora / RHEL

For RedHat distributions like CentOS and Fedora, run the command:

$ sudo dnf install nano

With Nano installed, let’s check basic usage.


1 open file nano
2 Edit the file
3 save file Ctrl + O and press Enter
4th End Ctrl + X
Basic nano use

How to open a file

To open a file with text, just call the nano Command followed by the name of the file as shown.

$ nano filename

How to create a file

Nano command followed by a new file name creates a new file.

For example, to create a new file called. to create Greetings.txt, carry out:

$ nano greetings.txt

This will open an empty GNU Nano editor. The Nano Editor screen displays the version of Nano and the name of the file, which are displayed just below the menu bar.


Open a blank file with Nano
Open a blank file with Nano

At the very bottom there are a number of keyboard shortcuts that you can use with Nano. All links are preceded by the caret symbol ( ^ ) what the denotes Ctrl Key on keyboard.

For example, ^ O means to press Ctrl + Off.

To get a list of all command abbreviations, press Ctrl + G. This will open the main Nano help text.


Nano main help text
Nano main help text

How to edit a file with Nano

Unlike the Vim editor, Nano is modeless, which means you can just start typing and changing text right after opening the file.

NOTE: To open a file with Nano, make sure you have the appropriate permissions. Otherwise, you will receive a notification below that the file cannot be written. As a workaround, simply assign the required permissions to the user or assign sudo permissions to the user.


    open a file with nano without the proper permission
open a file with nano without the proper permission

Copy, cut and paste text

With Nano you can easily copy / cut and paste text. To better demonstrate this we have a simple text file called. created greetings.txt and added the displayed content.

$ sudo vim greetings.txt

To start copying text, place the cursor at the beginning of your preferred text and press Alt + a. This will create a selection mark and you will see the message ‘Mark Set’ at the bottom.


Set selection mark when copying text in Nano
Set selection mark when copying text in Nano

In the next step, select the text to be copied. So press the forward arrow key to the end of the text to highlight the text. In the example shown, we have highlighted the first sentence. To deselect or cancel, press Ctrl + 6.

To copy the selected text to the clipboard, press Alt + 6.

To cut the selected text instead of copying it, press Ctrl + k. When cutting, the text is completely removed.


Once the selected text has been copied or cut, scroll to where you want to paste the text and press Ctrl + you.


Search and replace

Nano-Editor allows you to find and replace text. to Find text, just press Ctrl + w Then enter your search term. Here we are looking for the string “Linux”.


Search for a string

This will take you to the first instance of the appropriate keyword.


Match text search

To go to the next game, press Alt + w.

to find and replace a stringg, press Ctrl + . This asks you to enter the search term. Here we want to search for the string “Linux” and replace it with “Unix”. After entering the string, press ENTER


Enter a search term in nano.  a
Enter a search term in nano. a

Enter the string to replace the search keyword. In this case we have provided “Unix” as a replacement.


Enter a string to replace the search term in the nano editor
Enter a string to replace the search term in the nano editor

Then press ENTER again. You will be asked to confirm the changes. Press ‘Y’ to confirm and, if in doubt, just press ‘N’.


Confirm the replacement of the string to nano
Confirm the replacement of the string to nano

Eventually the string is replaced.


Replaced search string in nano editor
Replaced search string in nano editor

Save and exit text

To save changes to a text file, just press Ctrl + O and confirms the file name to be written, then press Enter Key. If the file did not exist, it will be created automatically.

Just press to exit Ctrl + X.

If you try to exit without saving the changes, Nano will remind you that there are some unsaved changes and ask you to take action. Pressing ‘AND‘saves the changes during’n‘ignores saving changes and exits Nano.


Quit Nano without saving your changes
Quit Nano without saving

Configure Nano as the standard text editor

On most major systems, the default command line text editor is for utilities like crontab is set to vi. To make the GNU Nano editor the default text editor, define the following environment variables in the ~ / .bashrc File.

export VISUAL=nano
export EDITOR="$VISUAL"

Then save the changes you made and exit that ~ / .bashrc File.

Mark sentence position

The syntax highlighting feature helps to highlight the source code of components in a different color or font according to their syntax. By default, Nano includes syntax highlighting rules for common file types such as bash scripts, HTML, Perl, and Python files, to name a few. The syntax files are located in the /usr/share/nano Directory.


Nano syntax highlighting files
Nano syntax highlighting files

These are also by default in /etc/nanorc Configuration file.

diploma

In this tutorial we learned how to use the nano editor. Your feedback is greatly appreciated.