Thursday, January 11, 2018

Quick Terminal/iTerm2 Hack

My shell prompt uses unix status codes to either return a go-get-em Unicode smiley, when everything's going well, or a shrug, when a command failed.

To get this into my shell prompt, I use a shell prompt function, which sets than the usual $PS1 environment variable. You need the function because it sets the value of $PS1 every time something changes. This also makes it easy to, for example, display whatever git user, or set of users, I happen to "be," since I use git pair.

A nice side effect is that you can call arbitrary functions in there. tab-title $(this) sets the title of the Terminal or iTerm2 tab to whatever my current git branch name is. Since the shell prompt function is executed every time my shell needs to display a new prompt, the tab title updates every time I change git branches.

A couple notes: first, as you might guess from the comment, I found the code for tab-title on the big wide Web somewhere. Second, this is a function I wrote years ago, when it was difficult to get git branch names on the command line. I think there are easier ways to do it now.