Code completion for Drupal in Vim

31Jul2009

I am not a Vim guru, but it is my editor of choice. I get stuck on a lot of remote servers and it just is too much for my peanut brain to handle switching back and forth from a command line interface to a Gui just to write some code. It fits. I like it.

I recently have started learning Python, and as such have revisited my .vimrc file and added bunches of new stuff. I already had code completion for php on, so I enabled it for python as well, and then got to thinking. Why can't I get code completion for Drupal? It's a php file. Some digging into the Vim doc told me that it uses an external format generated by a program called ctags to parse projects and add to the function list for code completion. ... ... !!! If I grok that correctly, vim can code complete for /any/ project with 2-3 commands. So I tried it.


cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal co -d drupal6 -r DRUPAL-6 drupal
ctags -R -o drupal6.tags --langmap=php:.module *

Then you need to load the ctags file into vim (can be added to .vimrc)
:set tags=drupal6.tags

And voila, code completion for every function, variable and constant in Drupal core. What I lack yet, and am still working on is code hinting for hooks. For example, hook_block is never actually defined in Drupal core, rather it is implemented on a module by module basis from functions such as user_block(). But as it stands this is pretty neat.

Blog entry Vim code completion for Drupal has been updated.
Vim code completion for Drupal

* View
* Edit

Fri, 07/31/2009 - 17:13
Submitted by Sam Tresler

I am not a Vim guru, but it is my editor of choice. I get stuck on a lot of remote servers and it just is too much for my peanut brain to handle switching back and forth from a command line interface to a Gui just to write some code. It fits. I like it.

I recently have started learning Python, and as such was revisiting my .vimrc file and adding bunches of new stuff. I already had code completion for php on, so I enabled it for python as well, and then got to thinking. Why can't I get code completion for Drupal? It's a php file. Some digging into the Vim doc told me that it uses an external format generated by a program called ctags to parse projects and add to the function list for code completion. ... ... !!! If I grok that correctly, vim can code complete for /any/ project with 2-3 commands. So I tried it.

cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal co -d drupal6 -r DRUPAL-6 drupal
ctags -R -o drupal6.tags --langmap=php:.module *

Then you need to load the ctags file into vim (can be added to .vimrc)
:set tags=drupal6.tags

And voila, code completion for every function, variable and constant in Drupal core. What I lack yet, and am still working on is code hinting for hooks. For example, hook_block is never actually defined in Drupal core, rather it is implemented on a module by module basis from functions such as user_block(). But as it stands this is pretty neat.

UPDATE: So with some help from Neil Drumm on the devel mailing list, this new method just snags the hooks. In Drupal 7 these will be in .api.php files and won't require an additional checkout

$ cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal-contrib co -r DRUPAL-6--1 -d drupal6 contributions/docs/developer/hooks
$ ctags -R -o drupal6.tags hooks
$ mv drupal.tags ~/.vim

(make the .vim directory if you need to)

Then add this to ~/.vimrc

if has("autocmd")
" Drupal *.module and *.install files.
augroup module
autocmd BufRead,BufNewFile *.module set filetype=php
autocmd BufRead,BufNewFile *.install set filetype=php
set tags=.vim/drupal6.tags
augroup END
endif

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <img> <div><blockquote><i><b>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Prove you aren't a machine:
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.