Exhuberant Ctags in Vim for PHP on Ubuntu 8.10
Brian Dailey is a LAMP-stack developer with a wide range of experience in the development world. Get in touch!
For more articles on the development trade, see the Blog.
Function auto-completion in vim is ordinarily limited to the files that you have loaded into tabs or buffers. This is usually sufficient for me, but lately I've decided to set up exuberant ctags so auto-completion can be used project-wide. I also use CakePHP for most LAMP-stack projects that I work on, and I figured it would be nice to jump straight to a function to check arguments rather than relying on the online documentation.
Installing exhuberant-ctags on Ubuntu is easy as usual:
$ sudo apt-get install exhuberant-ctags
Next, create a directory in your vim settings directory to store your tag files for different projects:
$ mkdir ~/.vim/mytags
Now you can jump to your project base directory and compile a tags file. In this case, we're going to generate a tags file for CakePHP. Your installation directory will, of course, vary. We're also going to use a command that Matthew Weier O'Phinney helpfully fleshed out to make sure we include all PHP functions. I suggest creating a shell script file so you can automate this task for multiple projects.
$ cd /opt/cakephp-1.2/
$ ctags-exuberant -f ~/.vim/mytags/cake -h ".php" \
-R --exclude="\.svn" --totals=yes --tag-relative=yes \
--PHP-kinds=+cf --regex-PHP='/abstract class ([^ ]*)/\1/c/' \
--regex-PHP='/interface ([^ ]*)/\1/c/' \
--regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/'
Now when you are inside a CakePHP project you can type this into vim to access CakePHP's ctags file.
:set tags=~/.vim/mytags/cake
You can now auto-complete CakePHP functions and variables, as well as jump to them directly. For example, if you're in a controller and you're using $this->Session->setFlash, move the cursor over "setFlash" and press Ctrl + ]. This will open /cake/libs/session.php and put you in function setFlash. You can use Ctrl + T to jump back.