Pluralsight blog Where devs, IT admins & creative pros go for news, tips, videos and more.
Supercharge your skills with expert-authored tech & creative training. Unlimited. Online. Get it now →
December 3, 2009

The sound of no command


On the last night of RailsCamp 6 I hosted a show and tell of shell commands.

I always love seeing what aliases, functions, and techniques other people use. And I hope that people will find something useful from mine.

Earlier in the weekend, Dr. Nic showed me an inputrc he uses with bash that searches his command history like command-r but uses the up and down arrow keys. You just start typing part of a command, then hit up-arrow and it shows the previous command that starts with the string.

Unfortunately, I couldn’t get it to work with zsh. Alexandros Podaras sent in a zsh-compatible version.

It only searches the beginning of commands, not the entire command including arguments.

Bo’s Method Missing

Bo Jeanes took it up a notch with a method_missing-type handler for the fish shell.

He can do things like paste a bare URL into the terminal and it will download the file and untar it for him automatically. Git URLs will be cloned automatically. There is also an artificial shortcut for running Cucumber stories by typing just the name of the feature file.

I discovered that both zsh and bash have similar capabilities! Oddly, it only works with zsh on Snow Leopard, not Leopard OG.

For bash, use the trap handler and shell_method_missing script
found at right.

For zsh, write a function called command_not_found_handler that calls a custom Ruby script. The Ruby script will try to find the intended command and run it.

# For zsh. Add to your .zshrc
function command_not_found_handler() {
  ~/bin/shell_method_missing $*

I’ve modified the shell_method_missing.rb script to easily clone repositories from GitHub. Type the name of a user and the name of a project:

% bjeanes dot-files.git
=> Runs "git clone git://"

I can also install gems by typing the name and a .gem suffix.

% haml.gem
=> Runs "sudo gem install haml"

Currently, my zsh settings intercept git and http URLs and don’t pass it onto the command_not_found_handler. I haven’t figured out how to work around this yet.

One could go wild with the possibilities:

  • Run specs by name
  • Run rake tasks
  • Deploy a website by typing staging or production

Try it out!

About the Author

is VP of Open Source at Pluralsight. He previously founded PeepCode and is an all around entrepreneur, developer, designer, teacher and athlete. Follow him on Twitter at @topfunky.