Do you often SSH around a bunch of different hosts? I found that with several terminals open, I was easily losing track of which host I was looking at; even with the hostname displayed in the prompt, it takes a second to read it. Some sort of color-coding would be nice.

TCSH/CSH

This monstrosity of tcsh/csh code does the job. It takes the md5sum of your hostname, grabs the first byte, and uses that to construct the ANSI color code. It will also print out your current directory. I put it at the end of my .cshrc, and it works nicely!

# prompt logic
set hostnamebyte = `hostname | md5sum | head -c 1 | tr '[a-f]' '[A-F]'`
set hostnamenumber = `echo "ibase=16; var=$hostnamebyte; var+=2; var%=6; var+=1; var" | bc | head -c 1`
set p1 = '%{\033[1;37m%}[%{\033[1;3'
set p2 = 'm%}%n@%m %{\033[0;32m%}%c6%{\033[1;37m%}]$%{\033[0m%} '
set prompt = "$p1$hostnamenumber$p2"

Screenshot

I’m not exactly… proud of it, but it does work.

Bash

Updated Sept. 26, 2021

I did end up switching my shell to Bash, and therefore ported over the atrocity, now with additional color resolution.

colorcount=62
colorsarray=(46 47 48 49 50 51 82 83 84 85 86 87 118 119 120 121 122 123 154 155 156 157 158 159 190 191 192 193 194 195 226 227 228 229 230 231 208 209 210 211 212 213 166 167 168 169 170 171 202 203 204 205 206 207
136 137 138 139 140 141 172 173 174 175 176 177)
hostnamebyte=$(bash --noprofile --norc -c "hostname | md5sum | head -c 2 | tr '[a-f]' '[A-F]'")
hnum=$(printf "%d" $((16#$hostnamebyte)))
hnum=$(($hnum % $colorcount))
hcolor=${colorsarray[$hnum]}
usernamecolor=11

p=""
p="${p}\[\033[38;5;${usernamecolor}m\]"
p="${p}\u\[$(tput sgr0)\]"
p="${p}@\[\033[38;5;${hcolor}m\]\h\[$(tput sgr0)\]"
p="${p}:\[$(tput sgr0)\]\[\033[38;5;6m\]"
p="${p}[\w]\[$(tput sgr0)\]\\$ "
p="${p}\[$(tput sgr0)\]"
export PS1=$p

image.png