Wednesday, December 24, 2008

PHP development in VIM

Emacs is just as good for PHP. Why VIM? Why not.

Basic PHP development in VIM is obviously no problem: we have highlighting, we have indenting - but what are the options when we want to get serious?

Here's a list of plugins I found especially useful.

Syntax checking
checksyntax: provides syntax checking for PHP, (La)TeX and Ruby. Note that you'll need display_errors set to stderr in php.ini for this to work.
Folding
phpfolding: What it says, and it Just Works.
Documenting

This is a bit more complicated. First you need to decide on a documenting system. phpDocumentor and Doxygen seem to be the most widely used. phpDocumentor is obviously written specifically for PHP, but it feels sluggish - though that's just my opinion. Doxygen is originally written for C and C++, but PHP shares much of it's syntax with them, so that's a non-issue.

For phpDocumentor there are several plugins. I found PDV (PHP Documentor for Vim) the nicest. It lets you insert comment blocks with a shortcut, and extracts as much as possible from the definition (name, visibility, arguments).

Same goes for Doxygen. DoxygenToolkit is (like Doxygen itself) written for C/C++/Java, but it gets things right in PHP most of the time. Doxygen-via-Doxygen looks promising: it uses Doxygen to actually generate the comments. Elegant. I can't comment on quality though, haven't tried it myself (download an XSLT processor, set up the XSLT file, I'm tired for that now).

Highlight MySQL
Serious PHP usually means a database, and some kind of SQL at that - so why not syntax-highlight the SQL code? Yeah, I know, it looks funny inside strings, but I like it - YMMV. One: you need SQL highlighting in sql files (you most likely already have that). Two: stick the following line somewhere it gets executed (I put it in ~/.vim/ftplugin/php/custom.vim): let php_sql_query = 1

Additional suggestions are welcome. Not out of courtesy, I genuinely am interested.

4 comments:

Matt Bemis said...

ctrl+p does autocomplete in vim 7.2

Anonymous said...

" Check syntax of php files on save
autocmd BufWritePost *.php :!/usr/bin/php -; %
" check syntax manually
autocmd FileType php noremap ^L :!/usr/bin/php -l % ^M
" show php function reference for word under cursor
nmap ^P :!php --rf ^R^W ^M

Anonymous said...

try the zend studio :-)

Zoltán Nagy said...

@Matt: Indeed. I rarely use autocompletion - for me it's more effort to check whether it's the completion I want than to type the whole thing. Still useful, thanks.

@Shawn: These are very helpful, thanks a lot. Note: I believe checksyntax does something similar to your first autocmd.

@php london: I will, thanks for the suggestion. However, being an Eclipse plugin as far as I understood from the site, I don't think it'll have much appeal to die-hard VIM/Emacs users. You know, different philosophies and stuff.

Post a Comment