Skip to main content

Have Vim Recognise Drupal's .module Files As PHP

OS X's Vim doesn't by default recognise file-types when opening files.
You can turn it on by adding following lines to the .vimrc file in your home directory.

$ echo -e "filetype indent on\nsyntax on" >> ~/.vimrc

This will add "filetype indent on" and "syntax on" to the file, or create it if it doesn't already exist. The "syntax on" is needed or Vim won't recognise the filetypes.

Now Vim will recognise file-types, indent them and colour the syntax of the recognised files.
Drupal however puts a lot of PHP code in .module files. These .module files aren't recognised by Vim as being PHP files.

We can tell Vim to interpret .module files as PHP by modifying the "Virata" piece of code in Vim's filetype file at /usr/share/vim/vim73/filetype.vim from:

" Virata Config Script File
au BufRead,BufNewFile *.hw,*.module,*.pkg setf virata

to:

" Virata Config Script File or Drupal module
au BufRead,BufNewFile *.hw,*.module,*.pkg,
\ if getline(1) =~ '<?php' |
\ setf php |
\ else |
\ setf virata |
\ endif

If you don't have access to the global filetype.vim file you can also put these lines in your own home directory's .vimrc.

Next time you want to edit a .module file in Vim it'll detect it as a PHP file. Some of you might say, no-one uses Vim, but when you quickly need to check/hack something on a server, chances are you'll be connecting through SSH using Vim to make the changes, so this might come in handy. Do set up your server-accounts' vim in such a way that viewing PHP in Vim becomes a bit more bearable.

Other settings you can put in your ~/.vimrc that might help you live with Vim are these:

set ruler " Show a ruler on the bottom
set expandtab " Expand tabs as spaces
set tabstop=2 " A tab is 2 spaces
set softtabstop=2 " Typing a tab in insert mode is 2 spaces
set shiftwidth=2 " Indent 2 spaces
set smartindent " Be smart when indenting
set title
set cindent
set hlsearch " Highlight searches
set incsearch " Search incrementally