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 tp http://core.trac.wordpress.org/attachment/ticket/13237/alot.patch
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
mod(){ 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/$2.dev.$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
o(){ open $1 -a textmate.app } # 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. wpinc(){ o wp-includes/$1.php } wpa(){ o wp-admin/$1.php } wpai(){ o wp-admin/includes/$1.php } wpcss(){ o wp-admin/css/$1.dev.css } wpjs(){ o wp-admin/js/$1.dev.js } wptt(){ o wp-content/themes/twentyten/$1.* }
Usage:
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/nav-menu.dev.js
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?
Very very neat 😀
I only had Westi’s patch command until now.
I’m a huge fan of cli. I’ll have to implement your grep shortcuts…
For grepping,
ack
is usually good enough for me; it ignores most of the stuff I don’t want to see, like SVN files (rarely does my searched text turn up in a compressed js file). And with ack you can use the Perl regex metacharacters, which for most people are more familiar.I’m curious about the file-opening shortcuts. Does the Mac terminal client not have autocomplete? To get
wp-includes/query.php
, for example, I just have to typew [tab] i [tab] q [tab] p [tab]
, which is one fewer stroke thanwpi query
, and it’s universal.It does have autocomplete, but I find
wpi query
better for me, if an extra stroke — I simply find it easier and more logical (personally) for how I tend to traverse the codebase in my head. When I ssh into my server, for example, and use vim, I rather seamlessly switch over to autocomplete.I have used ack, but I haven’t added it to my workflow yet. Really ought to. As I said on Twitter, I’m normally not doing complex searches. But that will probably be the next thing I look into in order to discover more efficiency.
The
min
one is good. That’s what I searched for. I think I will subscribe to this blog 🙂These days I do most of my searching and opening in a TextMate project. Neat shortcuts and regular expressions, if you need them. Just type ⌘-T and you can open any file fast. The aliases for opening and searching are cool, though.
I still have a couple of
svn
aliases. My favourite is:alias v='svn diff | vim -'
. The rest are here: http://github.com/nb/dotfiles/blob/master/shellfiles/aliases#L14By the way, have you tried to copy a file in the shell? 😉
Heh, yeah… Definitely borked cp there. Really like your aliases — will definitely need to snag some.
I use TextMate as well, but I haven’t quite gotten used to it. I’m in the Terminal so often though, it just seems quick to me.
These scripts are great and really helpful. My one question (and a note for people to be on the lookout for) is that by using the alias cp for the clean patch function, you are taking over the cp program used for copying. This can cause other bash scripts that use cp to fail. Otherwise, thanks so much for sharing these.
Good catch. Goes to show how rarely I use cp, I guess. I’ll rename it to ctp.
You’ll need make sure you call
unalias cp
otherwise just changing the script won’t give you cp back.Thanks for sharing these! You inspired me to write one of my own:
http://wordpress.pastebin.com/QVMSJKz9
It will detect wp-config.php in the current directory or one directory deep, then use the values there to dump the WordPress database to a file.
This is an impressive use of two of my favorite things, Bash and WordPress. I was looking up some bash functions to build my collection and came across these. Great job!
You can grep only in certain file types like this:
grep -in --include=*.php 'something' *
The -in is “case insensitive” and “show line number” and are optional. The magic is in –include=*.ext