Configuring Vim

Indentation

The following commands will indent your code the right amount, using spaces rather than tabs and automatically indent after you start.

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

Syntax highlighting

If you enjoy syntax highlighting, it is 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 *.module is PHP content. For *.modules use this snippet in .vimrc:

if has("autocmd")
  " Drupal *.module files.
  augroup module
    autocmd BufRead *.module set filetype=php
  augroup END
endif

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

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

AttachmentSize
php_snippets.txt9.43 KB

The all-powerful Modeline ;)

exhuma - January 10, 2006 - 16:05

You are missing out one of my favourite vim features... the modeline. Just put the following at the end of the source file (though in front of the ?> of course):

#vim: set expandtab tabstop=2 shiftwidth=2 autoindent smartindent:

Note: Don't forget the trailing colon!

from now on vim will use these settings once you open that file. This overrides your user-settings and is imho better than using a separate vimrc file and aliasing the command.

As a useful side-effect, these settings will then also come in effect on everybody else's box when opening your file. And hence help in a consistent coding standard. There are other ways of formatting the modeline for compatibility reasons. I prefer the above style. For more info enter :help modeline

Another note:
vim only looks for "vim: <instructions>:" so you can use whatever commenting style you like. You might argue that the usage of the pound symbol in this example is a bit out of place as the drupal coding standards discourage that character for comments. However, I personally have very similar coding style (only difference being indenting width ;) ) and use the pound symbol nevertheless. But only for 2 things. One: everything not code/logic-related, and two: a double pound for temporarily removed or debugging lines so I can grep those away ;).
As such, the vim modeline falls under the first case ;)

Enjoy

MA

Correction

coofercat - February 17, 2007 - 22:17

I never knew this about vim... How nifty! It's going in all my source files from now on.

However, :help modeline explains you have to have a white space at the start. Adding in a reminder that it's PHP, we get:

# vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:

or:

// vim: set filetype=php expandtab tabstop=2 shiftwidth=2 autoindent smartindent:

Also, don't the Drupal

emanaton - March 22, 2007 - 13:25

Also, don't the Drupal coding standards state one should wrap text at 80 chars? This helps ID such occurances:

" Highlight chars that go over the 80-column limit
:highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white
:match OverLength '\%81v.*'

Sean P. O. MacCath-Moran
Programmer Analyst
Voice & Fax: (877) 313-8381
Drupal at emanaton dot com
www.emanaton.com

additional parameter may be needed

ymcp - June 3, 2006 - 09:29

I found that my (Ubuntu Linux) desktop PC needed an additional line added to the .vimrc file to enable syntax highlighting:

syntax on

Additional Vim configuration items.

davev - June 22, 2006 - 03:05

You may also want to set some of the PHP specific vim options.

Snipped from :help vim.php:

There are the following options for the php syntax highlighting.

If you like SQL syntax highlighting inside Strings: >

  let php_sql_query = 1

For highlighting the Baselib methods: >

  let php_baselib = 1

Enable HTML syntax highlighting inside strings: >

  let php_htmlInStrings = 1

Using the old colorstyle: >

  let php_oldStyle = 1

Enable highlighting ASP-style short tags: >

  let php_asp_tags = 1

Disable short tags: >

  let php_noShortTags = 1

For highlighting parent error ] or ): >

  let php_parent_error_close = 1

For skipping an php end tag, if there exists an open ( or [ without a closing
one: >

  let php_parent_error_open = 1

Enable folding for classes and functions: >

  let php_folding = 1

Selecting syncing method: >

  let php_sync_method = x

x = -1 to sync by search (default),
x > 0 to sync at least x lines backwards,
x = 0 to sync from start.

Enjoy.

Alternative alias approach

emanaton - March 25, 2007 - 02:56

Well... I don't care for the notion of maintaining two .vimrc files, but I wish to maintain distinct default vim characteristics separate from my drupal editing, so I used this approach instead.

.vimrc I left alone.

I created .vimrc_drupal with the changes I wish to have from drupal (ref. comments in this article).

I then added this alias:
alias dvim='cat ~/.vimrc ~/.vimrc_drupal > /tmp/.vimrc_combined && vim -u /tmp/.vimrc_combined'

In this way, the configuration I seek is achieved each time without the need for maintaining two complete .vimrc files. Is there a better way? It occures to me that one might add some command line foo to pass a parameter in to VIM that could then be dealt with using conditional statements in the .vimrc files, but I'm not sure how (or if) this can be done. Any thoughts?

Sean P. O. MacCath-Moran
Programmer Analyst
Voice & Fax: (877) 313-8381
Drupal at emanaton dot com
www.emanaton.com

Perhaps you could simply put

akahn - July 25, 2008 - 15:22

Perhaps you could simply put source ~/.vimrc in your .vimrc-drupal file.

Highlight redundant whitespaces and tabs.

frjo - May 17, 2007 - 05:09

I don't remember where I found this snippet but I have found it really helpful in keeping my code clean and neat.

" Highlight redundant whitespaces and tabs.
highlight RedundantSpaces ctermbg=red guibg=red
match RedundantSpaces /\s\+$\| \+\ze\t\|\t/

Here is a readymade ~/.vimrc

edrex - July 7, 2007 - 15:37

Here is a readymade ~/.vimrc with most of the settings above, for those that don't want to spend 20 mins fooling:
http://oheric.com/sites/oheric.com/files/vimrc

Module editing

caligari - July 13, 2007 - 02:10

I can't edit a module with no folding and others:

set foldmethod=indent
set foldnestmax=1

and use:
zo Zap Open
zc Zap Close
zO Zap Open recursive
zC Zap Close recursive

This is my favorite modeline:

// vim:fenc=utf-8:ft=php:ai:si:ts=2:sw=2:et:nu:fdm=indent:fdn=1:

fenc (fileencoding) Set utf-8 encoding (latin1 is usual).
ft (filetype) Set .module as php file type (with syntax).
ai (autoindent) Copy indent when starting new line.
si (smartindent) Indent when starting a new line after '{'.
sw (shiftwidth) Number of spaces to use for autoindent steps.
ts (tabstop) Number of spaces that a tab counts for.
et (expandtab) Use spaces to insert a tab.
nu (number) Precede each line with its line number.
fdm (foldmethod) Fold code by indent.
fdn (foldnestmax) 1 maximum nesting (functions in module).

PHP Syntax Checking

mcantelon - February 7, 2008 - 23:22

Another handy thing you can do from Vim is check your PHP syntax. You can type shift-: then enter !php -l % to check the syntax of your last saved file. For convenience you can add the line cabbr pc !php -l % to your .vimrc file to enable you to check syntax by typing shift-: then entering pc.

-Mike Cantelon
http://mikecantelon.com

you can also use

snufkin - May 1, 2008 - 11:55

you can also use abbreviations as a way to quickly insert hooks. for this you will need to create a custom function, and an abbreviation:

iabbr Hook <C-R>=HookFunc()<CR>

" HookFunc(): Drupal helper function
function! HookFunc()
  let f = substitute(expand("%"), ".module", "", "g")
  return "function " . f . "_"
endfunction     

this replaces typed in Hook with function <yourmodulename>_.

 
 

Drupal is a registered trademark of Dries Buytaert.