<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  <channel>
    <title>Tag: vim :: phly, boy, phly</title>
    <description>Tag: vim :: phly, boy, phly</description>
    <pubDate>Fri, 17 Dec 2010 08:50:53 +0000</pubDate>
    <generator>Zend_Feed_Writer 2.1.4dev (http://framework.zend.com)</generator>
    <link>http://mwop.net/blog/tag/vim.html</link>
    <atom:link rel="self" type="application/rss+xml" href="http://mwop.net/blog/tag/vim-rss.xml"/>
    <item>
      <title>Vim Toolbox, 2010 Edition</title>
      <pubDate>Fri, 17 Dec 2010 08:50:53 +0000</pubDate>
      <link>http://mwop.net/blog/249-Vim-Toolbox,-2010-Edition.html</link>
      <guid>http://mwop.net/blog/249-Vim-Toolbox,-2010-Edition.html</guid>
      <author>me@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>
I've been using <a href="http://www.vim.org/">Vim</a> for close to a decade. I've often said
that <em>"Unix is my IDE"</em> -- because Vim is built in the Unix philosophy, allowing
me to pipe input into it, out of it, and every which way I want. It fits very
nicely with the Unix philosophy of doing one task well, and allowing
redirection. I've found it ideal for web development in general and PHP
development specifically -- in fact, I've had excellent experiences in every
language I've tried my hand at developing in when using Vim.
</p>

<p>
Vim is also my chosen productivity suite. When I want to write a document, I
don't go into OO.o Writer or MS Word or some other word processor; I open up a
window and start typing. In most cases, I can either cut and paste my work into
other tools, or pipe it to transformation tools. I worry about the <em>content</em>
first, and the <em>presentation</em> later... like any good MVC application. ;-)
</p>

<p>
Like any good tool, you have to invest time in it in order to reap its benefits.
My learning has, to date, fallen into three time periods:
</p>

<ul>
<li>
The initial months in which I first learned Vim, via vimtutor and Steve
   Oualline's Vim book.
</li>
<li>
A period in 2006-2007 when I felt the need to make my coding more efficient,
   and first started playing with exuberant-ctags and omni-completion.
</li>
<li>
The last quarter of 2010 (yes, that's now) when I was introduced to a number
   of new tools via Twitter.
</li>
</ul>
   
<p>
So, this is my Vim Toolbox, 2010 edition.
</p><h2 id="toc_1.1">Getting Plugins</h2>

<p>
I've added two primary ways to add new plugins to my arsenal:
</p>

<ul>
<li>
<a href="https://github.com/c9s/Vimana">Vimana</a>, which is a command-line tool for
   discovering, downloading, installing, and upgrading scripts found on
   <a href="http://www.vim.org">vim.org</a>. It's not perfect, but if you know the name of
   the script, and it's provided in either a
   <a href="http://www.vim.org/scripts/script.php?script_id=1502">vimball</a> format and/or
   follows the Vim runtime file structure, it's a great way to keep your
   plugins, syntax files, etc. up-to-date.
</li>
<li>
<a href="http://tammersaleh.com/posts/the-modern-vim-config-with-pathogen">Vim Pathogen</a> 
   allows you to install plugins as "bundles", allowing you to keep
   them up-to-date separately, in their own file tree. This looks like the
   following:
<pre>
.vim/
    bundle/
        vim-task/
            ftdetect/
            ftplugin/
            syntax/
</pre>
   In short, a "bundle" mimics the structure of a Vim directory.
</li>
</ul>

<p>
The latter, Pathogen, is my preferred installation method of choice at this
point. Why? One acronym: DVCS.
</p>

<p>
A ton of popular Vim plugins are now being either developed or mirrored on
GitHub or other DVCS sites. This allows you to clone them and then create a
branch that's specific to your configuration. As an example, the popular
<a href="http://www.vim.org/scripts/script.php?script_id=2540">snipMate plugin</a> has its
key-bindings hard-coded -- which causes problems if you're already using those
bindings. Which, if you're using any form of omni-completion, is all too likely.
I simply <a href="https://github.com/weierophinney/snipmate.vim">cloned the snipMate repo</a>, 
and created a branch for my configuration (I use <code>&lt;Leader&gt;&lt;tab&gt;</code> to invoke it).
</p>

<p>
Now, it gets even better: I've made <a href="http://git.mwop.net/?a=summary&amp;p=vimrc">a git repository for my Vim configuration</a>; 
with judicious use of <code>git submodule</code>, I can now add pathogen bundles as
submodules of my repository. Right now, I've got bundles for html5.vim,
mustache.vim, NErdtree, snipMate, TagList, vim-fugitive, vim-task, and
vimwiki. This keeps my repository lean, while retaining the features I need and
use daily.
</p>

<p>
As part of creating my Vim configuration repository, I also made a few
changes to facilitate the process. First, I moved <code>$HOME/.vimrc</code> to
<code>$HOME/.vim/vimrc</code>, and symlinked the former to the latter. This allows me to
keep all my Vim configuration in one place.
</p>

<p>
Next, I moved my Vim view files outside the directory; this data is volatile and
constantly changing, and really does not need to be versioned. These are now
in my <code>$HOME/.vim.view/</code> directory. Finally, I moved my tag files into a new 
<code>$HOME/.vim.tags/</code> directory. More on that later, but, again, the rationale is
that this data is volatile and does not need to be versioned.
</p>

<h2 id="toc_1.2">DVCS</h2>

<p>
I mentioned I created a Git repository for my Vim configuration. In part, this
is due to the fact that I'm proficient with Git -- I use it day-in, day-out.  Hg
and other DVCS systems are also great; I'm not using them nearly as often,
however.
</p>

<p>
To that end, I'm now using <a href="https://github.com/tpope/vim-fugitive">vim-fugitive</a>.
The author boasts that it's "a Git wrapper so awesome, it should be illegal"; I
wouldn't necessarily go that far, but I do find it incredibly useful. While I'm
typically working in a console, I also find myself in GVim windows regularly as
well -- and having a nice, familiar interface to Git is very useful. If you use
both Vim and Git, I highly recommend checking out vim-fugitive.
</p>

<h2 id="toc_1.3">Filesystem Navigation and Projects</h2>

<p>
At some point, unless you're one of those developers who likes to code
everything in a single file, you need to do some sort of navigation. A couple
years back, <a href="http://zmievski.org/">Andrei Zmievski</a> introduced me to
<a href="http://www.vim.org/scripts/script.php?script_id=1658">NErdtree</a>, a dead-simple,
colorized navigation. I use this every. single. day.
</p>

<p>
I also use a tool called <a href="http://www.vim.org/scripts/script.php?script_id=69">Project</a>. 
This tool allows you to specify "files of interest" to a project -- either by
automatically scanning a tree, or manually. Additionally, the way you specify
the hierarchy can be entirely arbitrary -- allowing you to flatten the tree when
it's getting in the way. I use this tool regularly as well, though not quite as
much as NErdtree.
</p>

<h2 id="toc_1.4">Navigating Code</h2>

<p>
One often touted feature of modern IDEs is code completion and hinting. These
are definitely useful features, particularly when working on unfamiliar code, or
code you haven't touched in some time.
</p>

<p>
Vim actually has some great tools for this already. One is built-in:
omni-completion (<code>:he new-omni-completion</code> for Vim's help on the feature). By
default, it inspects the files in open buffers to provide completion (assuming
it has a definition for that language and/or syntax highlighting) -- but it can
also utilize <em>tag</em> files.
</p>

<p>
The built-in omni-completion for PHP is reasonable -- you can jump around by
class names, function/method names, variables, etc. It gets much, much more
useful, however, when you utilize tag files, as you don't need the files already
open in order to get completion. I've <a href="http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html">blogged about ctags before</a>; 
however, I've updated my scripts a bit.
</p>

<p>
First, exuberant-ctags is much more PHP aware now than when I blogged. This
means you don't need to do any special regex-fu in order to properly identify
abstract classes, interfaces, and methods. Second, I found that I could generate
a single script with prompts to indicate the directory and tag file name.
<a href="http://git.mwop.net/?a=viewblob&amp;p=vimrc&amp;h=3a8ca75bcd6a28dbea2cee02f31d72e82f415e5b&amp;hb=c1a728c96fa593be5c570c78cac5dcb2b7052fd3&amp;f=bin/mkTags">That script</a> 
basically looks like this:
</p>

<div class="example"><pre><code lang="sh">
#!/bin/sh
dir=\&quot;\&quot;
name=\&quot;\&quot;
if [ $# -ge 2 ] ; then
    # Two arguments: first is directory, second is \&quot;alias\&quot;
    dir=$1
    name=$2
else
    if [ $# -eq 1 ] ; then
        # One argument: use as directory, and use basename of directory as alias
        dir=$1
        name=`basename $1`
    else
        # Otherwise: prompt
        echo \&quot;Enter the path to a directory containing PHP code you wish\&quot;
        echo \&quot;to create tags for:\&quot;
        read dir
        echo \&quot;Enter the name of the tagfile you wish to create:\&quot;
        read name
    fi
fi

echo \&quot;Creating tags for directory '$dir' using alias '$name'\&quot;
cd $dir
exec ctags-exuberant -f ~/.vim.tags/$name \
-h \&quot;.php\&quot; -R \
--exclude=\&quot;\.svn\&quot; \
--totals=yes \
--tag-relative=yes \
--fields=+afkst \
--PHP-kinds=+cf 
echo \&quot;[DONE]\&quot;
</code></pre></div>

<p>
Two things to note: 
</p>

<ul>
<li>
It creates the tag files in <code>$HOME/.vim.tags/</code>. I do this as my tag files
   change fairly regularly, and can be re-generated on the fly as needed.
   There's no reason to version them.
</li>
<li>
Once generated, you need to load them. I created a "LoadTags" Vim function
   that will load a tag file by the given name from the <code>$HOME/.vim.tags/</code>
   directory. By default, I load the ones I most commonly use (ZF1, ZF2,
   PHPUnit). Otherwise, a quick <code>:Ltag &lt;tag filename&gt;</code> will load on-demand.
</li>
</ul>

<p>
Once the tags are created, you can use Vim's normal tag features to load files,
jump to files, etc. The most common commands I use are:
</p>

<ul>
<li>
<code>:stag &lt;tagname&gt;</code>, which splits the current window and loads the given tag in
   the newly created split.
</li>
<li>
<code>&lt;Ctrl-w&gt;]</code>, when on text you suspect of being a tag (such as a classname),
   will split the current window and load that tag file in the new pane.
</li>
</ul>

<p>
These two commands I use constantly, and are huge timesavers -- I can basically
use the code as my documentation.
</p>

<p>
Additionally, the main use of omni-completion is to give tab-completion for
known tags. This means that you can start typing, hit <code>&lt;Tab&gt;</code>, and either have
it immediately complete, or give you a list of potential matches. It's not quite
as useful as a good IDE -- it's not context-aware, so you'll get <strong>any</strong> potential
match from <strong>any</strong> class -- but it's better than nothing, provides reasonable
hinting, and helps protect you from spelling errors.
</p>

<p>
That said, there's also something to be said about just having the signatures
and prototypes of the various methods easily accessible. For that, there's the
<a href="http://vim-taglist.sourceforge.net/">Vim TagList</a> plugin. This plugin will scan
open files and produce a list of classes, variables, and methods. With this
list, you can get the method prototypes, as well as jump directly to their
definitions. Pressing <code>&lt;Space&gt;</code> will show you the prototype, <code>&lt;CR&gt;</code> will jump to
it.
</p>

<p>
Between these two features (omni-completion with tags and TagList), I have most
of the useful features of any IDE immediately available.
</p>

<h2 id="toc_1.5">Working With Code</h2>

<p>
Since I sling code for a living, it's useful to have some plugins and syntax
highlighting to make working with code easier.
</p>

<p>
First off, I've been experimenting with HTML5; as such, I added the
<a href="https://github.com/othree/html5.vim">html5.vim</a> syntax highlighting as a
Pathogen module. This adds support for a bunch of HTML5-specific features, while
retaining the fantastic HTML support already in the official HTML syntax
provided with Vim.
</p>

<p>
Next, I use the <a href="http://www.vim.org/scripts/script.php?script_id=967">php.vim</a>
syntax file from vim.org. This particular syntax file has support for PHP 5.3
features, which come in very handy while I'm coding for ZF2. The author of this
syntax has also created a script (<code>php_vimgen.php</code>) for generating syntax files
for core classes as well as extensions using the Reflection API. I've modified
the tool in my repository to strip out the generated syntax, and instead
source it from the file created with the <code>php_vimgen.php</code> script; I've also
altered said script to create the syntax in <code>__DIR__ . '/php_syntax_vimgen.vim'</code>, 
ensuring I can always source it from the same location. This allows me to keep
my PHP syntax highlighting up-to-date.
</p>

<p>
Finally, I use <a href="http://www.vim.org/scripts/script.php?script_id=2540">snipMate</a>,
a tool that emulates TextMate's "snippet" features. Basically, this is
dead-simple, templated code generation. You can write your own files (
<a href="http://git.mwop.net/?a=tree&amp;p=vimrc&amp;h=5990c978d877f9dcad2a02239ae3af74bcb75ba4&amp;hb=c1a728c96fa593be5c570c78cac5dcb2b7052fd3&amp;f=snippets/php">I did</a>),
or use those that come with it. Once you've got some snippets, you type a word
(usually a mnemonic for the operation you're trying to perform), and it will
either just spit up a template, or optionally provide "prompts" for you to fill
in (along with variable completion!). Basically, I never code accessors and
mutators anymore; snipMate does these for me, with a little prompting.
</p>

<h2 id="toc_1.6">Organization</h2>

<p>
I use Vim day-in, day-out, for all sorts of things: mail, drafting blog posts,
drafting presentation outlines, taking meeting notes, managing my todo list, and
more. As such, I try to keep as much of my "organization" within Vim -- it's
just easier.
</p>

<p>
I've tried a number of tools over the years. For a good 4 or 5 years, my primary
tool was <a href="http://www.vimoutliner.org/">VimOutliner</a>. It provided decent syntax,
decent folding, and reasonable HTML generation from the outline. However, in
recent years, I feel the project had stalled, and I also found that the way I
wanted to use it had changed: outlining is great, but I often want to use the
outline as a starting point for generating content; task tracking is fine, but I
found, for whatever reason, that the way VimOutliner handled task status often
didn't work well -- either from a tooling or a syntax standpoint.
</p>

<p>
At some point, <a href="http://twitter.com/tswicegood">Travis Swicegood</a> introduced me to
<a href="http://code.google.com/p/vimwiki/">vimwiki</a>. This tool provides a personal wiki
<em>within</em> Vim. This tool allowed me to organize my notes in an ad-hoc,
semi-hierarchical way, link back and forth between them, and have not only
reasonable in-editor highlighting, but great HTML generation. This allowed me to
ditch VimOutliner for everything but task tracking. Once I made my "wiki"
directory a Git repository, I then received versioning basically for free
(especially with vim-fugitive, which makes it easy for me to hit <code>:Gwrite</code> and
<code>:Gcommit</code> when I create and/or update files).
</p>

<p>
Another feature vimwiki provides is a "diary". You access it using
<code>&lt;Leader&gt;w&lt;Leader&gt;w</code>, which opens up a new wiki page for the current day (or, if
you already opened it before, re-opens the one created earlier in the day). This
is a really useful tool for taking notes during meetings, or when doing
research, etc.
</p>

<p>
Couple these features with integrated search (<code>:VWS /pattern/</code>), and vimwiki is
<em>the</em> killer productivity tool in my toolbox.
</p>

<p>
At another point, Travis then pointed out another tool:
<a href="https://github.com/samsonw/vim-task">vim-task</a>. This is perhaps the most
dead-simple task tracker I've ever used; each line is a task, and is either
incomplete (starts with a "-"), or complete (starts with a checkmark). A simple
keybinding, which I've mapped to <code>&lt;Leader&gt;m</code>, toggles status - and complete
items get highlighted in green and italicized, making you feel good and giving a
good visual queue as to what you've completed.
</p>

<p>
At some point, Travis also tossed out the idea that combining vimwiki with
vim-task would be useful -- and I latched onto this idea. I've now created
<a href="https://github.com/weierophinney/vimwiki/tree/feature/vim-task">a fork of vimwiki with vim-task integration</a>,
which allows me to keep my tasks and notes in a single place... and, since my
wiki is versioned, my tasks are as well.
</p>

<h2 id="toc_1.7">Various Oddities</h2>

<p>
As I mentioned at the start of this post, I've been using Vim for close to a
decade. Part of the reason Vim was appealing to me was due to the fact that it
kept me in the "home row" of the keyboard -- which provides a huge amount of
efficency. You don't have to move to the arrow keys to scroll, no leaving the
keyboard for the mouse, etc. That said, some key combinations are difficult to
reach:
</p>

<ul>
<li>
The placement of the <code>&lt;Esc&gt;</code> key varies from keyboard to keyboard, and is
   rarely in a place that is easy to reach. On my current keyboard, it's in the
   top left corner, above the function keys; it's impossible to reach without
   moving my hand. A tip I picked up pretty much when I began using Vim was to
   map "jj" to <code>&lt;Esc&gt;</code>; it's rare to type a "j" repeatedly in the English
   language, and it's dead-center on the home row. This is incredibly efficent.
</li>
<li>
I've mapped my Caps Lock key to <code>&lt;Ctrl&gt;</code> on every system I've owned in the
   past decade. I never used it, and it's almst always on the home row. Again,
   hugely efficient.
</li>
<li>
Keybindings are great, but there's so many already in use that it's hard
   <em>not</em> to overwrite existing ones. Using the <code>&lt;Leader&gt;</code> key to define
   keybindings has been great. As examples, I mapped <code>&lt;Leader&gt;m</code> to toggle
   tasks, and <code>&lt;Leader&gt;&lt;Tab&gt;</code> to invoke snipMate.
</li>
</ul>

<p>
In Vim, <code>&lt;C-m&gt;</code> has long been the "make" binding, and <code>&lt;C-l&gt;</code> for linters. In
languages like PHP and JavaScript, these often don't make sense. However, I've
bound these in both languages -- in PHP, "make" executes the current script
using the PHP executable, while "lint" runs it through the PHP linter. In JS, I
leave "make" unbound, while "lint" runs the script through jslint.
</p>

<p>
I've also added the "php-doc.vim" plugin, and mapped <code>&lt;C-P&gt;</code> to create PHP
docblocks; the plugin is context aware, and will create appropriate annotations.
</p>

<h2 id="toc_1.8">Cloning my repo</h2>

<p>
As noted, I've created a repository for my Vim configuration. If you want to
clone it and explore it, you can do so as follows:
</p>

<ul>
<li>
Browse the repository: <a href="http://git.mwop.net/?a=summary&amp;p=vimrc">http://git.mwop.net/?a=summary&amp;p=vimrc</a>
</li>
<li>
Clone the repo: <code>git clone git://mwop.net/vimrc.git</code>
</li>
</ul>

<p>
Be aware that there a number of git submodules in play (all the pathogen modules
are git submodules). To initialize these, simply run <code>git submodule init</code>
followed by <code>git submodule update</code> after you clone the repository.
</p>

<h2 id="toc_1.9">Resources</h2>

<p>
I didn't learn all this overnight. As with any toolset, it's only as good as the
amount of time you invest learning it. For me, my primary resources lately have
been:
</p>

<ul>
<li>
<a href="http://twitter.com/#!/search/%23vim">#vim hashtag on Twitter</a>
</li>
<li>
<a href="http://vimcasts.org/">VimCasts</a> are a fantastic source of information,
   provided by Drew Neil. Seriously, these are <strong>completely</strong> worth the time spent
   watching them.
</li>
<li>
<a href="http://twitter.com/tswicegood">Travis Swicegood</a> has tweeted a number of
   times about interesting things he does with Vim and Git, and inspired me to
   write the vim-task syntax for vimwiki.
</li>
</ul>

<h2 id="toc_1.10">More Tools</h2>

<p>
This post has been on my <i>Vim</i> toolbox. I've also been usig a number of
other tools lately -- <a href="http://tmux.sourceforge.net/">tmux</a>, <a
    href="http://www.zsh.org/">zsh</a> (in particular, git prompts), <a
    href="http://hotot.org/">Hotot</a> (GTK2 + WebKit Twitter client), and more;
I may blog about those in the future -- using Vim. ;-)
</p>]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>Vimgrep and Vim Project</title>
      <pubDate>Thu, 23 Oct 2008 01:55:03 +0000</pubDate>
      <link>http://mwop.net/blog/194-Vimgrep-and-Vim-Project.html</link>
      <guid>http://mwop.net/blog/194-Vimgrep-and-Vim-Project.html</guid>
      <author>me@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>
    Chris Hartjes today was <a href="http://www.littlehart.net/atthekeyboard/2008/10/20/vim-programming-bounty-fuzzyfind-inside-files/">on a quest for a "find in project" feature for Vim</a>.
    "Find in Project" was a feature of Textmate that he'd grown accustomed to
    and was having trouble finding an equivalent for.
</p>

<p>
    The funny thing is that Textmate is a newcomer, and, of course, vim has had
    such a feature for years. The thing to remember with vim, of course, is its
    unix roots; typically if you know the unix command for doing something, you
    can find what you need in vim. In this case, the key is the vimgrep plugin,
    which ships in the standard vim distribution.
</p><p>
    There are a variety of resources on vimgrep. The vim documentation includes
    a chapter on it, and a quick <a href="http://www.google.com/search?q=vimgrep">google search</a>
    on the subject turns up some nice tutorials immediately. If you've ever used
    grep, the syntax is very straightforward:
</p>

<code><pre>
vimgrep /{pattern}/[g][j] {file} ...
</pre></code>

<p>
    The "g" option indicates that all matches for a search will be returned
    instead of just one per line, and the "j" option tells vim <em>not</em> to
    jump to the first match automatically. What does the "g" flag really mean,
    though, and how are searches returned?
</p>

<p>
    Vimgrep returns search results in what's known as a "quickfix" window, and
    this is where the vimgrep documentation falls apart... it doesn't explain
    what this is, or link to it (which would be a nice indication that it
    actually has a separate topic for this).
</p>

<p>
    The Quickfix window is a pane that shows a search result per line. Each line
    shows the file that matches, the line number, and the contents of that line:
</p>

<code><pre>
/home/matthew/git/bugapp/application/controllers/helpers/GetForm.php|10| * @var Zend_Loader_PluginLoader
</pre></code>

<p>
    You can't do much from this window; it simply serves as a visual indicator
    of what file you're currently looking at from the list. However, in the main
    window, you can start iterating through the results one at a time, using a
    subset of the Quickfix commands. As a quick summary:
</p>

<ul>
    <li><b>:cc</b> will move to the next match in the list</li>
    <li><b>:cn</b> will move to the next match in the list</li>
    <li><b>:cp</b> will move to the previous match in the list</li>
    <li><b>:cr</b> will rewind to the first match in the list</li>
    <li><b>:cla</b> will fast forward to the last match in the list</li>
</ul>

<p>
    When done, you can simply close the Quickfix window/pane, and continue
    working.
</p>

<p>
    I should note that vimgrep <em>is</em> cross-platform. On *nix-based
    systems, it defaults to using the native grep command, but it also contains
    an internal (slower) implementation for use on operating systems that do not
    provide grep by default. You may also map the command to alternate
    implementations if desired.
</p>

<p>
    I personally use this feature most with the <a href="http://www.vim.org/scripts/script.php?script_id=69">project plugin</a>.
    Project maps vimgrep to two different commands: &lt;Leader&gt;g and
    &lt;Leader&gt;G. The first will grep all files in the current project at the
    current level; the second does the same, but also recurses into subprojects.
    This is an incredibly easy way to refactor code, particularly for name
    changes.
</p>]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>Vim Productivity Tips for PHP Developers</title>
      <pubDate>Tue, 25 Mar 2008 15:39:35 +0000</pubDate>
      <link>http://mwop.net/blog/164-Vim-Productivity-Tips-for-PHP-Developers.html</link>
      <guid>http://mwop.net/blog/164-Vim-Productivity-Tips-for-PHP-Developers.html</guid>
      <author>me@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>
    I use <a href="http://www.vim.org/">Vim</a> 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.
</p><h3>Mapping the PHP interpreter and linter to keystrokes</h3>
<p>
    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:
</p>

<div>
<div class="example"><pre><code lang="vim">
\&quot; run file with PHP CLI (CTRL-M)
:autocmd FileType php noremap &lt;C-M&gt; :w!&lt;CR&gt;:!$HOME/bin/php %&lt;CR&gt;

\&quot; PHP parser check (CTRL-L)
:autocmd FileType php noremap &lt;C-L&gt; :!$HOME/bin/php -l %&lt;CR&gt;
</code></pre></div>
</div>

<p>
    (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.)
</p>

<p>
    These two commands allow me to quickly and easily check that my syntax is
    okay, as well as to run unit test suites easily.
</p>
    
<h3>Vim Project</h3>

<p>
    Next up is the excellent 
    <a href="http://www.vim.org/scripts/script.php?script_id=69">Project plugin</a>.
</p>

<p>
    "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; <code>\C</code> will help you create your first project.
</p>

<p>
    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.
</p>

<div><img src="/uploads/2008-03-22-VimProject.png" alt="Vim Project" /></div>

<p>
    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.
</p>

<div><img src="/uploads/2008-03-22-VimUnitTests.png" alt="Vim Project" /></div>

<p>
    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.
</p>

<p>
    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 <code>:r ~/.projects/&lt;projectname&gt;</code> 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.
</p>

<h3>Ctags</h3>

<p>
    I've <a href="/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html">covered ctags</a> 
    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.
</p>

<hr />

<p>
    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.
</p>]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>exuberant ctags with PHP in Vim</title>
      <pubDate>Thu, 25 Mar 2010 20:18:59 +0000</pubDate>
      <link>http://mwop.net/blog/134-exuberant-ctags-with-PHP-in-Vim.html</link>
      <guid>http://mwop.net/blog/134-exuberant-ctags-with-PHP-in-Vim.html</guid>
      <author>me@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>
    One reason I've heard PHP developers use for adopting an IDE when developing
    is the ability to click on a class or function name and jump to the
    declaration. Sounds like magic, and it's definitely something I've desired.
</p>
<p>
    One way I get around it is by adopting PEAR coding standards for naming my
    classes. Since they define a one-to-one mapping of class name to the file
    system (substitute the underscore character ('_') with the directory
    separator), I can usually very quickly and easily open a class file,
    particularly if I start in the base directory of the project install.
</p>
<p>
    Today, however, I found <a href="http://ctags.sourceforge.net">exuberant ctags</a>,
    a library which can be used to generate an index file mapping language
    objects to source files and the line in the source file where they are
    declared. Contrary to its name, it's not just for the C language; it
    currently supports 33 different programming languages, including PHP.
</p>
<p>
    I decided to try it out on the Zend Framework core library today. At first
    run, it was pretty useful. However, it was only mapping classes, and, in
    addition, only those defined with the single word 'class' -- abstract classes
    and interfaces were entirely left out. So, I looked into the documentation
    to see if I could change the behaviour.
</p>
<p>
    And, being a Unix program, of course I could. First off, you can add
    functions to the items it indexes with a simple flag. Additionally, you can
    use POSIX regular expressions to refine what it searches.
</p>
<p>
    I whipped up the following script to create my tags index:
</p>
<div class="example"><pre><code lang="bash">
#!/bin/bash
cd /path/to/framework/library
exec ctags-exuberant -f ~/.vim/mytags/framework \
-h \&quot;.php\&quot; -R \
--exclude=\&quot;\.svn\&quot; \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+cf \
--regex-PHP='/abstract class ([^ ]*)/\1/c/' \
--regex-PHP='/interface ([^ ]*)/\1/c/' \
--regex-PHP='/(public |static |abstract |protected |private )+function ([^ (]*)/\2/f/'
</code></pre></div>
<p>
    This script creates the tag index in the file
    <kbd>$HOME/.vim/mytags/framework</kbd>. It scans for PHP files recursively
    through the tree, excluding any files found in a <kbd>.svn</kbd> directory
    (I'm using a checkout from the subversion repository). The file paths in the
    index are created relative to the tags file; this was important, because if
    this wasn't provided, vim was unable to jump to the file, as it couldn't
    find it. <kbd>--PHP-kinds=+cf</kbd> tells it to index classes and functions.
    Next, I've got three regular expressions.  The first tells it to match
    classes beginning with 'abstract class' as classes. The second tells it to
    match interfaces as classes. The last is so that PHP 5 methods, which begin
    with a visibility operator, to be matched as functions.
</p>
<p>
    Once the index file is generated (it takes less than a second), all you need
    to do in vim is tell it to load it: <kbd>:set
        tags=~/.vim/mytags/framework</kbd>. At this point, you can do all sorts
    of fun stuff. Place the cursor on a class name or method name, anywhere in
    it, and hit <kbd>Ctrl-]</kbd>, and you'll jump to the file and line of its
    declaration; <kbd>Ctrl-T</kbd> then takes you back. If you change the
    invocation to <kbd>Ctrl-W ]</kbd>, it will split the current window and open
    the declaration in the new pane. (If you're familiar with how help works
    with Vim, this should seem pretty familiar.)
</p>
<p>
    One more reason to stick with Vim for your PHP editing needs. :-)
</p>]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
  </channel>
</rss>
