I just watched Daniel Bachhuber’s “The Zen of WordPress Coding” talk from #wcphx in February.1 There’s some really good stuff so I definitely recommend it if you’re a WordPress developer. He used a tool that I had never heard of called ack. Ack is “designed for programmers with large heterogeneous trees of source code…” — it’s faster than grep.

Daniel talks about some useful options for ack that make it especially useful for WordPress in a command like ack --before-context=10 --ignore-dir=wp-admin 'function esc_'. Find all the function definitions that start with “esc_”, ignore the wp-admin directory, and print the 10 lines before the function definition because that’s where the documentation is — genius. As soon as I saw this I knew it’d be something I could use all the time, so I opened the dotfiles.


First, a shortcut function called a that runs a similar command and automatically wraps double quotes around the entire arguments string.

function a() {
  if [[ $ARGC -eq 0 ]]
  then ack
  else ack --before-context=10 --pager='less -FRX' "$*";

It checks that there were actually arguments specified and then runs ack with --before-context=10 to show the 10 lines before every result, --pager='less -FRX' so the results are paged and the formatting is kept (it also automatically quits if the results aren’t longer than one page), and "$*" which takes all the arguments and wraps them in double quotes. Now I can do something like a function esc_.

af esc_

Then I realized that it would probably be useful to have a shortcut since looking up functions is probably the most useful part of this in the first place. So, I created af.

alias af='a function $*'

This one is even more straight forward. Essentially, it turns what would have been ack --before-context=10 --pager='less -FRX' 'function esc_', or a function esc_, into af esc_.

  1. The slides for Daniel’s talk are available on his website.