It is basic requirement for the command line work to create or edit text files. Vim and Emacs are powerful command line editors but those might be overwhelming for new Linux users. Nano text editor is straightforward and easy to use. It includes all the basic functionality same as other text editors such as UTF-8 encoding, syntax highlighting, search and replace with regular expression support, multiple buffers, spellchecking and more. In this tutorial we will show you how to install and use of nano text editor.
Install Nano Text Editor
Nano text editor is by default included in macOS and most Linux distributions. The process depends on the operating system you are running. If Nano text editor is pre-installed, you can skip this section to start learning the basic Nano text editing commands. You can check whether nano is installed or not issue the below command:
It should show output as given below:
GNU nano, version 2.9.3 (C) 1999-2011, 2013-2018 Free Software Foundation, Inc. (C) 2014-2018 the contributors to nano Email: firstname.lastname@example.org Web: https://nano-editor.org/
If you don’t have nano installed on your system, you can install it using the package manager of your distribution. Make sure you need administrator privileges for Ubuntu, Debian and CentOS to install nano.
Installing Nano on Debian and Ubuntu
To install Nano text editor on Debian or Ubuntu system, issue the following command:
sudo apt install nano
Installing Nano on CentOS and RHEL
To install the Nano text editor on CentOS or RHEL based platforms, run this command:
sudo yum install nano
Use Nano Text Editor
Open and Create Files
Type nano followed by the filename, to create or open an existing file. Make sure that if you want to edit an existing file, you must be in the directory where the file is located. Alternatively, you can specify the full path to the file. To be able to open a file you must have read permissions to the file.
It will open a new editor window and you can write or edit it. Use arrow keys on your keyboard to move the cursor around the text. At the bottom of this window, you can find some shortcuts to use with the Nano editor. Symbol
^ means that you must press
CTRL + [Key]
(CMD + [Key] for Mac users) to use the chosen command.
To save the changes made in the file and continue editing press CTRL + O.
To exit from the editor press
CTRL + X. If any changes have been made to the currently open file, it will ask whether to save them or not otherwise it will exit right away. Input
no, and then press
Also, you can get a list of all commands by typing
You can start typing and editing the content immediately after opening the file because nano is modeless editor. In order to select text go to the beginning of the text and press
ALT + A. This will set a mark for selecting, then move over the text to be selected with the arrow keys.
To copy the selected text press
ALT + 6. This will copy text to the clipboard. To cut text press
CTRL + K.
If you want to paste text press
CTRL + U. If no text is selected before copying or pasting, it will
copy/cut the entire line.
Searching and Replacing Text
To search for a particular word or part of a text inside the editor, use the “where is” option with the
(^W). This will open a search prompt where you can type in the text you want to find. To continue to the next result, use
The search bar can also find specific line numbers. Press
Ctrl+T (^T) while in it and the line number you want to find.
You can also search with
regex (regular expressions). These represent a search pattern defined by a sequence of characters. To do so, use the
If your goal is to find and replace text, press
CTRL+W and then
CTRL+R. It will ask for the search text and the text to be replaced by. Afterward, the editor will take you to the first instance of the text and ask you whether to replace it or all of the occurrences.
Select, Copy, Cut and Paste Text
If you want to select part of a file, navigate to the starting of the text, press the
(M-A) and use the arrow keys to move over the text you wish to select. The selected text will be highlighted. If you want to cancel the selection press
Next, you can copy the selected text with the
Alt+6 or combination
(M-6) or you can cut selected text with
Ctrl+K (^K). If you use these shortcuts without selecting any text prior, it will copy or cut the entire line of text.
To paste the text move the cursor to where you want to put the text and press
Save and Exit File
To save changes made by you in a file, use the
Ctrl+O (^O) keyboard combination. It will ask you to enter a file name or confirm the name of an existing file. If the file does not exists then it will create once you save it.
To exit out of Nano, press
Ctrl+X (Nano displays it as
^X). If there is any unsaved changes it will prompt whether you want to save the changes or not.
Set Nano as the Default Text Editor
In most of Linux systems, the default text editor is set to vi. You can make nano as a default text editor by making change to
EDITOR environment variables.
Bash users can export the variables in the
export VISUAL=nano export EDITOR="$VISUAL"
You have learned how to install and use GNU nano text editor. It is a popular text editor and very straightforward for new Linux users. If you would like to learn more about GNU Nano visit the official nano documentation page.
If any question or suggestion, please leave comment below.