LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Saturday, March 22. 2008

Vim Productivity Tips for PHP Developers

I 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 keystrokes

Probably 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 Project

Next 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; \C will help you create your first project.

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.

Vim Project

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.

Vim Project

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 :r ~/.projects/<projectname> to read in a given project when I want to work on it. This helps me keep my workspace uncluttered, and also helps me focus on a given project at a time.

Ctags

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

Posted by Matthew Weier O'Phinney in PHP at 10:41 | Comments (20) | Trackback (1)
Defined tags for this entry: best practices, php, vim
Related entries by tags:
Speaking at php|tek
Creating composite elements
Speaking at DPC (again!)
Rendering Zend_Form decorators individually
Zend Framework 1.8 PREVIEW Release

Trackbacks
Trackback specific URI for this entry

VIM an a PHP IDE
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
Weblog: Thomas Koch
Tracked: Mar 23, 14:50

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.
#1 brendon on 2008-03-22 13:01 (Reply)
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 :-)
#2 Pento (Link) on 2008-03-22 13:07 (Reply)
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
#3 Toby (Link) on 2008-03-22 13:18 (Reply)
Toby -- I actually plan to write on the PHPDoc for Vim plugin in a later post; I use this regularly. :-)
#3.1 Matthew Weier O'Phinney (Link) on 2008-03-22 13:21 (Reply)
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.
#4 Steve on 2008-03-23 17:43 (Reply)
Why VIM? If you ever used :g you'd never have asked this question.
#4.1 Toby (Link) on 2008-03-23 19:23 (Reply)
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.
#4.1.1 Symbio on 2008-03-24 07:08 (Reply)
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.
#4.1.1.1 Matthew Weier O'Phinney (Link) on 2008-03-24 08:01 (Reply)
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.
#4.2 Matthew Weier O'Phinney (Link) on 2008-03-23 20:03 (Reply)
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
#4.2.1 Maru on 2008-04-14 10:03 (Reply)
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.
#4.2.1.1 Matthew Weier O'Phinney (Link) on 2008-04-14 10:27 (Reply)
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!
#4.2.2 Andrew Ashbacher (Link) on 2009-04-06 20:26 (Reply)
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 :-)
#4.3 PrettyCoder on 2008-03-25 04:34 (Reply)
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.
#4.4 David (Link) on 2008-04-25 00:44 (Reply)
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.
#5 LornaJane (Link) on 2008-03-25 11:39 (Reply)
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' :-)
#6 Mike on 2008-07-24 02:25 (Reply)
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.
#6.1 Matthew Weier O'Phinney (Link) on 2008-07-24 06:52 (Reply)
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!
#6.1.1 Mike on 2008-07-24 21:08 (Reply)
Search Vim help for vimgrep. It's already part of Vim you don't need an external program.
#6.2 Kevin on 2008-07-26 00:19 (Reply)
Thanks for the tip - that actually worked! :-)
#6.2.1 Mike on 2008-07-27 15:36 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back July '09
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

July 2009
June 2009
May 2009
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml decorators
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml oop
xml pear
xml perl
xml personal
xml php
xml phpworks08
xml programming
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net