Showing posts with label nano. Show all posts
Showing posts with label nano. Show all posts

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.

Thursday, 9 April 2009

How-To: Enable Syntax Highlighting in Nano

Nano is a popular, user-friendly text editor which can be ran in a shell, without the need of an X Server. It can be very useful especially for editing text files (like configuration files or scripts) in a fast manner, being faster than more advanced, graphical text editors.

Nano has syntax highlighting support for programming languages, which is disabled by default. However, Nano provides default rules for several languages like Perl, Python, or C, to name a few. These highlighting definitions are kept inside the /usr/share/nano/ directory, and for each language there is available a file with rules.

For example, ls /usr/share/nano will output the following on my system, which has Nano 2.0.7 installed:

$ ls /usr/share/nano/
asm.nanorc groff.nanorc java.nanorc mutt.nanorc nanorc.nanorc perl.nanorc python.nanorc sh.nanorc
c.nanorc html.nanorc man.nanorc nano-menu.xpm patch.nanorc pov.nanorc ruby.nanorc tex.nanorc

These are default syntax highlighting rules for several languages, like Java, Perl, Python, Bash, C, HTML or Ruby.

In order to enable syntax highlighting in Nano based on one of these files, edit the ~/.nanorc file, where ~ is your home directory. Notice that you will have to create this file if it doesn't already exist. Then add lines to it in this fashion:

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

And so on for each language you want to have syntax highlighting enabled. Those four lines will include the files c.nanorc, perl.nanorc, sh.nanorc and html.nanorc, which will define highlighting rules for C, Perl, Bash and HTML respectively. This can also be accomplished by copying the contents of one of those files directly into ~/.nanorc. For example:

cat /usr/share/nano/c.nanorc >> ~/.nanorc

The above command will append the contents of /usr/share/nano/c.nanorc at the end of ~/.nanorc, enabling syntax highlighting for C. You can also search on Google for other rules, if the default colours don't fit well.

Syntax highlighting for C in Nano

Notice: This tutorial is a later, revised version of the tutorial which I initially published here.