Configuring vim

Last updated on
September 30, 2016 - 13:41

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.

Indentation

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.

set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent

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 modeline in vim or gvim.

Syntax highlighting

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:

if has("autocmd")
  " Drupal *.module and *.install files.
  augroup module
    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
  augroup END
endif
syntax on

OmniCompletion

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 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:

:set tags=/path/to/your/ctags

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 'tags' and '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
  • Run 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: