Drupal Autocomplete in Vim with CTags

I really enjoy using Vim. It took a lot to get into using vim but I don’t think I could ever change to another editor now that I’ve learned how useful vim can be. One of the things I’ve recently discovered is how to use CTAGS for autocompletion and jumping to function declarations. When I first started learning Drupal it was a pain as I would always forget functions names or what they required, but those days are gone gone gone. Most of my knowledge came from Jon Cairn’s blog post about using Vim as an IDE so I feel obligated to mention him, some of this post will be duplicated from his.

Installation

Ubuntu

If you are running Ubuntu installation is really easy.

$ sudo apt-get install exuberant-ctags

Mac

If you are running a Mac the process involves a few more steps. I did it using brew because I find it to best package manager that I’ve tried so far.

$ brew install ctags

Then check if you which ctags you are using.

$ which ctags
/usr/bin/

If you get the following you will have to add the following line to your .bashrc file.

PATH=/usr/local/bin:$PATH

Check again to see make sure everything is working proper.

$ which ctags
/usr/local/bin

Other OS’s

You will have to Google them yourself, I’ve only worked with these two operating systems.

Building Tags File

Now change to the directory of your Drupal project. To create a very basic php tag file you can run the following

$ ctags-exuberant --languages=PHP --recurse

You should get a file called tags as and output and if you are using git you will probably want to add the tags file to your gitignore. You can check the file tags and you will see a lot of tags but the unfortunate part is most the ones you wanted got skipped. Files like .inc, .module and .theme don’t have their functions added. This following command makes sure they get included, I don’t remember where I found this but I thank the person who did.

$ ctags --langmap=php:.engine.inc.module.theme.install.php --php-kinds=cdfi --languages=php --recurse

Now your tags file will include all the Drupal files. Now when you want to use the tags file you either have to start vim from the same directory as the tags file or add it to your vimrc. I like to be at root directory when using vim so I don’t have to worry. Now you can see if everything worked by typing out a drupal function and then press . Once you get the function name complete you can try <c-]> for jumping to the function declaration. Happy vimming!