Saturday, March 22. 2008Vim Productivity Tips for PHP DevelopersI use Vim for all my editing needs -- TODO lists, email, presentation outlines, coding in any language... everything. So, I thought I'd start sharing some of my vim habits and tools with others, particularly those that pertain to using Vim with PHP. Mapping the PHP interpreter and linter to keystrokesProbably the most useful thing I've done as a PHP developer is to add mappings to run the current file through (a) the PHP interpreter (using Ctrl-M), and (b) the PHP interpreter's linter (using Ctrl-L). These are accomplished with the following: " run file with PHP CLI (CTRL-M) :autocmd FileType php noremap <C-M> :w!<CR>:!$HOME/bin/php %<CR> " PHP parser check (CTRL-L) :autocmd FileType php noremap <C-L> :!$HOME/bin/php -l %<CR> (I have ~/bin/php as my PHP interpreter, which allows me to run PHP with a custom config file, as well as to change which PHP binary I'm using.) These two commands allow me to quickly and easily check that my syntax is okay, as well as to run unit test suites easily. Vim ProjectNext up is the excellent Project plugin.
"Project", at its most basic, allows you to setup a navigation pane with a
list of files related to your project. The files are typically organized by
directory, but the beauty is that the hierarchy can be defined however it
makes sense for your given project. It also has tools for creating projects
based on a given directory, recursively pulling in files based on filters
you specify. Type ':help project' to get documentation on this after you
install it; Each project can consist of one or more project folds; these can be sub projects, or a self-defined hierarchy or grouping of files. For instance, in my Zend Framework project file, I have "library", "tests", and "documentation" folds -- "library" points to "library/Zend/", "tests" points to "tests/", and "documentation" points to "documentation/manual/en/". Within each, I then have folds for each subdirectory. Since directories and subprojects are specified as folds, you can use Vim's native folding mechanisms to keep only the file of interest visible, which is very handy. ![]() Basically, Project allows vim to act like a minimal IDE. With the file list on the left, you simply hit enter on a file, and it loads in the main pane. More fun is when you use the \S command, which will split the main pane and load the file into the new pane. This is particularly useful when doing Test Driven Development, as you can have one pane for the unit test code, and another for the class file, allowing you to jump back and forth between them. Add to this the Ctrl-M and Ctrl-L commands I listed earlier, and you're now also able to quickly and easily check your files for syntax errors and run tests directly within the Vim window. ![]() There are other commands, too. You can run all files through a particular script, grep all files in a project, map particular file types to specific launchers, etc. Combine it with other Vim functionality, and you have a minimal, yet powerful, IDE at your disposal that launches in under a second.
By default, Project stores projects in $HOME/.vimprojects. I find that I
don't necessarily want all my projects at any given time, so I've created a
$HOME/.projects/ directory that has a project entry for each project -- I
simply save the contents of a project fold to files under this tree. I can
then perform CtagsI've covered ctags elsewhere, so I won't cover them here, but with ctags defined, I get tab completion for most classes and methods (and Vim takes care of tab-completion for class members in the current class file), as well as the ability to quickly and easily open class files for classes I've tagged -- which is useful when you want to see what methods are available and how they work. I'll try and cover other vim techniques I use in upcoming blog entries. Those listed in here, though, have greatly increased my productivity, and are things I use daily. Comments
Display comments as
(Linear | Threaded)
Instead of the project plugin i recommend another plugin for browsing files, comes with some extra features and just a little bit easier to work with.
NERDTree http://www.vim.org/scripts/script.php?script_id=1658 and from the same author NERDComment http://www.vim.org/scripts/script.php?script_id=1218 basically this allows you to select lines of code and comment them out for any language. http://www.vim.org/scripts/script.php?script_id=1318 finally snippet support, already comes with a bunch of snippets for php, phpdoc, and propel. The article about how to make PHP IDE from Vim plus some plugins:
http://allaboutvim.blogspot.com/2007/07/vim2ide-vim-ide-php.html P.S. Sorry, but it's on russian Hi!
You might possibly interested in my "Comfortable PHP editing with VIM" articles (http://schlitt.info/applications/blog/index.php/plugin/tag/vim) and the "PHP Documentor for VIM" plugin I wrote. I will definitly try out the "Project" plugin. Thanks for the hint! Cheers! Toby Toby -- I actually plan to write on the PHPDoc for Vim plugin in a later post; I use this regularly.
Why use Vim at all? If you're a fairly advanced developer, the hardest thing about writing PHP is remembering names of functions and their arguments. As far as I can tell, an IDE with a GUI would be far preferable because it could give you code completion tips and function references.
I can also think of a number of absurdly feature-filled IDEs that load in under a second on modern computers: UltraEdit-32 is an example. If you use Vim because you're editing directly on a server, you're setting yourself up for data loss. Why VIM? If you ever used :g you'd never have asked this question.
I already *have* code completion with vim (via ctags, as well as through some other native facilities in vim) can you please describe some other native facilities which can help to view class functions definitions and other .. I can not find any for my vim like PDT class browser in Eclipse.
The 'taglist' plugin will show you all methods in a class file; I don't know if it's a direct analogue to PDT's class browser, however.
Listen, I'm not saying Vim is for everyone, and I'm not trying to convince people to use Vim instead of a full-blown IDE or other editor. What I *am* doing is providing some information on plugins and tools you can use with Vim if you *are* using Vim to edit PHP. Certainly, if you prefer Eclipse's PDT, and have productivity tools in there that you find superior to other tools, by all means, use them and blog about them. Editors are very much an extension and personal preference of the individual developer. I prefer vim -- it keeps me at the keyboard, which, since I do a ton of typing, is a very big consideration. I know others, however, who are incredibly productive using IDEs and who could never consider working in Vim. Use the tool that suits your needs and your workflow. Steve -- I already *have* code completion with vim (via ctags, as well as through some other native facilities in vim) -- and for native PHP functions, can use omnicompletion to get parameter hinting. Additionally, with vim, it's trivial to execute shell commands -- so I can use w3m to get the full documentation for a function or do a lookup on a function name (and, in my case, I have the PHP documentation compiled locally, and run a PHP server that allows me to do function lookups even offline).
Additionally, I have used -- and still use -- a variety of operating systems, and need an editor that can travel with me. Vim is cross-platform, and exhibits the same functionality across them. And yes, I do need to edit files on remote servers... but only files I can check into repositories. Being able to use the same tool on those servers as I use in my daily work means less mistakes and less wasted time adjusting to different tools. Finally, as I mention in my first paragraph to the post, I use vim for *every* text-related task I perform, not just PHP. I use vimoutliner for outlining TODOs, projects, and presentations; I use vim within mutt for composing emails; I use vim when composing my blog posts; I have plugins in Firefox that allow me to edit textareas using vim; I use slrn with vim for using newsgroups; I write docbook XML documentation for my projects in vim... you get the idea. Why? Because vim is flexible, and I can learn a single editor and accomplish a variety of tasks. Personally, I'd rather not need to have a complete cognitive shift for every task I do that requires typing. Hi Matthew,
Sounds very powerful. I have never used Unix/Linux before. There are a number of options to be used in a Windows environment. From past experience which option do you suggest to be used on a Windows XP machine. Best regards, Muluget Maru When I was on Windows, I also used vim -- it's a cross-platform editor, and it works fine in every OS I've used.
Dude, seriously, you're a bad ass. I love vim also. I'm a die hard vim user -- also cross-platform compatible. Just stoked to see a similar enjoyment of such a powerful utility elsewhere. I will definitely be using some of these resources you've pointed out. Rock on!
I use (G)vim (with friends) for:
* syntax coloring (for everything!) * syntax checking (php -l, xmllint, tidy, jslint) * debugging (xdebug) * window layout for comfy editing (split vert/horiz, tabs) * spellchecking/sorting/sed-ing/vimdiff-ing * code formatting - eclipse cleanup (line endings, whitespace/tabs) * documentation (phpdoc) * code completion & search (snippets/ctags) * tetris I was first skeptical about VIM, because it was weird and look so old school and hard to use. I have used all kinds of editors on the planet you can imagine, like UltraEdit 32, Dreamweaver, Eclipse, Zend Studio, Komodo, Geany, etc. for 10 years. But once I started to use VIM and understand its hidden features (for 2 months now), it gets more and more productive. The things that you can do with VIM and it's keys is just incredible. The guys that made VI and VIM are just genius.
Its reassuring to know its not just me that writes whole powerpoint presentations in vim, and then pastes them into the outline format! I love the idea of the plugin for firefox, I need one of those - however I'm an opera user (for the same reasons of keeping my hands on the keyboard), so I will have to manage without that.
Hello!
I'm trying to use the vim project plugin, but the 'grep' feature doesn't seem to work on Windows. I think it's because I don't have grep installed - so I installed Cygwin, then tried to add 'let &path="C:\cygwin\bin"' to my .vimrc. It's still giving me an error - it looks like grep is returning 1, instead of 0. Sorry to hit y'all with this, but this is one of the top-rated Google results for 'vim project plugin' When I used windows, I was using cygwin, and used vim installed for cygwin -- so Project and grep worked fine. I'm not sure how you'd go about getting vim and grep to play together natively in windows, unfortunately; maybe another reader can answer that.
Thanks for the quick response! Using Vim w/ cygwin hadn't occurred to me - thanks for the tip!
I tried vim under cygwin, but wouldn't install the project plugin. I put it into ~/.vim/plugin, but it said that the :Project command was unknown. Hmmmm.... I'll see if I can't fiddle with it tomorrow some. Still - thanks! Search Vim help for vimgrep. It's already part of Vim you don't need an external program.
I've been using vim as a consultant for the last 9 years, just recently I took on a job on a team of about 30 developers. The house default was netBeans. I tried for the last 6 weeks to work with NB and Eclipse and even with the Jvi and viplugin I can't take any more. I need my vim! Then after upgrading to Snow Leopard netBeans crashes hourly and Eclipse is dreadfully slow.
I just need to see about vim and svn and I'll be set. Worst case, stick with command line svn. Any thoughts? I just found your site, great articles - thanks! - D Does this Project work in vim 7.2? I installed it on ~/.vim/plugin/, but none of the mappings work \C does nothing, or better said is not mapped so it acts as a normal C (change to the end of line), \c does nothing. I have vim 7.2 on Ubuntu jaunty (9.04).
Cheers I've been using it in vim 7.2 successfully for some time now, and have no problems with any of the mappings. Clearly, YMMV -- you might ask around on one of the vim mailing lists or IRC channels.
Add Comment
|
Calendar
QuicksearchLinks
CategoriesSyndicate This BlogShow tagged entries |
||||||||||||||||||||||||||||||||||||||||||








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
Tracked: Mar 23, 14:50
Today I did something I wanted to do for a long time: to throw away Eclipse. I’ve been using Eclipse for PHP development for some time now, and it always didn’t really feel good. It was a constant pain in the ass. And a few times, it really...
Tracked: Feb 06, 18:36