My WordPress bash functions

Since my MacBook Pro arrived less than three weeks ago, there are a few applications I’ve yet to close: a browser (Chrome primary, Firefox secondary), an editor (TextMate, and some TextWrangler), and Terminal. I’ve found that as a developer, Terminal is where it’s at.

Here’s a quick rundown of my bash functions file so far. Some of these functions were borrowed from or inspired by fellow core developers Peter Westwood and Ryan Boren, and others just came out of a need or a shortcut. They’re all relatively simplistic, but they’re a window into how I work.

In particular, the way I interact with patches has evolved quite a bit when I switched over to Mac. To think I believed the CLI would slow me down after dealing with TortiseSVN! (Thankfully, I haven’t had to merge too many patches that no longer apply cleanly.)

Downloading and Applying Patches from Trac

# Apply a Trac patch.
trac_patch() {
	curl "$1?format=raw" | patch -p0
# Apply a Trac patch on a clean repo.
clean_patch() {
	svn cleanup
	svn revert -R *
	svn up
	curl "$1?format=raw" | patch -p0
alias tp=trac_patch
alias ctp=clean_patch
# usage

I don’t use the clean_patch command much, mainly because I have a good idea of the status of my repository when I’m applying a patch. This one in particular was actually inspired by Automattic’s Barry Abrahamson, who warned me at WordCamp SF that Jane Wells can break anything.

(Necessary backstory: Jane was set up with a local WordPress install on her new MacBook Pro, and thus a simple command that cleans up a mess and applies a patch is exactly what she would need. No more commit-it-and-let’s-see-how-it-looks come 3.1.)

trac_patch came from Peter, here. Of course, trac_patch on steroids would be this python script from Nikolay.

SVN: Files Modified

	svn stat --ignore-externals | grep '^[^?X]'

This one gives me a quick look at core files I’ve modified. svn diff sometimes produces too much output for me to easily ensure I’m not about to commit cruft in another file, so this is a nifty sanity check.

I otherwise haven’t come up with anything else to shorten svn commands. They’re pretty short as it is (and baked into the back of my eyes).

Grepping through Core

jsgrep() { # search development scripts
find . \( -name "*.dev.js" -print \)  | xargs grep -n "$1"
cssgrep() { # search development css files
find . \( -name "*.dev.css" -print \)  | xargs grep -n "$1"
phpgrep() { # search php files
find . \( -name "*.php" -print \)  | xargs grep -n "$1"
wpgrep() { # search php files and development scripts/styles
find . \( -name "*.php" -print -or -name "*.dev.js" -or -name "*.dev.css" -print \)  | xargs grep -n "$1"
# usage
wpgrep 'function the_content'

Also inspired by Peter, these commands are quite awesome for finding what you need when you need it.

Script Compression

min() {
java -jar ~/downloads/scripts/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar -v --type $1 -o wp-admin/$1/$2.$1 wp-admin/$1/$$1
# usage (I've run these two more than I care to count)
min js nav-menu
min css nav-menu

We use the YUI Compressor for scripts and styles. After toying with a few variations, I came up with the above. Originally inspired by Ryan in a comment on Trac, I traded off some functionality for less paths to traverse. I still have a variation that allows me to specify any two files, however, for the rare instance when I’m not compressing core scripts.

Opening Files Fast

open $1 -a
# usage
o wp-config.php

Since I find myself in Terminal so often, I often find I want to open files from there. My o command operates exactly like TextMate’s mate command, but o is shorter, and I can also change it to another application if I move to another one.

That command helps, but I’m not sure what I’d do without these commands:

# Shortcuts for opening WP files insanely fast.
	o wp-includes/$1.php
	o wp-admin/$1.php
	o wp-admin/includes/$1.php
	o wp-admin/css/$
	o wp-admin/js/$
	o wp-content/themes/twentyten/$1.*


wpi post # wp-includes/post.php
wpai schema # wp-admin/includes/schema.php
wptt style # wp-content/themes/twentyten/style.css
wptt functions # wp-content/themes/twentyten/functions.php
wpjs nav-menu # wp-admin/css/

These evolved from a weakness of the o command — it was slow for traversing the same paths over and over. I knew what the file was called and where it existed, and I just wanted to open the file now.

So there you have it. Are there any functions you use that can speed up a WordPress or web development workflow?