Using Vim for Your PHP development

Source: Internet
Author: User
Tags configuration settings
by W. Jason Gilmore

How much of your average work day are 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 Tim E, and the sounds and sights surrounding those of us who work in non-traditional environments such as the local coffee sho p, 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-write, organize and refactor code as quickly as you can type. For millions of developers around the globe, which IDE is vim, and in this article I'll show you what Vim can help you to WR ITE PHP code faster than ever before.

Introducing Vim

Vim is a descendant of VI, the now legendary text editor, which has been a mainstay of unix-based operating systems since TE Chnology luminary Bill Joy released the first version in 1976. Vim, which stands for "Vi I mproved," was also not exactly a new entrant to the software community, have been first rele ased 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 your ' re running an operating system such as Ubuntu , you'll find it within your package manager.

Vim, like VI, was a modal editor, meaning its behavior was 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 so allows you to write code and navigate the editor ' s features WI Th 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 direct Ory. Then execute the following command to begin editing a PHP script named books.php within Vim:

%>vim books.php

The Vim editor would open, although except for the status bar at the bottom you'll hardly be able to distinguish it from th e command line. When in insert mode, Vim was indistinguishable from other editors in so you'll type as normal, using the Enter key to Pro Ceed to the next line and the Backspace key to delete characters. To enter insert mode, press I. Once done, the status bar would indicate that inserts mode is enabled.

Next, type the following script into the editor.

 
  0)   {     echo "must like to read!",   } else {     echo "want to play Video";   }?>

If you do 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 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 is at first seem awkward, the task would soon become second nature.

If you wanted to save the file and quit Vim, press ESC followed by the SEQUENCE:WQ.

Now suppose your were doing a bit of refactoring and wanted to change the variable name $books to $library. You can use the Vim ' s global search-and-replace feature to perform the this task with the amazing speed. To does 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 the literally hundreds of command-mode tasks at your disposal. For instance, suppose you wanted to edit the files simultaneously within Vim. Given the sparse interface, this seems impossible, right? Once again enter command mode (ESC) and type:new. This would 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 and in command mode using the sequence Gt.:tab.

Even with 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 Comman D 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 would read each time of the program is started. The above command would 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 this 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 would recognize that the five functions shown in this list is just a few supported by PHP 5.3.X . This was because the command-completion file tends to lag behind the latest release, so if you suspect that command you ' re looking for isn ' t in the Select box, refer to the manual just to be sure.

Taking Advantage of Vim Plugins

Vim is an extensible IDE and developers around the world has created hundreds of useful plugins that make your life as a Developer much easier. Project is one such plugin, which allows your to manage and access files associated with a particular project easily. Figure 3 illustrates how project would 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

Conclusion

As this tutorial demonstrated if you become familiar with Vim ' s command sequences, it's possible to write and manage SC Ripts with amazing speed.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.