Dynamic Konsole Session Tabs/Names

I was finally annoyed with having to rename my Konsole sessions every time I ssh'd somewhere, and I finally found a really slick way to do this dynamically. It's only a slight pain of having to put a custom \$PS1 on every user you touch, but this would typically only be done when you get a new account on a computer somewhere - it only needs to be done in one big batch once.

I had a little trouble understanding Konsole's Tips (Help -> Tip of the Day), so I finally managed to find a site describing the PS1 modifications in a little better detail.

So now I have this on my laptop in my .bashrc:

export PS1=$PS1"\[\e]30;\h:\W\a\e[28;0t\]"

This sets the PS1 variable to what it normally is, with an escape code to set the Konsole session name to [short-hostname]:[current-working-dir] and another escape code to set the color to black (this is so it switches back to black once we get home again).

In my root .bashrc's I modified it slightly to be:

export PS1=$PS1"\[\e]30;\h:\W\a\e[28;10485760t\]"

10485760 equals #A00000 converted from hex to decimal.

Finally, on my servers i changed my home dirs' .bashrc to be:

export PS1=$PS1"\[\e]30;\u@\h:\W\a\e[28;160t\]"

I've also setup a different color for my work servers so I can quickly see whether I'm at home, a root terminal (oh no!), or a work or home server.

Update 16:28

I updated the \w to be \W in the examples above. I found that \w was very unwieldy once you got to a deep path, and your session name was incredibly long. This was my old home .bashrc line:

export PS1=$PS1"\[\e]30;\u@\h:\w\a\]\e[28;0t\]"

Update 2006.01.24 11:11

I updated it again to fix a problem I was having that wrapping on the first line was breaking. I also removed \u@ (username) from my local computer and any servers that I typically don't use multiple accounts on since my main user and root are two different colors. This makes my tabs shorter and I can cram more in one Konsole. A side-effect of configuring Konsole's tabs this way is that you can't set the session name to something custom at all anymore - every time you hit a key it's reset. I think this might be a bug and I may file it as such with KDE.

links

social