Open BitBucket from Bash

posted by Steve Losh on October 8, 2009

If a lot of the repositories you work with are on BitBucket, you probably find yourself going to their project pages pretty often. You can avoid having to manually open a browser and type in a URL by adding a small function to your ~/.bashrc file.

For Mac OS X

Add this to your ~/.bashrc file if you use OS X:

bitb() {
    local P="$(hg paths 2>/dev/null | grep 'bitbucket.org' | head -1)"
    local URL="$(echo $P | sed -e's|.*\(bitbucket.org.*\)|http://\1|')"
    [[ -n $URL ]] && open $URL || echo "No BitBucket path found!"
}

Once you’ve added it, either open a new Terminal window or run source ~/.bashrc. From now on you can use bitb to open up the BitBucket page for the repository you’re currently working in.

For Cygwin on Windows

Add this to your ~/.bashrc file if you use Cygwin:

bitb() {
    local P="$(hg paths 2>/dev/null | grep 'bitbucket.org' | head -1)"
    local URL="$(echo $P | sed -e's|.*\(bitbucket.org.*\)|http://\1|')"
    [[ -n $URL ]] && cygstart $URL || echo "No BitBucket path found!"
}

Once you’ve added it, either open a new Cygwin window or run source ~/.bashrc. From now on you can use bitb to open up the BitBucket page for the repository you’re currently working in.

For Linux

Add this to your ~/.bashrc file if you use Linux:

bitb() {
    local P="$(hg paths 2>/dev/null | grep 'bitbucket.org' | head -1)"
    local URL="$(echo $P | sed -e's|.*\(bitbucket.org.*\)|http://\1|')"
    [[ -n $URL ]] && /path/to/your/browser $URL || echo "No BitBucket path found!"
}

Replace /path/to/your/browser with the correct path to your web browser.

Once you’ve added it, either open a new terminal window or run source ~/.bashrc. From now on you can use bitb to open up the BitBucket page for the repository you’re currently working in.

For Others

If you use an OS not listed here, the important line that you might need to change is the last one. Replace open in the OS X example with whatever you use to open URLs from the command line, then add your change as a comment here so other people can use it!

Tired of manually firing up a web browser to go a repository’s BitBucket page?