Est. reading time: 6 minutes
Beginner's Guide to GNU nano Text Editor - Linux

Beginners Guide to GNU nano Text Editor – Linux

If you are new to the Linux console, you might be having a hard time navigating the cumbersome pre-installed text editors. Fret not, as there is a beginner-friendly text editor called nano, which will simplify your command-line editing manifold.

nano has all the features you might need, and more. It’s very easy to install, and very intuitive to use. Unlike some of the default Linux editors like vim or ed, it doesn’t pose a learning curve for beginners.

In the following article, we will share the installation steps for the nano editor and spell check on different operating systems. We will also share a comprehensive guide on how to open, edit, and save files with it, along with its many keyboard shortcuts. Let’s begin!


How to Install nano on Debian/Ubuntu

Checking if nano is pre-installed

nano comes pre-installed on some Linux distributions. To check whether it’s present on your system, use the following command:

nano --version

If you get any output other than the version number (see below), it’s safe to assume that nano isn’t installed.

terminal

Installing nano

Installing nano is as simple as running a single command:

sudo apt-get install nano

Upon successful installation, you should get the following output:

terminal install


How to Install nano on CentOS/RHEL/Fedora

Checking if nano is pre-installed

nano comes pre-installed on some Linux distributions. To check whether it’s present on your system, use the following command:

nano --version

If you get any output other than the version number (see below), it’s safe to assume that Nano isn’t installed.

terminal

Installing nano

Installing nano is as simple as running a single command:

sudo yum install nano

Upon successful installation, you should get the following output:

terminal


How to use the nano Text Editor

Managing Files

Creating a new file

To open a blank file in the editor, you can simply use the nano command.

nano

Once the editor opens, you will see the most frequently used shortcuts at the bottom.

If you make changes to the blank file and save or exit, you will be prompted to name your file.

To exit, you simply have to press ctrl+X.

Opening an existing file

If you want to open an existing file, you can use the nano command followed by the name of the file. For example,

nano test.txt

If the file doesn’t exist, it will create and open a file named test.txt in the current directory

or

if the file exists, then it will open the file named test.txt from the current directory.

Read-only

To open a file as read-only, use the nano -v command followed by the filename. For example:

nano -v test.txt

Configuration files

To open a config file, start nano with the -w flag followed by the filename to prevent nano from wrapping text to fit your display, as wrapping can cause problems later ‘down the line’. Example below:

nano -w /etc/wireguard/wg0.cnf

Editing

The basics (select, copy, cut, paste, delete)

To select something, navigate your cursor to the beginning of the relevant text, and press alt+A. Now you can use the directional keys to traverse the text and select/deselect it.

Copy selected text: alt+6.

Cut: select and press ctrl+k. Alternatively, if you want to cut until the end of the buffer, press alt+T.

Paste: copy or cut the selected text, then navigate to the desired location, and press ctrl+U.

For deletion, there are multiple shortcuts:

  1. delete the character before the cursor: ctlr+H.
  2. delete the character under the cursor: ctrl+D.
  3. delete the word on the left: alt+backspace.
  4. delete the word on the right: ctrl+del.
  5. delete the current line: alt+del.

Undo and redo

To undo an action, press alt+U.

To redo last undone action, press alt+E

Search and replace

To start a backward search, press ctrl+Q.

To start a forward search, press ctrl+W.

To find the next occurrence in the forward direction, press alt+W.

To find the next occurrence in the backward direction, press alt+Q.

To begin replacing, press alt+R, and follow instructions at the bottom.

Activating spell check

To activate spell check, press ctrl+T.

Note: If you get an error while invoking spell check, follow these instructions:

1. Open the /etc/nanorc file:

sudo nano /etc/nanorc

2. Find the line #set speller "aspell -x -c" in the file and remove the # from the beginning to uncomment it.

3. Press ctrl+X to close and save the file.

Regular Expression-based searching and replacing

To activate RegEx-based searching, we have to edit the /etc/nanorc file:

1. Open the /etc/nanorc file:

sudo nano /etc/nanorc

2. Find the line #set regexp" in the file and remove the # from the beginning to uncomment it.

3. Press ctrl+X to close and save the file.

Now, if you open any file in Nano, and enter ctrl+\, you should be able to perform regex-based searching and replacing.

gnu

Line numbers

To turn line numbers on/off, press alt+n.

To go to a specific line number, press ctrl+_ and enter the line number.

Insert a file into another

To insert a file into another, press ctrl+R and enter the path/name of the file to insert.

Moving Around

To move backward one character, press ctrl+B.

To move forward one character, press ctrl+F.

To move backward one word, press ctrl+←.

To move forward one word, press ctrl+→.

To move to the start of the line, press ctrl+A.

To move to the end of the line, press ctrl+E.

To move upward one line, press ctrl+P.

To move downward one line, press ctrl+N.

To move to previous block, press ctrl+↑.

To move to next block, press ctrl+↓.

To move up one page, press ctrl+Y.

To move down one page, press ctrl+V.

To move to the top of the buffer, press alt+\.

To move to the end of the buffer, press alt+/.

Special movements

To go to a complementary bracket, press alt+].

To scroll up your viewpoint, press alt+↑.

To scroll down your viewpoint, press alt+↓.

To switch to the preceding buffer, press alt+<.

To switch to the succeeding buffer, press alt+>.

Miscellaneous shortcuts

To turn the mark on/off, press alt+A.

To indent a marked region, press tab.

To unindent a marked region, press shift+tab.

To turn visible whitespace on/off, press alt+P.

To enter the next keystroke verbatim (exactly as it is), press alt+V.

To refresh the screen, press ctrl+L.

To suspend nano, press ctrl+Z.

Get nano help

At any time, if you want to see a list of all available commands, press ctrl+G.

Saving Files

To save your file while editing, and without exiting, press ctrl+O.

To save and exit, press ctrl+X, and enter Y when prompted.

To save a file, and create a backup of the original contents, you have to include the "--backup" command line argument while running nano.

nano test.txt --backup

Now when you save the file, nano will create a backup of the file, using the same name suffixed with a ~. See below:

backup

Customising

Syntax Highlighting:

To facilitate programmers, nano provides syntax highlighting for certain languages. You can check the contents of the /usr/share/nano contents to see the supported languages on your system:

terminal

To add any language to your nano installation, open or create the ~/.nanorc file using:

nano ~/.nanorc

Add the following line to it:

include /usr/share/nano/sh.nanorc

(Note: This will add support for the shell scripting language. However, you can add any supported language)

Save and exit.

Now when you edit a .sh file in nano, you will see the syntactical highlighting.

gnu editor

Set nano as the default editor

Open the ~/.bashrc file using nano:

nano ~/.bashrc

Paste the following two lines inside it:

export VISUAL=nano
export EDITOR="$VISUAL"

Save and close the file.

Now run the following command:

source .bashrc

nano is now your default text editor!

Now you have a good foundation on GNU nano Text Editor. Happy Editing!

Related posts