Write a post

Linux Terminal Text Editor: Getting started with nano

Published Dec 27, 2016Last updated Jan 18, 2017
Linux Terminal Text Editor: Getting started with nano

Background

Working with Linux servers on a daily basis involves modifying services, tweaking configurations, fixing bugs, and other sysadmin tasks are some tasks that need to be done using some kind of text editor. It can get confusing when you're trying to decide which is the best text editor to use. And with the rise of control panels, people can use the respective control panel GUI text editor or use the Linux terminal and the text editors that the OS has available.

In this article, we will cover the latter category of users, the ones that work with Linux terminal text editors. Their usage differs. And with Vim and Emacs competing for the top spot, many Linux enthusiasts still find them hard to use, which leads to an unwillingness to learn. When this happens, they find comfort in a "simpler" text editor such as nano.

Introduction

Nanos' Wikipedia page states that nano emulates the Pico text editor and was created in 1999 with the name TIP (This Isn't Pico). It's creator, Chris Allegretta, says that his motivation was to create a free software replacement for Pico, since it was not distributed under a free software license. Later the name was changed to NANO to avoid a naming conflict with the existing Linux utility tip. In 2001, NANO was added to the GNU project.

Nano implements features that Pico lacks. Features such as colored text, search and replace regular expression, multiple buffers, rebindable key support and smooth scrolling. And it's way more user-friendly than most of the advanced text editors, which is why most beginners tend to use Nano.

How to install nano

Nano is included with some Linux distributions by default, however on systems where it is not present it can be installed easily using the OS package manager.

  • Debian/Ubuntu users can use the below command to install nano:
# apt-get install nano
  • CentOS/Fedora users can install nano with:
# yum install nano
  • Arch Linux users:
# pacman -S nano

Running and using nano

Nano can be used differently. You can open or create a file with a command such as the one below:

# nano /var/opt/config.php

This command starts nano and opens or creates a config.php file. When you receive an output on the screen you can see if the file already exists or if you are creating it. If it is a new file, at the bottom of the screen you will see [ New File ]. If it's an existing file, you will some text inside it.

As the help text for nano says, the nano editor is designed to emulate the functionality and ease-of-use of the UW Pico text editor. There are four main sections of the editor. The top line shows the program version, the current filename being edited, and whether or not the file has been modified. Next is the main editor window showing the file being edited. The status line is the third line from the bottom and shows important messages. The bottom two lines show the most commonly used shortcuts in the editor.

To open a file at a specific line, use this syntax:

# nano +LINE,COLUMN File

Using the v argument will open a file as read-only:

# nano -v file

When you edit files that are used to configure apps or other utilities, you can use the -w argument to disable wrapping of long lines. How will this be of use? Well, it will prevent wrapping lines that are too long to fit on your screen and you will avoid problems if the config directives are saved across multiple lines.

Functions in nano such as saving, justifying, moving etc. are referred to as "shortcuts".

Once you have a file opened with nano, hit CTRL+G to see a list of available shortcuts.

Do not be confused with the capital letters that represent the shortcut. You can use lowercase letters when shortcutting. Exit the help window using Ctrl+X or F2.

Now that you are more familiar with nano's shortcuts, let's talk about navigating through nano. Besides the obvious use of Home, End, Page Up and Page Down, you can move throughout the text using the arrow keys.

  • To move the cursor forward or backward, use Ctrl+f and Ctrl+b respectively. This will move forward or backward one letter at a time. If you want to move the cursor one word at a time, use Ctrl+Space. To do this backwards, use Alt+Space.
  • To search a word or some specific text, use Ctrl+w, enter the word and press Enter. If the text contains the word you are searching for, you will be directed to the line that contains it. Using Alt+w will search the same word if it is present in multiple lines.
  • To cancel a command use Ctrl+c.
  • To quit nano use F2 or Ctrl+x.

Copy/Paste/Cut

In graphical user interfaces, you copy text by highlighting it with a cursor either with your mouse or keyboard. With nano, you can mark text using Ctrl+^. The marking is done with the arrow keys. Once you hit Ctrl+^, move the cursor to where you want to start and finish the marking, then use Ctrl+^ to finish it.

You can also highlight text backwards.

While the text is marked, to copy it hit Alt+^. To cut it hit Ctrl+k.

Now that you have that specific word or text copied, paste it by using Ctrl+u into a position of your choice.

Find and replace

What if you want to find and replace text? Simple, use Alt+r, enter the text you want to be replaced in the first prompt and the text value you want to replace the previous text within the second prompt. If there are multiple instances of your searched text, you will be asked whether you want to replace the highlighted instance only or all text.

Once you're done with editing a file and you need to save it, press F2, enter y for YES and press Enter.

Conclusion

These tips and shortcuts that we've covered are just a glimpse of what nano has to offer. You can use nano's main page and see which shortcuts or arguments are appropriate for your needs. In your terminal, run:

# man nano

This will open the manual page for the nano command.

Most users find nano extremely handy and easy to use. Its simplicity is what draws people to use nano instead of vim. So next time when you're modifying text in some files from your terminal, use nano. You'll love it. If you are an intermediate user or if you want to experiment with other text editors, use Vim. It's a great alternative to nano and it has a lot of useful features.


Author's Bio

Andy Nelson is a professional Linux system administrator for more than 8 years at RoseHosting. He loves working with different Linux distros and experimenting with various hardware setups. In his spare time, he works with various JavaScript frameworks and likes to try all the new and trendy frameworks.

Discover and read more posts from Codementor Team
get started
Enjoy this post?

Leave a like and comment for Codementor

Subscribe to our weekly newsletter