My Favorite Vim Tips

Vim IconHere are some of my favorite tips for working in Vim. I use MacVim but everything below should apply to any version of Vim you are using. Vim is such an amazingly powerful editor you can use it for years and still learn new stuff all the time. This is a small collection of features I use most often and find most helpful. Hopefully they will be helpful to you as well.

Continue reading

If Statement Variable Scope In PHP

PHP Logo Variable scope is the context within your code in which a variable is defined and able to accessed. If you try to access a variable that is out of scope, the variable will be undefined and you will not get the results you are expecting.

In PHP, variables all exist within the same scope when your code is inline or included in an include or require statement. Classes and functions have their own scope. If statements do not have their own variable scope.

Continue reading

WordPress register_activation_hook not firing

WordPressThe register_activation_hook() in WordPress looks for the “wp-content/plugins” directory in the plug-in file’s canonical pathname. So, if your files physically live somewhere other than in your WordPress tree, WordPress (PHP) calculates inappropriate paths.

The Solution: Move all your plug-in files into your WordPress tree and the activation hook should start firing.

Sort Text Column Numerically in MySQL

MySQL LogoNormally when you want to sort numerically on a column in your database you’d make the column some sort of numeric type such as an int. Sometimes, however, you are stuck with someone else’s schema and they have decided to store numbers in a text type column and you need to sort your results numerically because an alphabetic sort on a number does not produce the results you want. There is a quick trick to that makes this easy.

Continue reading

John 3:16 In PHP and Ruby

John 3:16 in PHP

$son = $god->loved($the_world);
foreach($whosoever as $person) {
  if($person->believe($son)) {

John 3:16 in Ruby

son = god.loved(the_world)
whosoever.each do |person|
  son.everlasting_life!(person) if person.believe?(son)

Quickly Folding Functions In Vim

Vim IconThe is really a Vim tip on how to fold any code block including functions. It’s really helpful if you want to collapse a function or any other type of code block. Best of all, it is quick and easy to do.

Navigate your cursor somewhere inside of the code block you want to fold. You do not have to have your cursor on the opening brace, anywhere in the block is fine. Make sure you are in command/normal mode and type:


To save your folds between vim sessions you need to issue the command :mkview otherwise when you close vim your folds will be lost (your folds, not your code). To make life easier on you, you can have your folds automatically saved for you by adding this to your .vimrc file

au BufWinLeave * mkview
au BufWinEnter * silent loadview