Search code examples
bashshellterminaltmux

Send prefix key to tmux session


So I am using this tmux plugin for logging the entire tmux history. https://github.com/tmux-plugins/tmux-logging

The plugin uses the key bindings C-b + P to start and stop the logging session. Basically, prefix + P.

I am programatically trying to to stop and start the logging session to create a new output file for each day. For this, I want to setup a crontab to run a simple script. The content of the script is as follows.

tmux send-keys -t 0:0 C-b P # stops the logging session
tmux send-keys -t 0:0 C-b P # restarts the logging session

But when I attach to the tmux session, what I see is this printed on the screen.

^BP^BP

So C-b instead of being treated as a prefix, is being treated as a normal key combination. I have also tried using the following, but still the same result.

tmux send-prefix -t 0:0 # prints ^B on the session window
tmux send-keys -t 0:0 P

What is the workaround for this?


Solution

  • According to the maintainer of tmux, "You can't trigger tmux key bindings with send-keys. You could just run the commands the key is bound to instead.".


    For your specific case, I see in logging.tmux:

    tmux bind-key "$logging_key" run-shell "$CURRENT_DIR/scripts/toggle_logging.sh"
    tmux bind-key "$pane_screen_capture_key" run-shell "$CURRENT_DIR/scripts/screen_capture.sh"
    tmux bind-key "$save_complete_history_key" run-shell "$CURRENT_DIR/scripts/save_complete_history.sh"
    tmux bind-key "$clear_history_key" run-shell "$CURRENT_DIR/scripts/clear_history.sh"
    

    where $CURRENT_DIR is the directory that this script lives in.

    So, find that script on your machine and then change your send-keys to

    tmux send-keys -t 0:0 '$SCRIPT_DIR/scripts/toggle_logging.sh' Enter