I'd like tmux
to pick a color dynamically based on the hostname of the machine. Since I share my tmux.conf
across several machines, assigning an unique color for each of those hosts would be handy visually, especially when working on more than one of the simultaneously. Is this doable?
4 Answers
I came up with the following shell function:
hash_string256() {
# Hash $1 into a number
hash_value=$(printf "%s" "$1" | md5sum |tr -d " -"| tr "a-f" "A-F")
# Add the hash with $2 and modulo 256 the result
# if $2 == "" it is 0
printf "ibase=16; (%s + %X) %% 100\n" $hash_value "$2" | bc
}
This function can be used like this (The results are true if $HOST
is LOL
):
$hash_string256 $HOST
113
$hash_string256 $HOST 127
240
To connected it with tmux
you can use a script that starts and configures tmux
.
#!/bin/sh
SESSION=$USER
hash_string256() {
hash_value=$(printf "%s" "$1" | md5sum |tr -d " -"| tr "a-f" "A-F")
printf "ibase=16; (%s + %X) %% 100 \n" $hash_value "$2" | bc
}
tmux -2 new-session -d -s $SESSION
tmux set -g status-fg colour$(hash_string256 $HOST)
tmux set -g status-bg colour$(hash_string256 $HOST 127)
# Attach to session
tmux -2 attach-session -t $SESSION
For the hostname LOL
it would set the status-fg
to colour113
and status-bg
to colour240
. The number 127 in $(hash_string256 $HOST 127)
is there so the background will be not the same as the foreground color and far apart from each other.
For none GNU systems
If your system has md5
instead of md5sum
the line
hash_value=$(printf "%s" "$1" | md5sum |tr -d " -"| tr "a-f" "A-F")
can be replaced with
hash_value=$(printf "%s" "$1" | md5 | tr "a-f" "A-F")
-
If some one knows a way to use the function inside the
tmux.conf
I would appreciate it. Commented Apr 1, 2014 at 11:12 -
1My tmux config includes a way to define and run arbitrary shell functions self contained inside the tmux conf file.– CalebCommented Jan 25, 2016 at 8:05
-
@Caleb interesting. Currently I don't have the time to edit it in to the answer. But I will add it. Commented Jan 26, 2016 at 8:47
-
1You might want to make it an alternate ending for the answer rather than entering it as the main answer because its likely to confuse the heck out of anybody who doesn't have some advanced shell foo under their belt. The solution is a hack involving here-docs and to get both a config file and an executable script out of the same file, so implementing this requires a change to the whole config file, not just adding a couple of lines. Getting it right won't be for everybody, so give them an way to edit their regular config using external scripts before suggesting it can be mashed together.– CalebCommented Jan 26, 2016 at 9:00
-
@Caleb: I believe that you may have posted this comment in the wrong place. I looked at Raphael Ahrens’s answer (and even the revision history) and couldn’t understand how your comment applied. And then I read DrMilk’s answer and was totally baffled. And then I re-read your comment here, and a light began to glow dimly. Commented May 2, 2022 at 1:15
I wanted this feature as well.
I basically merged everything into this .tmux.conf
# cat <<__DATA__ >/dev/null
# Embed shell scripts
set -g status-utf8 on
set -g utf8 on
set -g default-terminal "screen-256color"
run "cut -c3- ~/.tmux.conf | bash -s apply_configuration"
# __DATA__
#
# apply_configuration() {
# tmux set -g status-bg colour$(hash_string256 $(hostname))
# }
# hash_string256() {
# hash_value=$(printf "%s" "$1" | md5sum | sed -e 's/[^[:alnum:]]\+//g' | tr "a-f" "A-F")
# if [ "x" != "x$2" ]
# then
# v2="+ $2"
# fi
# echo "$(((0x$hash_value $v2) % 255))" | tr -d "-"
# }
#
# $1
I removed using bc
because I didn't have it in my git-bash.
Thus I wanted it to work on both my Linux systems
and Windows with Cygwin without adding extra stuff.
-
1Afraid this might be a silly question, but how do I exactly use it? I copy/pasted it but after reloading my
.tmux.conf
I got"cut -c3- ~/.tmux.conf | bash -s apply_configuration" returned 1
. Nonetheless, my status bar indeed turned red!!!– user115903Commented Sep 22, 2017 at 7:04 -
Not a silly question at all; I second the above. I understand Unix & Linux pretty well, and I can only with great difficulty begin to see how this answer is supposed to work, or how it is supposed to be used. DrMilk hasn’t logged in here for a long time, so I don’t expect them to respond. Will somebody who upvoted this please edit it to make it clearer and more complete? Commented May 2, 2022 at 1:09
.tmux.conf:
run "tmux set -g status-bg $(hostname -s | hexdump -e '\"0x%02x\"' | cut -c-4 | xargs printf 'colour%d')"
-
2Please explain what this does and how it does it. I don’t care if it is 70% the same as another answer; that answer is also unclear. And each answer should stand alone; even if Tmuxer’s answer is deleted, yours should still be understandable. … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. … (Cont’d) Commented May 1, 2022 at 19:25
-
1(Cont’d) … P.S. If you copy somebody else’s answer and modify it, you should give credit to the original answer, both by linking to it and by citing the author’s name. If you were really using the above code before yesterday, you should probably say so, because it sure looks like you copied Tmuxer’s answer. P.P.S. If you are Tmuxer, please don’t post two separate answers that are 70% the same. Merge your accounts and post one answer that contains both solutions (with explanations!). Commented May 1, 2022 at 19:25
-
2Welcome to Unix & Linux Stack Exchange! Brevity is acceptable, but fuller explanations are better.– Kusalananda ♦Commented May 4, 2022 at 16:01
I use this. Maybe the hexdump format string can make the cut part obsolete, but I didn't figure out how.
.tmux.conf:
run "tmux set -g status-bg $(hostname -s | hexdump -e '\"#%06x\"' | cut -c-7)"
-
1Please explain what this does and how it does it. … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. Commented May 1, 2022 at 19:28
-
Welcome to Unix & Linux Stack Exchange! Brevity is acceptable, but fuller explanations are better.– Kusalananda ♦Commented May 4, 2022 at 16:01