Gui Ambros
Gui Ambros

Reputation: 1065

Tmux - how to execute a command automatically when changing window panes?

Is there a way to execute a command automatically every time I change panes in tmux?

Of course I could execute it manually, but I'm trying to fix a glitch in colors automatically [1]. I'm using tmux 2.6, on Ubuntu 18.04.

thanks!

--

[1] for context: I'm changing pane colors automatically based on the current user (so root would have a different pane color), and change the fg/bg based on active/inactive window. Everything works fine, but there's some edge cases where I need to issue a tmux refresh-client. So I'm trying to have the refresh-client running automatically every time I change panes.

Upvotes: 1

Views: 2070

Answers (2)

guest
guest

Reputation: 11

I created a script to notify me a windows is zoomed:

~/.byobu/hook.conf

set-hook session-window-changed {
    run-shell "bash ~/.byobu/bin/set_status_bg_color.sh"
}

set-hook window-pane-changed {
    run-shell "bash ~/.byobu/bin/set_status_bg_color.sh"
}

~/.byobu/keybindings.tmux

bind z {
   resize-pane -Z

   source ~/.byobu/hook.conf                                # if you just put the content of hook.conf in .tmux.conf, it won't work properly
   run-shell "bash ~/.byobu/bin/set_status_bg_color.sh"     # the order of the two statements is important
}

~/.byobu/bin/set_status_bg_color.sh

#!/usr/bin/bash

tmux list-panes -F "#F" | grep "Z" > /dev/null 2>&1

if [ $? -eq 0 ]; then
  tmux set -g status-bg magenta #blue
  tmux set -g status-left " #(echo #{pane_current_path} | sed 's#$HOME#~#g') | ZOOMED-IN "
else
  tmux set -g status-bg grey19 #default #green # Try default first. If the color of the status after unzooming doesn't look right, change the color
  tmux set -g status-left " #(echo #{pane_current_path} | sed 's#$HOME#~#g') "
fi

PS: The script finally works.

Upvotes: 1

Lucas Werkmeister
Lucas Werkmeister

Reputation: 2742

I think these should do the trick:

tmux set-hook window-pane-changed refresh-client
tmux set-hook session-window-changed refresh-client
tmux set-hook session-changed refresh-client

But I only tested it with 'display-message hi' as the command, so it’s possible that the refresh-client won’t work as expected.

Upvotes: 7

Related Questions