Tuesday, 28 April 2009

5 Popular Tricks to Customise Nano Editor


Nano is a popular and user-friendly text editor for console which is mostly used for editing quickly configuration files, sources or various other text files. It does not compete with advanced development environments like Vim or Emacs, but it's fast and easy on resources.

Here are five (popular, I hope) tips for customising Nano and changing its default behaviour.

1. Enable auto-indentation
When Nano starts, it reads a system wide configuration file which is usually located inside /etc/nanorc and executes the commands located inside it, then it searches for the user configuration file, located in ~/.nanorc, where ~ is the user's home directory. To configure Nano for your user, you only need to edit the ~/.nanorc file. Notice that since this is a hidden file, you may have to turn on hidden file view in your file manager, or use ls -a in a terminal.

Edit the ~/.nanorc configuration file (create it if it doesn't exist; ~ is your home directory) and add the following line:

set autoindent

This will turn on auto-indentation for a new line to the same level of the previous line. Alternately, you can run Nano with the -i switch:

nano -i

Or just type Alt+I inside Nano when it's running.

2. Enable syntax highlighting
Edit the ~/.nanorc configuration file and add lines which go like this:

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

Default Perl syntax highlighting in Nano

Several other default highlighting rules are found inside the /usr/share/nano/ directory. I wrote a full tutorial about enabling syntax highlighting in Nano here.

3. Turn off the creation of backup files
By default, Nano creates backup files with names like filename~ after editing a file. To turn this behaviour off, edit the ~/.nanorc file and add the line:

unset backup

To turn backup files on again, use set backup in the configuration file.

4. Don't add newlines at end of files
By default, Nano will add a newline character at the end of a saved text file. If you want this disabled, add the following option inside ~/.nanorc:

set nonewlines

5. Change tab size
Nano uses by default 8 columns for the tab size. In order to change it to another value, say 4, use:

set tabsize 4

Or some other value depending on your needs.

These are several settings I think are among the most popular. For a complete manual, use man nano, man nanorc and the official online manual.

5 comments:

Ikon said...

Thanks for this valuable tutorial! ^_^

Grant said...

One "feature" that really annoys me about nano is how it likes to automatically wrap long lines. How can one turn that option off?

Anonymous said...

Grant, add 'set nowrap' to your ~/.nanorc.

em4r1z said...

If you disable the addition of a new line at the end of the file, some system files won't work. xorg.conf, for example.

Anonymous said...

Thank you. This is very useful!