A Weird Imagination

256 color hostnames

The problem

When using multiple terminals on different hosts, it can sometimes be confusing to remember which host you are on. The hostname appears in the command prompt, but it's easy to skim past that if you are not paying attention.

One solution that works pretty well for me is recoloring the prompt based on what host I am on. This is in fact why I researched how to get 256 colors terminals working in the first place: in order to have enough colors to be able to make a good choice for each host I use frequently.

Customizing the prompt

In bash, the prompt can be customized by changing the $PS1 variable, which is usually set in ~/.bashrc so it will apply to all sessions. The Arch wiki has many examples of interesting prompts. The prompt I use is almost the same as the default one on Debian/Ubuntu which looks like

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

which makes a prompt like

alice@example:~$ 

This can be difficult to read because it's so dense. Ignoring the first part about debian_chroot, the next part has the familiar \033[ start of an ANSI escape code. Specifically, 01 means bold/bright and 32 means green. It's surrounded by \[ and \] to let bash know it's non-printing characters because bash needs to know where to place the cursor. \u inserts the current user's username and \h inserts the computer's hostname, making the prompt start like alice@example. Next is another escape code, 00, which resets the color for a :. Then the code 34 sets the color to blue for \w, which is the path. Finally, the color is reset again for \$ which shows $ for normal users and # for root.

Hostname-dependent color

To recolor the prompt, we will replace the 01;32 with a different color selection (other than the default of green) depending on what the current host is:

case $(hostname) in
    foo.example.com ) ps1_color='37;38;5;101' ;;
    bar ) ps1_color='33;38;5;136' ;;
    baz ) ps1_color='33;38;5;228' ;;
    * ) ps1_color='32;48;5;52' ;;
esac
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;'$ps1_color'm\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

On a 256 color terminal, here's what this will look like on four different hosts:

alice@foo:~$ 
alice@bar:~$ 
alice@baz:~$ 
alice@unknown:~$ 

And on the same four hosts with a 8 color terminal, it will look like

alice@foo:~$ 
alice@bar:~$ 
alice@baz:~$ 
alice@unknown:~$ 

How it works

The hostname command gives the computer's hostname and then the case selects a color based on whether the hostname is foo.example.com, bar, or baz. Be aware that depending on your configuration, hostname may return a fully qualified name like foo.example.com while \h displays only foo.

All of the color selections have colors for both 8 color terminals first (37 for gray or 33 for yellow) and then 38;5; followed by the color number for 256 color terminals. The test scripts in my post on setting up 256 color terminals may be useful for selecting colors.

Terminals without 256 color support will just ignore the sequences for 256 color terminals, so that gives a graceful fallback for 8 color terminals. The default case (*) sets the color to the default green (32) for 8 color terminals but on 256 color terminals also sets the background color to a dark red to indicate an error to encourage adding a color for that host to the case.

Comments

Have something to add? Post a comment by sending an email to comments@aweirdimagination.net. You may use Markdown for formatting.

There are no comments yet.