Sunday, March 23. 2008
UPDATE: I'm using Emacs since some months and consider it superior to VIM for most use cases after using VIM for 4 years. (And I also came to hate PHP by the way.)
Today I read an article from Matthew Weier O'Phinney on Planet-PHP about Programming PHP with VIM. Since he want's to continue his series and I wrote a similiar text these days, I thought to post the text I wrote so far.
Maybe we can continue together to write a nice teaser to for PHP with VIM. It would be fine, to have one place (vim.org, php.net/docs ?) to collect all those nice helper scripts and plugins. The main benefit could be, that all the scripts can be installed together without overlapping key mappings.
So much for now, since I'm with my family to celebrate easter. Happy Easter to everybody celebrating it today!
VIM as an IDE for PHP
Codesniffer is a tool to check PHP code against a Coding standard. By integrating it into vim you can get the list of violations in a separate error window. The errow window allows jumping to each error in the source code, even if the line numbers have changed.
See help quickfix in the vim help.
Put the following code in $VIMHOME/plugin/phpcs.vim:
function! RunPhpcs() let l:filename=@% let l:phpcs_output=system('phpcs --report=csv --standard=YMC '.l:filename) " echo l:phpcs_output let l:phpcs_list=split(l:phpcs_output, "\n") unlet l:phpcs_list cexpr l:phpcs_list cwindow endfunction set errorformat+=\"%f\"\\,%l\\,%c\\,%t%*[a-zA-Z]\\,\"%m\" command! Phpcs execute RunPhpcs()
Now you can run a Codesniff for the current file via
You also need to install the links text browser, which is used to grap the online PHP manual:
apt-get install links
I took links, but elinks or links2 may work too.
After installation you simply press C-K on top of a keyword and the corresponding manual page will be opened in a separate window.
Vim helps you to complete identifiers:
The first proposed item beginning with auto is an PHP function. Vim can autocomplete with different language dictionaries depending on the filetype. Thus it won't propose you any PYTHON function while you're editing a PHP file.
The second item is a word from another file I'm editing in the same Vim session. Thus you can easily complete Variablenames or Functionnames from other opened files.
Vim tells you also where it got the term from. Since the last item is from the same file, there's no filename written after the word.
You do document your code with proper inline comments, do you? To make this task easier, Tobias Schlitt wrote a VIM plugin which automatically lookup some characteristics of the item you want to document and creates a Docblock sceleton for you:
Just like in a big IDE like eclipse, you can easily jump to the definitions of functions and classes. Vim integrates cscope for the job (apt-get install cscope).
Vim needs an index file for all identifiers. This file is build with cscope. First we create the file cscope.files which tells cscope which file to scan. In our case this are all files called *.php.
The -b option tells cscope to use the list from cscope.files:
cd project_dir find . -name '*.php' > ./cscope.files cscope -b rm ./cscope.files
The shell commands from above left a file cscope.out in project_dir. Now we tell vim to use the generated tagfile:
cscope add project_dir/cscope.out project_dir
Note that you have to add the project_dir as the second argument to cscope add!
Every IDE gives you an overview of all functions in a class, right?
Install exuberant-ctags ( >= Version 5.7 ) and the taglist vim plugin. On debian you go with:
apt-get install exuberant-ctags
vim-addons install taglist
The 5.7 version of exuberant-ctags still has problems to parse PHP properly. It can not distinguis between the word "function" in a comment and a real function decleration. Therefore I still use a patched version of exuberant-ctags version 5.6.
What's an IDE without a Debugger?
You can find the most up to date link to the vim-xdebug plugin at the remote section of xdebug.org.
Tracked: Jun 23, 02:58
Tracked: Sep 24, 20:53
Social comments and analytics for this post
This post was mentioned on Twitter by llagerlof: Transforme o VIM em uma IDE COMPLETA para PHP. F**A!! http://tr.im/zDeI
Weblog: uberVU - social comments
Tracked: Jan 02, 11:22
VIM: PHP development
Programming PHP on GNU/Linux can be even easier if one were to use VIM as an development tool. Here's two great article's which describes how one can turn VIM into a PHP IDE. You don't really use GNU/Linux until you VIM VIM an a PHP IDE Vim Productivit...
Tracked: Feb 18, 23:21
Tracked: Jul 19, 19:53
Tracked: Sep 08, 02:42
Display comments as (Linear | Threaded)
Another important feature is the tree file browsing. Checkout the NERDTree plugin for Vim
the YMC can't be found, change to Zend works for me.
But the quick fix window will only show one record, not list all the errors as you picture of screen.
Very nice list you did. Exactly what I needed.
But I've a problem with a feature which I urge to use:
It's the code sniffer integration. I've the same problem as hileon wrote: When I run :Phpcs there's only one line and it seems to behave like a proper command line action because it shows the green line of pressing ENTER or a command. So there must be any problem. It is also not colored.
Any ideas how I could fix it (I'm new to vim)? I'm running (g)vim72 on windows.
I have been thinking a lot about learning PHP, so this might be a good IDE to help get started. Thanks for this useful information that you have been shared to us readers.
Editing PHP with TAGS and Emacs or Vim -ish Tools. I'm just starting to use PHP and after a bit of searching tonight found a way to generate pretty reasonable TAGS files for use with emacs.
I tried the codesniffer thing you've mentioned, it works fine. I didn't know about codesniffers in php, and since i tried them now, i find the regular coding standards to be very strict, all the errors in my code are related to formatting that's too harsh isn't. so i searched for a easy going standard on net but hasn't found one, I was wondering can you give me your YMC coding standard, or is this one is again very strict.
Great post man. I would also suggest http://www.vim.org/scripts/script.php?script_id=2540 . It speeds up development considerably..
Thanks. But for the part of Codesniffer integration, it does not work. "let l:filename=@%" does not get the name of the current file.
I had to work a little to make phpcs work with vim. It seems that output format of phpcs changed (source column was added)
Also my installation had no YMC coding standard, so i set it to Pear.
This works for me:
let l:phpcs_output=system('phpcs --report=csv --standard=Pear '.l:filename)
command! Phpcs execute RunPhpcs()
Thank you for good post!
Very informative post. I've tried codesniffers too but still trying to work it out. But thanks for sharing how you do it here.
I've searched for all the other options and glad I found your post. I didn't know about codesniffers, and it's great and working for me well.
Many thanks for the post.
Just a comment: when including manuals, the shorcut seems to be Ctrl-K as seen on the text, but it is Caps-K
Last version of ManPageView downloaded from developer's site (link in post) is BUGGY
the developer just forgot some bit here and there in the code...so I suggest you people to find an older version somewhere else!
Im having a lot of trouble making Taglist
Could you please post a link to the patch u used for ctags 5.6? thanks
oh by the way, I found out that If u are developing in DRUPAL and installed the drupal-vim plugin
ManPageView and Taglist MUST BE PATCHED TO WORK WITH php.drupal file extensions
anyway great post!