How to Install and Use Nano Text Editor in Linux

Install and Use Nano Text Editor in Linux

The Nano text editor is a popular command line text editing program that comes standard on most Linux distributions. Known for its simple, easy to use interface, Nano is a great choice for Linux beginners who want to edit configuration files or write documents without learning complex shortcuts or commands.

In this comprehensive guide, we’ll cover everything you need to know to start using Nano effectively, from installation to basic usage and advanced features.

Installing Nano

Nano is included by default on most Linux distributions, so there’s a good chance you already have it installed. To confirm, open your terminal and type:

nano --version

If Nano is installed, you’ll see the version number displayed. If not, use your package manager to install it:

Debian/Ubuntu

sudo apt update sudo apt install nano

RHEL/CentOS

sudo yum install nano

Fedora

sudo dnf install nano

Arch Linux

sudo pacman -S nano

With Nano installed, let’s go over the basics of using this popular text editor.

Creating and Opening Files

To open an existing text file with Nano, simply pass in the filename as an argument:

nano file.txt

If the file doesn’t exist, Nano will automatically create it when you start typing.

You can also specify a new filename to directly create a new file:

nano new-document.txt

Navigating and Editing Text

The Nano interface shows the filename at the top, the text editing area in the middle, and common keyboard shortcuts at the bottom. Use the arrow keys to move the cursor around and start typing to insert text. Here are some basic editing shortcuts:

  • Ctrl + K – Cut current line
  • Alt + 6 – Copy current line
  • Ctrl + U – Paste the copied text
  • Ctrl + \ – Search/replace

Nano has multi-level undo and redo to easily revert changes:

  • Alt + U – Undo last action
  • Ctrl + Alt + U – Redo last action

To select text, hold down the Shift key while using the arrow keys.

Saving and Closing Files

When you’re done editing, save the file with Ctrl + O. Nano will prompt you to confirm the filename to save the contents.

To close the file or exit the editor, use the Ctrl + X shortcut. If you haven’t saved your changes, Nano will ask if you want to save the modified buffer.

Advanced Nano Usage

Now that you know the basics, let’s go through some of Nano’s advanced features to level up your text editing skills.

Syntax Highlighting

Syntax highlighting displays code and markup in different colors and fonts depending on syntax rules and programming languages. This makes code easier to read and edit.

To enable syntax highlighting in Nano, edit the ~/.nanorc file in your user directory:

nano ~/.nanorc

Here you can add include rules to load highlighting for different file types. For example:

include "/usr/share/nano/html.nanorc"
include "/usr/share/nano/c.nanorc"

Save the file and open a syntax-highlighted document:

nano index.html

You should now see the file rendered in color with HTML tags and elements highlighted.

Line Numbers

Line numbers help when editing code or reviewing logs. To display line numbers in Nano:

nano ~/.nanorc

Add line numbers rule

set linenumbers

Line numbers will now display next to each line.

Soft Line Wrapping

By default, Nano will hard wrap lines that exceed the width of your terminal window. To keep long lines visually together, enable soft line wrapping by adding this rule to .nanorc:

set softwrap

Now lines will wrap visually but stay together as a single line.

Auto-Indentation

Proper indentation makes code and markup documents much easier to read at a glance. Turn on auto-indent in Nano to have the cursor move to the proper position when starting a new line:

set autoindent

Now when you press Enter, the new line will start at the same indentation level as the previous line.

Tab Completion

If you regularly open certain files, enable tab completion to quickly autocomplete filenames by pressing Tab.

nano ~/.nanorc

Add filename completion rule:

set tabsize 2
set tabstospaces
set fill 72
set historylog
set multibuffer
set suspend
set filenamecompletion

Now when opening files with Nano, you can type part of the name and press Tab to autocomplete.

Mouse Support

For easier text selection and navigation, enable mouse support in Nano:

set mouse

You can now quickly place the cursor by clicking in the editing area instead of using just arrow keys.

Customizing the Interface

To make Nano easier on the eyes, change interface elements like the color scheme. Add this to .nanorc for a more visible black on white display:

set titlecolor brightwhite,brightred
set statuscolor brightwhite,brightred 
set numbercolor brightwhite,brightred
set selectedcolor brightwhite,brightred
set stripecolor ,brightgreen  
set gaugecolor ,brightgreen
set indicatorcolor brightwhite
set selectedtextcolor brightred
set title "Nano Text Editor"

The Nano documentation lists additional customization options for the interface.

Tips and Tricks

Here are some helpful tips for getting the most from the Nano editor:

  • Use Ctrl + G to view help menu with list of shortcuts
  • Ctrl + C shows current cursor position and line/column number
  • Ctrl + _ to undo last action
  • pageUp and pageDown to scroll without moving cursor
  • Ctrl + W to search for text in file
  • Alt + A to mark text as append-only to avoid accidental edits
  • ^ symbol shows location of last edit
  • Enable constantshow in .nanorc to always show cursor position

Conclusion

While Nano lacks some advanced features of heavier editors, its simplicity and ease of use make it a staple for editing files and documents in Linux.

With syntax highlighting, spell checking, auto-indenting, and other options covered here, you can tune Nano to be a surprisingly robust editor for everything from config files to source code.

Integrating with external commands and utilities also demonstrates the flexibility of Nano to adapt to advanced workflows.

So the next time you need to quickly edit some text or code in Linux, don’t overlook the humble Nano editor. With a little practice, you may find it’s the only editor you need!

Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts