Vim is an advanced text editor that seeks to provide the power of the de-facto UNIX editor 'vi', with a more complete feature set.
Vim Plugin for Drupal
The Vim Plugin for Drupal (available on Drupal.org) provides the configuration information below along with additional goodies and hints for Drupal 6, 7, and 8.
The following commands will indent your code the right amount, using spaces rather than tabs and automatically indent after you start. The commands should be added to a .vimrc file in your home directory (~/.vimrc), you may need to create this.
If you would prefer not to have all your vim sessions use these settings, you can activate them in specific files using a "modeline". That is, within the first or last 5 lines of the file you are editing, add the following line:
// vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:
Note that not everyone uses vim to edit files, so this line is for your convenience and may be removed prior to submitting changes. For more information on how to use modelines, type
:help modelinein vim or gvim.
If you enjoy syntax highlighting, it may be worth remembering that many of Drupal's PHP files are *.module or *.inc, among others.
Vim seems to syntax highlight *.inc files properly by default but doesn't know that some other files are PHP content. For *.module and *.install, use this snippet in .vimrc:
" Drupal *.module and *.install files.
autocmd BufRead,BufNewFile *.module set filetype=php
autocmd BufRead,BufNewFile *.install set filetype=php
autocmd BufRead,BufNewFile *.test set filetype=php
autocmd BufRead,BufNewFile *.inc set filetype=php
autocmd BufRead,BufNewFile *.profile set filetype=php
autocmd BufRead,BufNewFile *.view set filetype=php
Omni completion provides smart autocompletion for programs. When invoked, the text before the cursor is inspected to guess what might follow. A popup menu offers word completion choices that may include struct and class members, system functions, and more. A similar feature in Microsoft Visual Studio is known as IntelliSense. To enable OmniCompletion for PHP just add this to you .vimrc
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
OmniCompletion can also use a ctags file to provide completion for drupal functions (not only the the core PHP ones). You can use exuberant ctags to generate this file (on debian/ubuntu you can install it with "apt-get install exuberant-ctags"). From the root of your drupal folder you can run (taken from koumbit blog):
ctags --langmap=php:.engine.inc.module.theme.install.php --php-kinds=cdfi --languages=php --recurse --fields=+l
With this file in place, vim will load automatically it if you open your files from drupal's root directory or using this command from within vim:
Once the file is loaded you can use:
- Ctrl-X Ctrl-O completion options after the first characters of an identifier (class, function, variable name)
- Ctrl-] to jump to a function definition
- Ctrl-T to move to the previous file
The Vim Plugin for Drupal takes care of setting the
'omnifunc' options as above. It also comes with tag files for Drupal core versions 6, 7, and 8, and it provides a drush command as a wrapper for
ctags. See the project page for a screenshot of OmniCompletion in action.
You can also install the YouCompleteMe plugin (https://github.com/Valloric/YouCompleteMe) that offers automatic auto complete (without having to use a keyboard shortcut to activate it).
Using these settings only with Drupal
- Copy your .vimrc to .vimrc-drupal
- Append these settings to the end
vim -u ~/.vimrc-drupal
To make this easier (using bash on UNIX or Linux), you could create an alias by typing:
alias vid="vim -u ~/.vimrc-drupal"
This allows you to just use the vid command instead of vi when you want to edit a Drupal file.
Note: To make this alias permanent add the above line to the .bashrc file in your home directory (*nix). More on the alias command
Also be aware that the -u option turns on 'compatible' (as in, compatible with vi) so you must put 'set nocompatible' at the top of your .vimrc-drupal otherwise you may get errors.
Using the snippetsEmu package for Drupal
There is a script which provides similar snippets functions to TextMate: http://www.vim.org/scripts/script.php?script_id=1318
Just copy the php_snippets.txt to your ~/.vim/after/ftplugin/php_snippets.php and overwrite the old one
Now you can type fieldset and you get a definition for the fieldsets, if you just want to add single lines add for example #collapsed
Using the snipMate package for Drupal
There is another plugin which allows you to easily use and create snippets: http://www.vim.org/scripts/script.php?script_id=2540 . There are currently two projects that provide drupal snippets:
- D6 mixed snippets: http://github.com/theunraveler/Drupal-Snippets-for-Vim
- Snippets for D6 hooks and many D6 core functions: https://github.com/agileadam/drupal6snipmate
- Vim snippets for Drupal 7: https://github.com/tanarurkerem/drupal-snippets. Please refer the README file for detailed installation instructions.