VIM for PHP Developers Part 3

VIM Plugins for PHP Developers  

VIM for the PHP Developer Part 3

Part 1 - Part 2
This article is about the plugins, that help make VIM use full for PHP development. This helps bring VIM to the level of a Full IDE.  I have found instructions on how to incorporate a php debugger into VIM, but as all of my projects run on REMOTE servers, it is not practical for me to run at this time.  I may do another tutorial on the debugger and possibly ctags.  

NERD Tree

This is by far the most useful plugin I have discovered yet for VIM.  It displays a file tree break down listing files and folders for you to view. Here is a screen shot showing it in action.

You can download this plugin HERE
http://www.vim.org/scripts/script.php?script_id=1658

Install:
To install simply unzip the file from that site into your .vim directory.

Quick Access:
To ad a shortcut to this function add this to your .vimrc file (please note I use to access NERDTree, you can use any key binding you like just replace with your key of choice:

" Plug-in key maps
" Toggle Nerd Tree using the F2 Key
map  :NERDTreeToggle

" Tell Nerd tree to ignore backup files, you can add more
" file types to ignor here...
let NERDTreeIgnore=['\.orig$','\.bak$']

Usage:
First... the documentation they create can be found using this command

:help NERD_tree.txt

If you want a quick run down of they keys and functions I use most:

o    Opens the currently selected file in the current buffer
t    Opens the currently selected file in a new tab and switches to that tab.
T    Opens the currently selected file in a new tab in the background.
i    Opens the currently selected file in a Horizontally split view
s    Opens the currently selected file in a Vertical split view
R    Recursively refreshes the display of files starting with the current root directory.

There are tons more features and options to this plugin, but this should be enough to really get things going.

phpDocumentor for VIM

This alone has saved me a ton of time, and I have only used VIM on 2 projects.  This is a plugin that generates the phpDoc tags for classes, functions, variables, and documents.  If you want to write good code... this is a must.

You can get it HERE:
http://www.vim.org/scripts/script.php?script_id=1355

Install:
Copy the file "php-doc.vim" into your pluggin directory.

Usage:
When you want it to generate a phpDoc tag for you, simply put your cursor in the item you wish to have it document for you and press "CTRL-p" It will automatically generate as much information as it can based off of the type of item you are on. You may have to add or modify some information but it is a lot faster than manually typing out the information.

SnipMate

This is another AMAZING time saver.  It takes a while to get used to and know what the "triggers" are, but I can write a complete (documented) controller for Codeigniter (a php framework) in under 5 seconds.  The allows you to fill in all the "stuff" you would normally have to type but it does so for you. For example if I typed "if" and pressed TAB, it will fill out a complete "if" statement with conditions, brackets and comments.  You can add your own custom snippets.  (I have added quite a few for Codeigniter, especially the database stuff I use on almost every project.)

You can get it HERE:
http://www.vim.org/scripts/script.php?script_id=2540

Install:
simply unzip the snipmate.zip into your .vim directory. (This will overwrite any previous versions of snipmate or snippets you previously had so care should be taken.) You should also update your help tags by running ":helptags ~/.vim/doc"

Usage:
You will want to know what the "triggers" are that fire off a snippet so I recomend you look at: "~/.vim/snippets/php.snippets" and look over the file.  this will show you the "snippet" (what I call a trigger) you have to type... and then what it will fill in when you hit tab.  You can add comments, and changeable variables.  It is very handy.  You can also modify the snippet file for any custom snippets you are looking for.  This is how I added a lot of the Codeigniter functions.

Activity Log

This plugin is a simple logger that keeps track of the files you edited and when you edited them.  It is not very complicated, but it doesn't need to be.  I don't know about you, but there are times that it is VERY helpful to go back and find out what files I had to change to accomplish something.  And that is not always easy to "remember" everything.  It also help keep track of time spent on portions of code.  Again this is not any type of "tracking" software, it just keeps up with what files you modified and when.  This is not always important... however it has saved my butt a few times, and I found it to be well worth it for me.  (Some people may not want to track what they are doing... that is ok too, but it helps me.)

You can get it here:
http://www.vim.org/scripts/script.php?script_id=3447

Install:
Just copy activity-log.vim into your "~/.vim/plugin" folder, and it will automatically start logging when you re-open vim.

Usage:
By default it puts it's logs in the "~/.vim/activty" folder however, if you would like to "tweak" it you can check the documentation for changing the path, and log format to better suit your needs.

MatchIt

Vim has a built in tag matching function "%".  (If you put the cursor on a "{", "[", "(", it will automatically take you to the opposite bracket open or close) This is very handy to find where a function or flow control is being opened or closed. But as a PHP developer we often work in multiple languages at the same time like Javascript, HTML and CSS.  Being able to find the opening and closing tags is very helpful (especially when buried in a few layers of divs... trust me).  This extends the tag matching function to be able to work in other (not necessarily programing) languages.

Install:
Unzip the file into your "~/.vim" directory.  (Don't forget to enable help with ":helptags ~/.vim/doc")

Usage:
This is pretty straight forward.  Put your cursor on the tag you are interested in and press "%".  It will take you to the oposing tag.


Tabular

This is VERY helpful.  While this does not qualify under my "must have" pluggins, I certainly use it a lot and find it very helpful in generating GOOD code.  This plugin allows you to align text by pattern match... This probably doesn't make sense.  Here is a better example:

var $cat = "orange";
var $fruit = "orange";
var $car = "black";
var $color = "#9";

After running tabular (by typing ":Tab /=")

var $cat   = "orange";
var $fruit = "orange";
var $car   = "black";
var $color = "#9";

It can automatically align text on multiple lines, in this case we specified to align the text using the "=" character. (This works even with multiple columns across multiple lines.) This is most helpful when working with large amounts of data, like big arrays, or an entire sequence of vars.

You can get it here:
https://github.com/godlygeek/tabular

Install:
Simply unzip the file in your "~/.vim/" folder.

Usage:
There are a lot of possible options so I will only explain the basics, see their documentation (and there are a number of good screen casts on this one too.) But in the above example you type ":Tab /="  while in command mode.  You can replace the "=" with any regular expression I think so it is pretty powerful. 

Taglist

This pluggin allows you to look over all of the objects in you open buffers.  It shows the vars, functions, methods, and classes listed out. It is a good "overview" pluggin that lets you quickly see the functions you have access to.  You can select the vars, functions, methods, and classes from the list and it will take you right to them (It also highlights where you are in you current code.)

You can get it here:
http://www.vim.org/scripts/script.php?script_id=273

Install:
Copy taglist.vim into your "~/.vim/plugin/" directory and copy the doctag.txt to "~/.vim/doc/" folder. (Dont forget to update your documentation)

Usage:
You can run the command ":TlistToggle" to turn the list on and off.  I mapped out a key to do this for me automatically.

tComment

This plugin allows you to comment and uncomment whole sections of code with ease.  You can comment or uncomment single lines or entire sections of code very quickly with this plugin.  I only need it every once and a while but it is nice to have when you need it.

You can get it here:
http://www.vim.org/scripts/script.php?script_id=1173

Install:
This is a bit more complicated it requires "vimball"  Check their documentation for how to install.

Usage:

gc{motion}    Toggle comments (for small comments within one line the &filetype_inline style will be used, if defined)
gcc           Toggle comment for the current line
gC{motion}    Comment region
gCc           Comment the current line

w3cValidator

This plugin validates your current document. (HTML or CSS, I haven't tried the others yet.) This is good if you want to produce valid markup. This requires that vim has been compiled with "+python" option enabled. (Many linux distributions, like Ubuntu, have this by default.)

You can get it here:
http://www.vim.org/scripts/script.php?script_id=3416

Install:
Simply copy the w3cvalidate.vim into your "~/.vim/plugin/" folder.

Usage:

:W3cValidate     to validate the current buffer
:W3cValidate     "[url_here]" to validate a hosted page
:W3cValidateDT     "[doctype]" to validate the buffer using the [doctype] override. Make sure the doctype is a valid doctype

ScreenShot

This plugin generates a complete (color) html layout of your vim screen.  I have never "needed" it but found it quite interesting, and could see times
where it could be very useful. (sometimes a picture is worth a thousand words?) This is FAR more useful if you are in a true console with no GUI, this way you can take "screen shots" and not have to be in a window manager.

You can get it here:
http://www.vim.org/scripts/script.php?script_id=1552

Install:
Simply save the ScreenShot.vim in your '~/.vim/plugin/' directory.

Usage:

:ScreenShot     This takes a screen shot of the current vim buffer and opens a new buffer with the HTML of the screen shot.

TaskList

This plugin generates an Eclypse style "tasklist" based on todo, fixme, or XXX (or custom tags).  I find this very handy especially when I comment code properly with things like "@todo Change this var for production".  It helps keep things orderly if you use it right.

You can get it here:
http://www.vim.org/scripts/script.php?script_id=2607

Install:
Configure the tasklist.vim, to taste, and copy it to your "~/.vim/plugin/" directory.  

Usage:

:TaskList    Opens the task list window.
q        quits the tag list window
Place the cursor on the item you want to go to, and hit  (aka ENTER) and it will take you to that item and close the todo list. 

Summary

This should provide a very good "base" to work with for PHP Web Development in VIM.  There are still some things that I find to be somewhat lacking, but overall I prefer this to any of the full blown IDE's I've used.  (Like Eclypse PDT, Komodo, or Netbeans.)  I find if working over a network connection (remotely) some of the syntax highlighting can take a while when typing, but aside from that I have found VIM to be an excellent coding platform.  I will probably do one more in this series for advanced plugins and configurations that I have found to work.  But that will only be after I have used it for a while. Enjoy and happy coding.

Page Information:
  • Tags: VIM, PHP, IDE, Developers
  • Description: Part 3 of VIM for PHP Developers Series.