#native_company# #native_desc#

Using Vim for Your PHP Development

By W. Jason Gilmore
on September 22, 2010

How much of your average work day is lost to hard-to-avoid distractions? Given the ubiquity of ringing cell phones and e-mail alerts, the allure of checking Facebook and Twitter just one more time, and the sounds and sights surrounding those of us who work in non-traditional environments such as the local coffee shop, you probably lose hours rather than minutes.
When you choose a streamlined IDE, you forfeit the visual bells and whistles usually found in many commercial products but you gain the ability to write, organize and refactor code as quickly as you can type. For millions of developers around the globe, that IDE is Vim, and in this article I’ll show you how Vim can help you to write PHP code faster than ever before.

Introducing Vim

Vim is a descendant of vi, the now legendary text editor that has been a mainstay of Unix-based operating systems since technology luminary Bill Joy released the first version in 1976. Vim, which stands for “Vi improved,” is also not exactly a new entrant to the software community, having been first released by Bram Moolenar in 1991. Despite the age, Vim remains under active development today, with a new version released in August of 2010.
An open source project, Vim is available on all major platforms, Windows included. You can download the latest version from the official Vim website, or if you’re running an operating system such as Ubuntu, you’ll find it within your package manager.
Vim, like vi, is a modal editor, meaning its behavior is dependent upon the use of meta keys, which determine whether the editor is in insert mode. This sounds confusing, but it’s precisely this feature that allows you to write code and navigate the editor’s features with incredible speed.

Vim for PHP Development

Presuming you’ve installed Vim, follow along as I create a simple PHP script using the editor.
Begin by opening a command prompt and navigating to a convenient location within your development Web server’s root directory. Then execute the following command to begin editing a PHP script named books.php within Vim:

%>vim books.php

The Vim editor will open, although except for the status bar at the bottom you’ll hardly be able to distinguish it from the command line. When in insert mode, Vim is indistinguishable from other editors in that you’ll type as normal, using the Enter key to proceed to the next line and the Backspace key to delete characters. To enter insert mode, press i. Once done, the status bar will indicate that insert mode is enabled.
Next, type the following script into the editor.

  $books = array(
    "The Sun Also Rises",
    "The Dome",
    "The Jackal"
  if (count($books) > 0)
    echo "You must like to read!";
  } else {
    echo "Want to play video games?";

If you make a mistake while typing, try undoing your changes using the undo command (Esc followed by u). Press u multiple times to undo more than one change.

Vim Command-mode Tasks

To save the file, you’ll need to leave insert mode and enter command mode. Do this by pressing the Esc key. Then press the colon (:) key and finally the w key to write, or save, the file. While this sequence may at first seem awkward, the task will soon become second nature.
If you wanted to save the file and quit Vim, press Esc followed by the sequence :wq.
Now suppose you were doing a bit of refactoring and wanted to change the variable name $books to $library. You can use Vim’s global search-and-replace feature to perform this task with amazing speed. To do so, enter command mode (again by pressing the Esc key) and type:


This command tells Vim to search (%s) for the string $books and replace it with the string $library (doing so globally %g).
This is just one of literally hundreds of command-mode tasks at your disposal. For instance, suppose you wanted to edit two files simultaneously within Vim. Given the sparse interface, this seems impossible, right? Once again enter command mode (Esc) and type :new. This will open a second “window” within Vim, as depicted in Figure 1.

Click here for larger image

Figure 1. Editing Multiple Files in Vim
To switch between these windows, you’ll use the command mode sequence Ctrl+w and then either k to enter the top window or j to enter the bottom window.
Incidentally, if you prefer a tabbed interface, try opening a second file using the command :tabe filename.php. You can then switch between tabs while in command mode using the sequence gt.:tab.