2 tmux panes stacked vertically are used in my development. I'm coding in the upper one and build in the bottom one.
My way of doing it is sending the following keys from the upper coding pane via adding the following lines into the .tmux.conf file
bind b send-keys -t.- 'clear' Enter 'cmake ~Project_a/Build/' Enter
However, I've got several projects to work on currently, say project_b and project_c apart from the project_a. Each time when I switch my work on different project I need to change the bind d definition in the config file.
Is there a way to create a function SetProject()
which can be invoked by Tmux and when I input project_c then the b key will change to the build process for project_c so I don't have to change this config file each time anymore
TL;DR: use #{pane_current_path}
with an if statement in your bind.
AFAICT, there is no way to get a tmux function to take input. While it is possible to bind tmux commands to use variables, e.g.
bind-key C-t display-message "words and $bash_environment_variable"
the problem is that tmux only checks the environment from which you started tmux for this variable, not the current pane's environment, so you'd have to detach from tmux, set the variable, reattach to tmux and re-source your .tmux.conf
for that to work.
The only thing I can think of is, if you are in that directory when you need to build, having tmux check the current pane's path. You could do something like
bind-key b if-shell '[[ #{pane_current_path} =~ /path/to/projA' '<build projA>' 'if shell projB' ...
see here for an example of how to correctly do multiple if statements (poor man's if-elif statements) in tmux.
If you set it up right, it might be possible to avoid the if-shell part and just to #{pane_current_path} as part of the path for the build command.
Furthermore, you can use your bottom pane's current pane and purposefully select the top pane in your tmux command. Here's an example:
tmux send-keys -t sess:0.2 'sudo htop' Enter
where is the session name, <0> is the window number (0-indexed unless you've changed it) and <2> is the pane number (also zero indexed and goes left->right top->bottom like you'd expect).