First off, if you’re not using iterm 2, it’s awesome and you should. Recently I found that iterm 2 has functionality that can help you automate actions based on things that happen in your terminal. This particular trick has proven helpful recently.
I use Bitbucket for much of my work. When I push up a new branch, Bitbucket responds with a URL that I can use to open a pull request for that branch. For the longest time, I’d highlight from Terminal, copy and paste into a browser and do my pull request. Shortly after that I learned you can right-click anywhere in the URL and select “Open Selection as URL” and it will open a browser. A bit after that I learned that you can Command-Click on the URL in the terminal and the browser will open. That’s pretty easy, but I’ve got a way that is even easier.
For reference, this is an example of what it might look like when I open a pull request in Bitbucket:
In iterm 2, you can tell it to watch for certain patterns and do something with them. You can make a sound, bounce the dock, run a command, open a Growl and more. For this, I want to run a command. In iterm 2, right click and select “Edit session…”.
Next, choose the Advanced tab and click on Edit under “Triggers”.
The triggers in iterm 2 work on matching a regular expression and doing something with it. The parts of the URL that will or could change will be the username, repo name and the branch name, but really, we don’t even need to be that specific.
For the regular expression, enter this:
There’s a few important parts with the regular expression if you’re not familiar. First of all, the parentheses around the part that matches the URL is to capture the pattern. In parameters, the \1 is substituted with the matched URL. I put the ‘remote:.+’ part in so that it will only trigger if the response looks like it comes from pushing up code. Without it, this will trigger on any bitbucket.org link that shows up in your terminal for any reason.
The last bit that you may want to change for your purposes is the part after source=. In mine, it’s “[A-Z]+-\d+”. This means it’s matching one or more capital letters followed by a dash followed by one or more digits. This is because our standard is to name our branches after our JIRA tickets. They look something like BUG-1234 or APP-443. In fact, the regular expression wouldn’t have worked on my first terminal example because “my-branch-name” doesn’t match that pattern. If you want to simplify it and make it work no matter how you name your branches, you could replace that pattern with .+:
With this in place, any time you push up code and Bitbucket responds with a Pull Request URL, iterm 2 will run “open” on the URL which results in opening your default browser to that URL.
For me it saves a bit of time and it’s pretty cool. You could also use these triggers along with a command like ‘tail -f ‘ on some log file and have iterm 2 let you know whenever a particular phrase shows up in a log file.
Hope this helps.