Skip to content

Instantly share code, notes, and snippets.

@edubkendo
Created June 9, 2012 15:11
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edubkendo/2901380 to your computer and use it in GitHub Desktop.
Save edubkendo/2901380 to your computer and use it in GitHub Desktop.
Ctags, Sublime Text, Coffeescript

Ctags with Sublime Text and Coffeescript

Get Ctags

Step one is to install Exuberant Ctags on your system. For those on an Ubuntu system, this is as simple as:

sudo apt-get install ctags

Get the Sublime Text Plug-in

Next, getting the ctags plugin for Sublime Text is similarly easy. If you are using Package Control -- And you really, really should be using Package Control -- just fire up Sublime Text, hit ctrl+shift+P to open the command palette, type 'install', then select Package: Install. From there search for "ctags", Make sure you pick the one called "CTags", not the other one which is for PHP users.

Once this is installed, you'll want to set up the keybindings. If they dont work out of the box (they didn't for me, may have been some conflicts with other packages), go to Preferences/Browsepackages. In there, open up the CTags folder, open the file Default.sublime-keymap with Sublime, then copy everything between the []'s. Again go to Preferences, but this time, pick Keybindings-User. Now, if you already have keybindings in this file, just tack these on at the end, before the last bracket. I found that Sublime wasn't happy and kept messing it up unless I first pasted it in, then went back and added the trailing comma after my older keybindings. If you have nothing else in your Keybindings-User file, the paste these in between the []'s and save.

Now for Coffeescript

In your $HOME folder, create a file called .ctags and open this in Sublime Text. Now paste this into it:

--langdef=coffee
--langmap=coffee:.coffee
--regex-coffee=/(^|=[ \t])*class ([A-Za-z]+\.)*([A-Za-z]+)( extends [A-Za-z.]+)?$/\3/c,class/
--regex-coffee=/^[ \t]*(module\.)?(exports\.)?@?([A-Za-z.]+):.*[-=]>.*$/\3/m,method/
--regex-coffee=/^[ \t]*(module\.)?(exports\.)?([A-Za-z.]+)[ \t]+=.*[-=]>.*$/\3/f,function/
--regex-coffee=/^[ \t]*([A-Za-z.]+)[ \t]+=[^->\n]*$/\1/v,variable/
--regex-coffee=/^[ \t]*@([A-Za-z.]+)[ \t]+=[^->\n]*$/\1/f,field/
--regex-coffee=/^[ \t]*@([A-Za-z.]+):[^->\n]*$/\1/f,static field/
--regex-coffee=/^[ \t]*([A-Za-z.]+):[^->\n]*$/\1/f,field/
--regex-coffee=/(constructor: \()@([A-Za-z.]+)/\2/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){0}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){1}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){2}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){3}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){4}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){5}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){6}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){7}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){8}/\3/f,field/
--regex-coffee=/(constructor: \()@[A-Za-z.]+(, @([A-Za-z.]+)){9}/\3/f,field/

I got this from this gist and while it isn't perfect, it works much better than any of the others I was able to find, including the first two I had originally posted in this entry.

Putting it all together

Now, in your terminal, navigate to the directory of your project or any other directory with lots of coffeescript files in it that you want to be able to hop around in easily, and paste this:

ctags -R -f .tags

This will create a file in your directory called .tags, which indexes the location of all those juicy functions, variables, etc that you want easy access to. Now, within any file in your project, type the name of a function you'd like to find, and with your cursor within or at the end of this word, hit ctrl+alt+] and Sublime Text will give you a list of all the places where that function(or etc) occurs. Pick one and watch as your instantly taken to it. Searches do seem to be case-sensitive, so if your not getting anything, check your case and try again.

Pro-tip: for function or class names seperated by a ".", like 'App.UsersController', first try just the last part, "UsersController". If that doesn't catch it, simply highlight the full phrase before using your key combination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment