Getting Started With VIM and PHP

Originally posted December 13, 2007
Brian Dailey

Brian Dailey is a LAMP-stack developer with a wide range of experience in the development world. Get in touch!

For more articles on the development trade, see the Blog.

VIM in Gnome Terminal

If you're already using VIM, and you just want tips on how to use VIM with PHP, you can just jump straight to the good stuff.

What's "VIM"?

VIM, also referred to as VI (the two are actually not the same thing: VIM is a clone based on VI) is a text-editor that is often included by default in most Linux distributions. Contrary to many beliefs, you don't need to be a Linux expert, or even a Linux whiz-kid, to use VIM.

Some notes on this article: when key presses are indicated, they are case sensitive. 'I' and 'i' will do two different things.

Why use VIM?

There are many reasons, but here is the distilled list of reasons why I choose to wield VIM as my weapon of choice:

I have honestly tried to use IDEs in the past. I forced myself to use Eclipse (for PHP) for several weeks but eventually I realized that it just slowed me down. I do admit that perhaps a lot of my complaints could be addressed in the form of configuration changes: but some of the issues I was having were not immediately fixable (long load times, memory hogging, using a mouse-based interface, etc). I do not mean this to be an article to bash all IDEs. I'm just sharing how I develop applications in the hope that it's useful to someone.

There are many good articles explaining why VIM is such a great tool for developers. One of the best is Why, oh WHY, do those #?@! nutheads use vi?. My challenge to you, if you're not a VIM user, is to try it for a month. The learning curve can be a little steep, but once of the hang of it, it's like crack for good developers. You just won't be able to stop.

The Absolute Basics

To open up a filename in *nix, type vim filename

The first thing you'll notice is that if you type anything in, you're going to see some odd things happening. This is because there are two "modes" of VIM. There is command mode and insert mode. A lot of articles have been written on this that will help you get started (I will link them at the end of this article) but basically to edit text you need to move the cursor to the item you want to edit. Before we do that, let's figure out how to move around.

The arrow keys will get you to where you want to go (or you might be able to use the mouse) but if you do that, you might as well be using Eclipse! One of the great advantages of VIM is that you can get where you want to go extremely fast. If you watch an export VIMmer in action, it will look like magic. Here's how it works:

h: left
l: right
j: down
k: up
w: move cursor one word to the right
b: move cursor one work to the left
0: move to beginning of line
$: move to end of line
gg: move to beginning of document
G: move to end of document
nG: move to line #n

There are a lot more commands, but these are the most essential ones. The nice feature is that you can apply multipliers. You can type 20j and the cursor will move 20 lines down.

Navigate to the area you want to edit. Attempt to get used to using h,l,j,k to move around rather than the arrows. You'll be glad you did later. Once you get your cursor where you want it, press 'i' to go into insert mode. Now you can edit to your heart's content. Once you're finished, use <Esc> to go back into command mode. Now you know how to switch from one mode to another and back. There are other ways to go into insert mode: for more on the basics, see VIM documentation.

If you want to undo what you edited, just press 'u' in command mode. To redo, use Ctrl+r.

Some commands are issued at the bottom by typing a colon followed by the command. To quit VIM, for example, type :q To save and quit, use :x

Some Useful Features

Lightning Fast Searches

If you want to jump around, the most efficient way to do it is often a search. To start a search in command mode press '/'. From here you can type in a string and it will move you to the first available match. To jump to the next match, press 'n', to move to the previous match, press 'N'. To make search more useful, check out these useful command incsearch which will cause VIM to automatically go to the first match of whatever you're typing, and hlsearch which will highlight all matches.


Let's say you are given a text file that has the following information:

        Bilbo Baggins, 212-555-3214
        Frodo Baggins, 212-555-1249 ext. 12
        Peregrin Took 212-555-1234
        Peregrin Took, 212-555-1234
        Esmeralda Brandybuck212-555-5929

Assume this is the first 5 lines of, say, 50. You need it to be formatted like this:

        $hobbits = array (
                'Firstname Lastname' => '212-555-3214',

You could be clever and write a program to parse the text, but for just this once it will be faster to just create a macro.

Here's how I would go about it...

Move your cursor to the first line of "Bilbo." Press qa to start a new macro using the 'a' key as a save point.

Type 'I' to begin insert mode at the beginning of the line. Add the first apostrophe. Then use the search feature to move to the first digit on the line. To do this, hit <Esc> and then type the slash key ( / ) and then "\d" to search for a digit. This will move the cursor to the beginning of "212" on the line with Bilbo. Press 'i' to go into insert mode, backspace, add the apostrophe then => and another apostrophe. Hit <Esc> then 'A' to insert after the last character of the line. Type in another apostrophe and then a comma and hit escape. Now hit 'j' to move down one line, and then press 'q' to stop recording the macro. Congratulations! You just created your first macro. Now you can test it by hitting @a (in command mode, of course) to execute it. If it works, you should see Frodo Baggins placed in an array format, and the cursor should now be on the line with Peregrin Took. We still have 48 more records to format like this, so type in 48@a and it will execute the macro 48 more times. You have to admit that's pretty cool.

VIM and PHP: How I Roll

VIM is great for software delopment. It works equally well for just about any language you can think of: Java, PHP, Python, Ruby, etc. However, I primarily develop in PHP so that's what I'm going to limit this article to.

The first thing you're going to want to do is make sure syntax highlighting is on. Use :syntax on

Some of the tools I use:

  • PHP Syntax file put together by Peter Hodge.
    • Fold arrays
    • Highlight mis-matched braces
  • phpDocumentor for VIM by Tobias Schlitt
    • Allows you to quickly add phpdoc-compatible comments in your code.
    • You can do a whole document, or just a variable or function at a time.
  • XDebug+VIM
    • I've never tried this one Windows.
    • If you have used Xdebug before in an IDE, you already know how this works. If you have't, then you should definitely check it out.

Useful Links