Use node to change your bash prompt

I use the bash shell with git completion on my Mac. It’s awesome except it makes my bash prompt (PS1) kind of long and ungainly:

I wanted to optimize space in my terminal, so I searched for alternative ways to display the bash prompt and found an elegant solution on askubuntu.com that reduces each directory in PWD to its first letter by evaluating the output of the `pwd` command and piping it through sed (streaming editor).

It works really well, but I wanted the last portion of the path displayed as the full directory name. I’m not familiar with sed syntax, and found myself thinking: I know how I would do this in node.js. So I copied the example, opened `.bash_profile`, and hacked up some code.

Turns out that when you invoke node with the `-e` parameter it will evaluate the string that follows. That gave me the exact prompt that I wanted:

Is it elegant? Not really. Is sed syntax more terse? Absolutely. But you know what, I do what I want and I had fun doing it. 😀

And yeah, I know, zsh.

Leave a Reply

Your email address will not be published. Required fields are marked *