Posted on February 17th, 2009

One of the smartest people I’ve ever digitally met, Ryan Paul, taught me this awesome tip for doing a project wide search in vim. I needed to look for all occurrences of the patter _Models_ in a PHP project I was working on and you can do it right inside of vim – the best text editor ever invented.
- Open vim and make sure you are in the top level folder of your project by typing :pwd
- Then type :vimgrep YourPattern **/*.php
- To open your search results in their own buffer type :copen
The **/ recursively searches through all your directories for you pattern. :copen opens the search results in their own buffer. You can use the arrow keys to move up and down through the list and hit enter to have it open that result in the main buffer. You can also use :cnext and :cprev to move to the next and previous items in the list. Perhaps you might bind those to keyboard shortcuts so you can move through the search results quickly.
Posted on December 12th, 2008
I like using Safari and I’m a web developer. Sometimes I’ll be developing a website and then decide to rename a page. The problem is Safari will cache the old pages’ URL and then auto-fill the old URL as I’m typing. Then I hit enter and shucks, I’m getting a File Not Found error. In FireFox you can just press Shift-Delete to get rid of the outdated URL. In Safari, to delete one, several, or even all of your cached urls used by the auto-fill feature, just delete the offending URLs from your history.
To do this you can go to Bookmarks –> Show All Bookmarks. Then in the upper right corner of your window you can search for the URL you want to delete. I’m frequently typing the domain of our development server so I can delete all of the development URLs they may be outdated. Once you see the URLs you want to delete, highlight them and press Delete. This is a little more cumbersome than Firefox’s Shift-Delete but at least it can be done.
Note: Both History and Bookmarks are used for the auto-fill feature. So even if you delete all of your history, the URLs from your Bookmarks will still be used for auto-fill. Of course, if you have bookmarked a page that is no longer available you probably want to delete that too.
Posted on December 10th, 2008
I Want Mulitple Gmail Signatures
I love using Gmail and I have several different email address for the various companies I work for. Gmail only lets me have one signature which is a bit of a problem because, depending on which from address I am using, I use different email signatures. There are a couple of Firefox add-ons that help with the problem but sometime I enjoy using other browsers like Safari. I have finally found the solution using QuickSilver and it’s Shelf feature.
QuickSilver And The Shelf
I’ve know about the Shelf on QuickSilver for a while now. It’s like a persistent clipboard. You can save snippets of text and then use that text later – like copying things to the clipboard but it stays there forever. So this was a good solution for my multiple email signature problem except that it took quite a few key stokes to get the text off the shelf and into my email. I set up triggers to do this but QuickSilver’s trigger display doesn’t handle the multi-line signature trigger very well. The name of the trigger spans multiple lines and overlaps the names of my other triggers. I hate that so I avoided using that as a solution. Also, it’s hard to think of enough unique keyboard shortcuts of each one of my five different signatures that don’t interfere with other shortcuts. So I created a trigger that just opened the shelf. Once the shelf is open I right-click the signature I want, then select paste from the actions menu. This was a fair solution, but the right-clicking and then hunting for paste in the pop-up actions menu was still a bit cumbersome.
Command Chains Can Be Stored On The Shelf
What I didn’t realize until today, was that you can store more than just text on the shelf. You can store chains of actions. So now my workflow is really nice. I have a keyboard shortcut that pulls up the Shelf then I just double-click on the signature I want and Blamo! my signature is in my email. Since this is a QuickSilver thing, I can use the signature for Mail.app, or Gmail and I can use any browser I want. It’s very nice. The one last tweak I wish I could figure out is how to get rich text in my signature because it seem that only plain text can be stored in QuickSilver’s shelf.
How To Store Command Chains On The Shelf
The huge realization I had today was that you can type Control-Enter rather than just Enter to make a chain of actions in QuickSilver. So I create the text (type . in the subject panel) for my email signature. Then tab to the action window and type paste the type Control-Enter and type Put on Shelf then click Execute. Now you can open your shelf, double-click your signature and it is pasted into whatever text area you were just in.
If anybody knows how to get rich text into the signature I would LOVE to know. I know that you can copy from Pages or Word or whatever and the bold, italics, links, etc come with you when you paste into Gmail. So I’ve thought about writing an AppleScript to open up a Pages document with my signature, copy the content to the clipboard, quitting Pages, then paste the clipboard. But the launching and quitting of pages takes too long and is more trouble than it’s worth.
Video Tutorial
Here is a quick video tutorial showing how to set all this up.
Posted on November 26th, 2008
There are bunches of examples on how to delete non-empty directories in PHP but none of the examples that I’ve seen take advantage of PHP5’s RecursiveDirectoryIterator which makes this an almost trivial task. If you haven’t already, please check out the SPL Standard PHP Library Classes. I keep a class of static utility functions and here is my function to delete non-empty directories recursively.
public static function deleteDir($dir) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if ($file->isDir()) {
rmdir($file->getPathname());
} else {
unlink($file->getPathname());
}
}
rmdir($dir);
}
Please note the RecursiveIteratorIterator::CHILD_FIRST parameter. Without that the listing will not be in the correct order. As you iterate along you will end up trying to delete directories before they are empty. This CHILD_FIRST parameter was added with PHP 5.1 so make sure you have at least that version installed before using this technique.
Posted on November 3rd, 2008
I just discovered a significant “gotcha” debugging some code today. Judging from the comments on a variety of blogs and forums I think many of us have had this question. To cut to the chase, if you aren’t getting your last insert id returned when you do a Zend_Db_Table insert, make sure that the array that you are inserting does not have an empty string or a zero set in the primary key field. So, if your primary key column in your database is `id` then this will cause you problems:
//NOTE: $table is an instance of class that extends Zend_Db_Table_Abstract
$data = array('id'=>'', 'color'=>'blue', 'size'=>'large');
$id = $table->insert($data);
echo $id; // --> will print an empty string
If you have to include the name of your primary key column as a key in the array you are tyring to insert, make sure it’s set to null NOT an empty string or zero.
//NOTE: $table is an instance of class that extends Zend_Db_Table_Abstract
$data = array('id'=>null, 'color'=>'blue', 'size'=>'large')'
$id = $table->insert($data);
echo $id; // --> will print the id of the newly inserted row
The reason for this is line 822 in the Zend_Db_Table_Abstract class:
if ($this->_sequence === true && !isset($data[$pkIdentity])) {
$data[$pkIdentity] = $this->_db->lastInsertId();
}
Notice the isset() condition. If primary key value in the data array you are inserting contains anything other than null then isset() will return true causing the lastInsertId() function to not get called. Hopefully this will clear things up and save us all alot of debugging time!
Posted on October 28th, 2008
UPDATED 6/10/2009: The checker works again.
I like the option to check Google PageRank easily no matter what browser I am using so I wrote this quick little script to grab the PageRank of whatever page you are looking at and then pop open a little window to display the rank. All you need to do is drag the link below to your bookmarks bar on your browser.
Check PR
Posted on October 28th, 2008
It is common to need to redirect from one controller action to another or even from one controller to another when coding with the Zend Framework. From my code reviews, I find that many people are using the _redirect() method from Zend_Controller_Action. This function takes a url and an optional array of options. This is a fine choice if you need to redirect to a completely different website but the vast majority of redirect in a web application is to another action within the same application.
The lesser known, and much easier, way to redirect within the Zend Framework is to use the Redirector Zend Controller Action Helper
The syntax is much easier since you don’t have to reconstruct a complete url, you just pass in the action and controller names. In fact, the only required parameter is the name of the action. If only an action name is given then the current controller is assumed to be the target controller. Here is a quick example.
_helper->redirector('target', 'example');
// NOTE: 'example' is optional since the default target controller
// is the current controller
}
public function targetAction() {
// Do some things...
}
}
?>
Posted on September 17th, 2008
Sometimes, especially when just testing things out, I don’t want to go to the trouble of having to create view script for my Controller Action and instead I just want to print something to the screen right from my Controller Action function. I guess because it’s sort of hard to find out how to do this in the ZF Documentation this is one of the most common questions I get asked – even from somewhat seasoned developers. The answer is in the Controller documentation and it’s easy to think that you ought to search under the View documentation to figure out how to turn this stuff off. Whatever the case, here’s the trick.
Read the rest of this entry »
Posted on August 21st, 2008
When using TextMate to code PHP, you may be annoyed by the way TextMate handles hitting the Enter key while in a PHP comment block. I would like the lead * to be continued on the new line. There are two ways to get this to happen.
Read the rest of this entry »
Posted on August 13th, 2008
Overloading PHP So That empty() Works
PHP5 has a much improved object oriented feature set. With great MVC PHP frameworks such as CodeIgniter, CakePHP, and the Zend Framework you should certainly be writing your own classes and taking advantage of objects in your coding. Using the magic methods that you get with PHP5 you can overload your classes so that you can do some very cool stuff.
PHP’s overloading deviates from the way other programming languages work. Overloading in most other languages enables you to define several different methods all with the same name but taking different types of paramters. PHP, on the other hand, uses overloading to handle calls to object members or methods that have not been defined or are not visible in the current scope of the caller.
A Quick Example Of Overloading
Read the rest of this entry »