Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functions in tmux config

Tags:

bash

tmux

Is it possible to define a function in tmux config? I have a generic workflow that I want to run for a given tmux window. Right now I have defined this in a bash script which gets the window number as a parameter. Example:

bind 1 run-shell "~/.config/tmux/switchWindow.sh 1"
bind 2 run-shell "~/.config/tmux/switchWindow.sh 2"
bind 3 run-shell "~/.config/tmux/switchWindow.sh 3"
bind 4 run-shell "~/.config/tmux/switchWindow.sh 4"
[...]

I have this setup for multiple functions. So, besides my tmux.config, multiple bash scripts are required for my tmux setup to work. I want to flatten this out and ideally have everything in the tmux.conf. Is there a way to define functions in my tmux config and use bash commands inside them?

like image 915
fwind Avatar asked Mar 10 '23 00:03

fwind


1 Answers

As far as I know, there is no official way to declare shell function directly in .tmux.conf. If you use bash usually, it might be helpful to declare functions in default-command and expose them to child processes with export -f, and open interactive screen with bash -i.

set-option -g default-command '   \
function switchWindow () {        \
  echo "Do something for $1";     \
};                                \
function otherFunc () {           \
  echo "Do something for $1";     \
};                                \
export -f switchWindow otherFunc; \
bash -i'

bind 1 send-keys "switchWindow 1" C-m
bind 2 send-keys "switchWindow 2" C-m
like image 72
Gre-san Avatar answered Mar 11 '23 14:03

Gre-san