Syntax Highlighting in nano

Trying to edit certain files using nano?  Looking for better syntax highlighting?  Been advised that you can clone a github repo and ‘just’ install it?  Not sure how to proceed?  Read on.

The GNU nano text editor is installed on a staggering number of Unix-based systems.  It’s light and friendly.  It’s also smart enough to highlight the syntax in a wide array of different types of files.  This makes life easy.  We like easy.

Unfortunately, developing your own syntax files is not easy.  So we search the web looking for an easy way to give us more syntax highlighting options.  Maybe you came across this article on Linux Magazine like I did?

If you find the syntax highlighting functionality in the nano text editor lacking, you will appreciate a collection of .nanorc files in the nanorc GitHub repository.

Here, you’ll find a selection of definition files for a broad range of programming languages: from Python and HTML, to Lua and Markdown. To add all this goodness to nano, clone the GitHub repository using the git clone https://github.com/nanorc/nanorc.git command, and then install the files by running the make install command. That’s all there is to it.

Some comments were:

C1:  Nope, not that simple. Good post but linux newcomers like me, need a little more than explanation than this. By default there is no .nano folder in the user home directory for Ubuntu 14. So not quite as straightforward as I hoped.

C2:  100% agreed.

So, for the benefit of all the “Linux newcomers” out there, I thought I’d just document an exact, step-by step process, that you can follow to clone the github repository and install the nano syntax highlighting files on your system.  For a ‘typical’ system, I’ll use Ubuntu 16.04 — your system can, and probably will, be different, but this will flesh things out nonetheless and should help you on your way.

Ready?

Step 1 — Right click your desktop and select ‘Open Terminal‘ from the pop-up window.  Enter the following commands at the command prompt (probably a ‘$’):

Step 2 — $ sudo apt-get install git

sudo means “superuser do” and is a way of running a command with elevated privileges.  You will be prompted for the superuser password.  Enter it to continue.

apt-get is part of the Advanced Packaging Tool suite of programs that Ubuntu users commonly use to install software on their systems.  It makes installation easy.

install instructs apt-get that you want to install a package.

git is the name of the package you want to install.  git is a package that makes installing software from github easy.  git is not installed by default on 16.04.

Step 3 — $ mkdir ~/git

If you’re relatively new to the whole Linux thing, you probably haven’t developed a sense for organising the file system yet.  Now that you’ve got git installed, and once you see how easy it is to use, you may be tempted to download and use a whole lot of software with it.  That software has to go somewhere.  If you create a git directory in your home directory, you can put all of that software in one place (instead of scattered all over the place).  A little bit of organisation goes a long way towards staying sane.

Step 4 — $ cd ~/git/

By default git will save the software in the current directory so, having created a directory for git, you now change into it.

Step 5 — $ git clone https://github.com/nanorc/nanorc.git

git is the software you installed in Step 2.

clone means you want to make an exact copy of something that is on github and save it on your computer.

https://github.com/nanorc/nanorc.git is the location of the github repository that you want to download.  Every ‘repo’ has one.  If you go to https://github.com/nanorc/nanorc, for example, you’ll see a green button marked “Clone or download”.  Click it to get the URI for the repo.

nanorcOnGithub

(Note: If you don’t have a github account, you’ll see something slightly different.  The button is still there, though.  You do not need to create a github account.)

Upon executing the command, git will connect to github and download all of the software required for nanorc.  Since the name of the repository is ‘nanorc’, git will automatically create a new directory (within the current directory) called ‘nanorc’ and put all of the software inside of that.

Step 6 — $ cd nanorc/

Change into the directory git just created.

Step 7 — $ make

make is a program that looks for a ‘Makefile’ in the current directory.  The Makefile contains instructions on how to build/assemble/compile the source software into binaries/executables/programs/etc.

Step 8 — $ make install

install tells make to actually move the things that it created in Step 7 into the right place(s) on your system — to install them.

Step 9 — $ ls -l ~/.nano/syntax/

Gives you a listing of the directory where your new syntax highlighting files were installed.

Step 10 — $ nano ~/.nanorc

The default location for syntax highlighting files (on Ubuntu 16.04) is /usr/share/nano/ but now that you have a new set installed, you need to tell nano to use those instead.  Add the following line to nano’s configuration file:

include ~/.nano/syntax/*.nanorc

The ‘*’ means to use all of the syntax files in that directory.  So you’re switching from using all of the default syntax files already on your system to all of the new syntax files from the nanorc repo.

You don’t need to do that, if you don’t want to — you can specify individual syntax files on separate lines if you want:

include ~/.nano/syntax/xml.nanorc
include ~/.nano/syntax/ruby.nanorc
include ~/.nano/syntax/lua.nanorc
include /usr/share/nano/c.nanorc
include /usr/share/nano/java.nanorc
include /usr/share/nano/python.nanorc

Mix and match the old and the new as you please…

That’s it.  You’re done!

Start editing files and trying out the new syntax highlighting and, if you like it for a particular type of file, use it.  If not, go back to the default for that particular type, or maybe hunt down another source of syntax highlighting files (from github or elsewhere) and give them a go.

I hope this has been helpful — enjoy!

Advertisements
This entry was posted in Stuff and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s