Heim >Backend-Entwicklung >PHP-Tutorial >Using Vim for Your PHP Development
by W. Jason Gilmore
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 VimVim 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 i mproved," 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 DevelopmentPresuming 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.
<?php $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 umultiple times to undo more than one change.
Vim Command-mode TasksTo 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:
:%s/$books/$library/g
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.
Even with 13 years of experience developing websites with PHP, I still manage to forget the name or parameter ordering of various native functions regularly. Vim removes the need to refer to the manual constantly thanks to its native omni completion feature, which supports command completion for dozens of languages, PHP included. To enable this feature, create a new file named .vimrc in your home directory and add the following line:
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
The .vimrc file is useful for managing configuration settings, which Vim will read each time the program is started. The above command will enable code completion for the PHP language. Once saved, restart Vim and open the books.php script again. Enter insert mode and type da, followed by Ctrl+x, Ctrl+o, and you'll see a select box similar to that shown in Figure 2.
Click here for larger image
Figure 2. Using PHP Command Completion
Use your arrow keys to select the desired function, pressing enter to insert it into the text. Experienced PHP developers will recognize that the five functions shown in this list are just a few supported by PHP 5.3.X. This is because the command-completion file tends to lag behind the latest release, so if you suspect that the command you're looking for isn't in the select box, refer to the manual just to be sure.
Taking Advantage of Vim PluginsVim is an extensible IDE and developers around the world have created hundreds of useful plugins that make your life as a developer much easier. Project is one such plugin, which allows you to manage and access files associated with a particular project easily. Figure 3 illustrates how Project will provide you with immediate and convenient access to a project's files. (See the Project homepage for configuration instructions.)
Click here for larger image
Figure 3. Using the Project Plugin
As this tutorial demonstrated, when you become familiar with Vim's command sequences, it's possible to write and manage scripts with amazing speed.