9

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?

2

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.

  • Afraid 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!!! – user115903 Sep 22 '17 at 7:04
3

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. – Raphael Ahrens Apr 1 '14 at 11:12
  • 1
    My tmux config includes a way to define and run arbitrary shell functions self contained inside the tmux conf file. – Caleb Jan 25 '16 at 8:05
  • @Caleb interesting. Currently I don't have the time to edit it in to the answer. But I will add it. – Raphael Ahrens Jan 26 '16 at 8:47
  • 1
    You 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. – Caleb Jan 26 '16 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.