How do I run a shell command from .tmux.conf
, or otherwise automatically when tmux launches, without having to hit any other command?
How can I create an alias, a function, or a script which boils down to, for example
tmuxirc -> tmux + irssi
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHow do I run a shell command from .tmux.conf
, or otherwise automatically when tmux launches, without having to hit any other command?
How can I create an alias, a function, or a script which boils down to, for example
tmuxirc -> tmux + irssi
It sounds like you want to externally invoke tmux from your shell rather than doing this from within tmux, so .tmux.conf
is the wrong place. You can use a shell alias (place this in your .bashrc
for reuse):
alias tmuxirc='tmux new-session -s irc irssi'
While it doesn't seem to be what you were looking for, the solution to:
How do I run a shell command from .tmux.conf
is run-shell
, or, in its abbreviated form, run
. From the tmux
man page:
run-shell shell-command (alias: run) Execute shell-command in the background without creating a win- dow. After it finishes, any output to stdout is displayed in copy mode. If the command doesn't return success, the exit sta- tus is also displayed.
If you need to silently kick off a script in the background whenever you launch tmux
, you could use run "command > /dev/null"
.
I do something similar with a script. When I want to fire up tmux with my development configuration I call it. The script itself looks like the following:
#!/bin/sh
tmux has-session -t development
if [ $? != 0 ]; then
tmux new-session -s development -n editor -d
tmux send-keys -t development 'cd /var/www/htdocs/' C-m
tmux send-keys -t development 'vim' C-m
tmux split-window -v -t development
tmux split-window -v -t development
tmux select-layout -t development main-horizontal
tmux send-keys -t development:0.1 'cd /var/www/htdocs/' C-m
tmux new-window -n console -t development
tmux send-keys -t development:1 'cd /var/www/htdocs/' C-m
tmux select-window -t development:0
fi
tmux attach -t development
What this gives me is a tmux session with 2 windows, window 1 has a Vim session in the top of the screen, with two terminals in the bottom 3rd or so of the screen, all pointed at my /var/www/htdocs/ directory. Window 2 is just a full screen console. Good thing about this is that it won't recreate the session if it's already there, it will just attach to it.
There's a display-message
command in tmux
, from its man page:
display-message [-p] [-c target-client] [-t target-pane] [message]
(alias: display)
Display a message. If -p is given, the output is printed to stdout, otherwise it is displayed in the target-client status line. The format of message is described in the FORMATS section; information is taken from target-pane if -t is given, otherwise the active pane for the session attached to target-client.
And in FORMATS section, there is a variable named session_name
, it will be replaced with the name of the session if you use it in format #{session_name}
.
Try to run tmux display-message -p '#{session_name}'
when you are in a tmux session. Maybe you will see a number, it is the id of the session you attached. Run tmux ls
to check the session list.
However, when you start up a tmux by tmux new -s myproject
, you will see myproject
as the output, but not a number. Because you specified the name of the session when you start tmux.
So, my solution is to put a piece of code in ~/.bashrc
file:
# [tmux] load scripts in ~/.tmux on creating a new pane
# load order: __before__.sh, ${session_name}.sh, __after__.sh
function tmux_load_startup_scripts_by_session_name() {
if [[ -n ${TMUX} ]]; then
local env_before_script="${HOME}/.tmux/__before__.sh"
[[ -f "${env_before_script}" ]] && { . "${env_before_script}"; }
local env_main_script="${HOME}/.tmux/$(tmux display-message -p '#{session_name}').sh"
[[ -f "${env_main_script}" ]] && { . "${env_main_script}"; }
local env_after_script="${HOME}/.tmux/__after__.sh"
[[ -f "${env_after_script}" ]] && { . "${env_after_script}"; }
fi
}
tmux_load_startup_scripts_by_session_name
mkdir ~/.tmux
cat "__before__.sh" > ~/.tmux/__before__.sh
cat "__after__.sh" > ~/.tmux/__after__.sh
cat "sample.sh" > ~/.tmux/sample.sh
tmux new -s sample
You will see output on each panel you create:
__before__.sh
sample.sh
__after__.sh
The advantage of this solution is you can specify different scripts for different projects or environment.