Using Ctags with Nano (and other editors)

Share

Introduction


Ctags is a pretty useful utility to aid navigating source code. It creates a database of function/variable/type definitions within your code.
Vi and Emacs support using a Ctags database, Nano (along with many other editors) does not.
I have implemented a script which reads the database and finds the appropriate file and line to open the editor at.
This post describes the installation and operation of this utility.

Unfortunately, since Nano seems to lack any sort of plugin interface, adding support for finding a tag highlighted within the editor is more work than I am willing to do. However, finding a tag specified on the command line is sufficient for my needs.








Usage

Firstly you’ll need to create the ctags database. Running the following command in your source directory should do the trick.

ctags -R

For more advanced options to ctags, I suggest reading the manual.

Download the script from here, install it somewhere in the path and make it executable. Eg:

sudo wget -O /usr/local/bin/nanoctags.sh http://www.sassan.me.uk/projects/nanoctags/nanoctags.sh
sudo chmod +x /usr/local/bin/nanoctags.sh

#optionally you can symlink it to a shorter name, I'm using "tag"
sudo ln -s /usr/local/bin/nanoctags.sh /usr/local/bin/tag

Now you may run “nanoctags.sh <sometag>” (or “tag <sometag>” if you created a link) to open nano at the point where it is defined.

First the script attempts to find the tags database. You may specify one on the command line (eg: “nanoctags.sh <sometag> <dbfile>”, in the environment variable $TAGFILE, or let the script locate it for you. It searches the current directory and the parent directories until it finds one.

Next it locates the relevant entry in the database. If you are not in the root of your source directory then it will search first for entries within your current directory, and then each of its parent directories until it reaches the folder containing the tags file. This means that if there are multiple definitions of the same name then it will try to find the one closest to where you are (and therefore hopefully the one you are looking for).

Once the most appropriate entry is found, it is parsed and used to determine the correct filename and line number.

Finally, Nano is opened at the correct location.

Bugs


Probably many, but it successfully worked with every test I could think of.

Updates


Updated the script so that it only reads the tags file once. This seems to improve performance significantly :)



Share

Tags: , , , , ,

Leave a Reply