Brendan Dawes
The Art of Form and Code

Creating ctags for Processing .pde files for use in Vim

Ctags generate a file of language objects — such as method names and vars — so they can then be used and located inside a text editor such as Vim. When used with the Ctrl-P plug-in you can do fuzzy searches for that method name across the whole of a project and jump to it really quickly.

When it comes to Processing .pde files however, Ctags won't automatically recognise the file type (though it knows about Java file types) so after some digging I managed to generate a ctag file for my Processing projects which I can then use in Vim.

Generating the Ctags file

First of all you'll need to install Exhuberant Ctags. The best way on Mac is to install it using homebrew like this:

brew install ctags

then alias that version of ctags

alias ctags="`brew --prefix`/bin/ctags"

and finally save that alias:

alias ctags >> ~/.bashrc

You should now have ctags setup and ready to generate a ctags file for your projects.

To get this to generate a Ctag file for Processing .pde files, in the console navigate to your sketch folder and then enter:

ctags -R --langmap=java:.pde --exclude=build-tmp

This runs ctags, but tells it to use the language mapping for java on .pde files. I've also added a flag to tell it to ignore the build-tmp directory as otherwise you might get all those .class files being tracked too, which is what we don't want.

After running that command in the console you should see a file called tags sat in the same directory as your sketch.

Using the ctags file in Vim

There's a couple of ways you can use the ctags file in Vim. The first is without any need for plug-ins using the :tag command like this:

:tag draw

Which would let you junp to the draw method. You can also use regular expression so you can write:

:tag /^draw*  

You can then flick through all the methods beginning with draw by using the command :tn (for tag next).

Fuzzy Search with Ctrl-P

All that though is a little clunky. I've found the real power of ctags is when combined with the Ctrl-P plug-in which does fuzzy search not only on files but also tags. Once you have Ctrl-P installed you can issue the command :CtrlPTag and you can then fuzzy search for that method or variable across your entire project and then jump to it instantly.

I've mapped this command in my .vimrc like this so it's just a key press away:

nnoremap <leader>T :CtrlPTag<cr>
Ctrl-p view
Ctrl-p listing the tags ready to use a fuzzy search


The TagBar plug-in is another way to use the ctags file, creating a project drawer style display for all the tags in the current file, giving you a nice overview of all the methods. The problem is it'll work great with .java files but won't show anything for a .pde file. To counteract this I did a little hack that switched the syntax of the .pde file from processing to java and then switched it back after TagBar does its thing. It actually works really well.

In my .vimrc I define a key:

nnoremap <silent> <leader>t :set filetype=java<cr>:TagbarToggle<cr>:set filetype=processing<cr><C-w>l 

When I press ,t the syntax will change (so quick I don't notice), TagBar will generate the project drawer view of all the methods and then switch the syntax back to what it was previously. Finally it gives focus to the now open TagBar drawer so I can jump up and down the methods with the j and k keys. Lovely!

Tagbar view of PDE file

Automating Ctag Generation

In order to keep your tags file updated you'll need to run the ctags command each time you add a new method or alter your pde files, which is a bit of a pain. Instead there's a way using autocmd in your vimrc to call the ctag command on the command line each time a pde file is saved like this:

autocmd BufWritePost *.pde :! /usr/local/Cellar/ctags/5.8_1/bin/ctags -R --langmap=java:.pde --exclude=build-tmp 

I have to put the full path to the homebrew installed version of ctags, but you may not have to. Now every time I save a .pde file it will run the ctags command, regenerating the tags file to keep it up to date.