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
Contents
Templates
TODO
Codesniffer integration
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[0]
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
:Phpcs
Including Manuals
Install the ManPageView script from Charles Campbell. It gives you access to the PHP manual, Perl manual and to man and info pages. The script could also be extended and I'd love to use it for Javascript and HTML, too. Make sure to take the most recent version, eventually from Campbell's homepage, since it may include bugfixes not yet uploaded to vimscripts.org.
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.
Autocompletition
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.
PDV (phpDocumentor for Vim)
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:
@todo
Tag Browsing with cscope (Identifier lookup)
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).
Build the taglist
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!
Taglist Window
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
Note
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.
Debug with xdebug
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.