Drupal Autocomplete in Vim

Vim Logo

I recently stumbled across a handy tutorial on configuring a Drupal dictionary in Vim for autocomplete. I also remembered helping a friend out with a similar problem, Drupal syntax highlighting for Nano. I then combined the two and have a simple to follow tutorial for generating your own drupal 6 dictionary for vim autocomplete!

[adsense:468x60:4496506397]

Step 1… Create a dictionaries folder if you don't already have one…

mkdir -p ~/.vim/dictionaries/

Step 2… Generate your dictionary… Run the following from your Drupal install…

grep "^function" modules/ includes/ -hR | gawk '{ sub(/\(.+/, "(", $2); print $2 }' | sort -u > ~/.vim/dictionaries/drupal6.dict

This will search the modules and includes folders recursively for all lines starting with "function". Then, via some gawk and tr magic, we end up with a list of functions that will be dumped into our dictionary file!

Step 3… Configure Vim to add the new dictionary file on load up…

vim ~/.vimrc

then add…

set dict +=~/.vim/dictionaries/drupal6.dict

Step 4… There is no step 4. You're done!

When editing your code (in INSERT mode), press Ctrl+X and then Ctrl+K to invoke the auto-complete menu.

Brilliant :-)

Comment Icon

6 Comments

The most recent comment was on Sun, 18th Mar 2012 - 13:11

This is very very cool!

Thank you so much! I exclusively use vim (because I just can't get around all those graphical IDEs) and this will help a lot!

this is great. thanks for sharing.

is there a way to include the parameters of a function as well? for example the link function takes: l($text, $path, $options = array()).

obviously your script could be updated to include this, but is there a way to get it working with the vim dictionary? i tried this manually but inserting parameters for a function in the dictionary file and it didn't work. i'm new to vim so imagine that the dictionary is limited to the function name but don't know. i'll keep looking and post if i find anything. in the meantime any insight would be appreciated. thanks.

That's a good question; I'd be inclined to map to a different key combination as an easy fix.

Add new comment

Filtered HTML

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • You can enable syntax highlighting of source code with the following tags: <code>, <pre>, <bash>, <css>, <html>, <js>, <jquery>, <mysql>, <php>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.