Saturday, March 09, 2013

bash tip: setting terminal tab title programatically

This one has been annoying me for quite a while - looking at the string of tabs in the mac's Terminal App, all of them titled "bash" except for the one trying to download webkit which is usefully titled "git".

So I finally decided to do something about it, namely google the solution. Here're the results:

# add these to your .bash_profile
# title sets your terminal tab's title to any string you pass in.
# from http://stackoverflow.com/a/1687708
function title() { echo -n -e "\033]0;$@\007";}
# cd modifies the cd command and uses title to set it to whatever the current dir is.
# from http://superuser.com/a/296555
function cd() { builtin cd "$@" && title ${PWD##*/}; }
# Confirmation that title() works on a mac came from a book called "Mac OSX for Unix Geeks" on Google books.
view raw term_title.sh hosted with ❤ by GitHub
As a bonus, the cd() function adds the ability to change the title to whatever directory you currently are on - which works just fine when you're in your projects directory :)

I think this should work on Linux as well; confirmation TBD.


No comments: