The television will be revolutionized Squirrels
May 24

One feature the Unix shell offers is customizing prompts. Most ‘power users’ make use of the feature, and it is indeed very handy. However, it’s easy to go completely overboard and end up with a prompt like this:

[21:52:15] [fred@webhost:/var/log/apache] $

The problem with a long prompt is that you quickly hit the right hand edge of the screen and your command starts wrapping. If you use KDE, however, there’s a better way.

Konsole

The xterm program introduced an escape sequence to set the window title. That can help a bit, because now you can put some of the boring info up in the window title, and reserve the prompt for path information.

I use KDE, and the Konsole terminal program in KDE goes further than xterm. As well as letting you change the window title, you can also have multiple terminal sessions in tabs (like Firefox web page tabs), and chance their titles too. The purpose of this posting is to explain the extra functionality in Konsole, and how to make use of it with the bash shell (default in Linux).

Konsole window titles take the form ‘window title - tab text - Konsole’. Tab text and window title can be updated with control sequences:

  • echo -ne '\033]30;MESSAGE\007' sets the konsole tab text to MESSAGE

  • echo -ne '\033]0;MESSAGE\007' sets the first part of the konsole window title to MESSAGE

If you have KDE 3.5 or later, there’s a new code which lets you change the color of the tab text: echo -ne '\033[28;RGBt'

In the new code, RGB is the hex RGB value converted to decimal. Hex RGB values are what's used in HTML (#rrggbb), so the only tricky bit is converting to decimal. For example, saturated red would be #ff0000 hex, or 16711680, so the ESC code would be \033[28;16711680t.

Bash

OK, that's the escape sequences for Konsole, but how do we use them dynamically rather than via the echo command?

Bash allows you to include escape sequences in your prompt string to insert bits of handy data. The ones I use are:

\u - your current user name.
\h - the current (short) host name.
\w - the current working directory (relative to ~ if possible).
\$ - $ if you're a regular user, # if you're currently root.

Another handy one is \! which displays the history number. This allows you to repeat a recent command that's still displayed on the screen by entering ! followed by the history number displayed in the prompt where you typed the command; for example, !33 repeats command 33 in the shell history.

In addition, there are bash prompt escapes for \033 and \007: \e and \a respectively. Finally, \[ should be placed before any sequence of characters that's expected to include special control codes, and \] at the end.

So, let’s put it all together. Here’s a magic incantation for your .bashrc:

export PS1='\[\e]30;\h\a\e]0;\u - \w\a\]\w \!\$ ‘

That should set your prompt to something nice and minimal, like this:

~ 4$

This indicates that you’re in your home directory (~), that you’re a regular user ($), and that whatever you type at this prompt can be repeated later by entering the command !4.

In addition, your current Konsole tab will contain the hostname of the machine you’re logged in to, and the top of the Konsole window should show the username, directory and hostname in this format:

john - ~ - webhost - Konsole

I don’t put the username or directory into the tab text, because tabs need to be narrow. If they aren’t, you have to start scrolling through to find the right tab, which is a pain. This is also why I don’t find the Konsole option “set tab text to window title” to be very useful.

If you’re using KDE 3.5 or above you can squeeze in a little more info by using the new tab text color code mentioned earlier. One obvious idea is to use it to make the tab text red for sessions where I’m root—but how?

One way to do it would be to use PROMPT_COMMAND instead of PS1. Whereas PS1 is just a string, albeit with some fancy escapes, PROMPT_COMMAND allows you to run arbitrary Unix commands and put the results in your prompt.

However, any security conscious Unix admin reading this will recoil in horror at the suggestion of running commands as root every time a prompt appears, so I’m not going to advocate such a thing. If you want to load the shotgun and point it at your foot, you’ll have to work out how for yourself.

Instead, I suggest that you make your .bashrc for root set a different PS1 to your regular .bashrc. Here’s the regular ‘black tab’ prompt:

export PS1='\[\e]30;\h\a\e]0;\u - \w\a\e[28;0t\]\w \!\$ ‘

And here’s the ‘red tab’ danger prompt for root sessions:

export PS1='\[\e]30;\h\a\e]0;\u - \w\a\e[28;16711680t\]\w \!\$ ‘

Finally, let’s add a splash of color to the terminal prompts themselves, and make the regular one green, and the root one bright red. Green is ESC [0;32m, bright red is ESC [0;31;1m and black is ESC [0;37m so:

Regular: export PS1='\[\e[0;32m\e]30;\h\a\e]0;\u - \w\a\e[28;16711680t\]\w \!\$\[\e[0;37m\] ‘

Root: export PS1='\[\e[0;31;1m\e]30;\h\a\e]0;\u - \w\a\e[28;16711680t\]\w \!\$\[\e[0;37m\] ‘

In case you’re wondering why the regular prompt sets the tab text to black given that it’s the default, it’s so that if you su to root and then ^D back out, the tab goes back to black.

If you don’t like those colors there are plenty of guides to how to change your prompt colors, so I’ll leave you to investigate that further.

Tagged: , , , , , , , , , , ,

Leave a Reply

You must be logged in to post a comment.