4

I have a shell script specific for a project which create environment variables, aliases and run some commands.

I want to create a tmux session for this project which will source my shell script when creating a new pane or window in the tmux session.

How can I specify tmux to source a file or run command in every new panes or windows of a specific session?

2

Set the default-command option for the session to run your shell (I'll assume it is bash) with the --rcfile option to use your shell script as the initialization file

set-option -g default-command "bash --rcfile yourscript.sh"

Since --rcfile replaces .bashrc, you add source .bashrc to the beginning of yourscript.sh. If you would ordinarily start a login shell in a tmux window, add source .bash_profile instead.


To have separate default commands for different sessions, you need to create the session first, then set its default command.

tmux new-session -s projectA
tmux set-option -s -t projectA default-command "bash --rcfile projectA.sh"

You might want to define a shell function to simplify setting up a new session, something like

new_session () {
    tmux new-session -s "$1"
    tmux set-option -s -t "$1" default-command "bash --rcfile $1.sh"
}
4
  • Thank you for the answer but how can I have different default-command according on the name of the session created with tmux new-session -s name? I'd like to have one default-command using projectA.sh if I run tmux new-session -s projectA and another default-commandusing projectB.sh if I run tmux new-session -s projectB – jmlrt Aug 29 '15 at 21:55
  • See the documentation for set-option; you should be able to specify the session you want change the default command for. – chepner Aug 30 '15 at 15:55
  • The man page of tmux doesn't help me. I can't configure session option to source a script. Here are my config files: - .tmux.conf: set-option -s -t "projectA" default-command "bash --rcfile projectA.sh" – jmlrt Aug 31 '15 at 20:38
  • Here are my config files: - .tmux.conf: set-option -s -t "projectA" default-command "bash --rcfile projectA.sh" - projectA.sh: export ENV="A" export PROJECT="~/projectA" alias cdproject="cd $PROJECT" If I run tmuxwithout parameter option: /root/.tmux.conf:1: session not found: projectA /root/.tmux.conf:1: couldn't set 'default-command' – jmlrt Aug 31 '15 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.