OSX Fish Functions to open Chrome from the Shell

These functions are quite handy, and allow you to do things such as copy and paste errors and google for them without having to manually open a browser. You have to enclose arguments to both functions in single quotes, e.g. ‘chrome ‘http://www.google.co.uk’ and google ‘one two three four’. You should add these to your config file at ~/.config/fish/config.fish and make sure Google Chrome is already installed.

function chrome
  /usr/bin/open -a '/Applications/Google Chrome.app' $argv[1:]
end
function google
  /usr/bin/open -a '/Applications/Google Chrome.app' "https://www.google.co.uk/search?q=$argv[1]"
end

Fish Shell Scripting

A few months ago I switched across from Bash onto the Fish shell, on my main development machine. I can’t get enough of its fast autosuggestions and its sane scripting language. Compared to Bash it is very fun to use.

Today I wrote a script to automate the backup of my development workspace onto a private bitbucket account. Bitbucket is good because it allows unlimited private repositories, but it caps the size of these repos at 2GB max, with some features disabled after 1GB. This means that I want to find out the size of my repo before automatically backing it up. This script does this:

#!/usr/bin/env fish

set size (du -sm . | awk '{print $1}')
echo "Workspace is $size MB"
if math "$size > 1000"
  echo "Workspace too big to commit!"
  exit
else
  echo "Workspace is under 1GB, OK to commit"
end

git add .
git commit -m "Automated backup"
git push origin master

Each individual directory additionally usually has its own git config which is synced to the separate repos for the code I’m actually working on. But if my development machine is stolen or somehow destroyed, or I want to quickly replicate my environment on another machine with access to the original repos, then I can regenerate the associations between the different directories. It also makes me mindful of not committing images or other video assets.