1

I would like to generate a bashrc configuration file each time I start a particular tmux session, and I want to use my user's default tmux configuration file ${XDG_CONFIG_HOME}/tmux/tmux.conf as well. This same configuration should be reused with each window/pane created in any given session.

To prevent race conditions when starting multiple sessions simultaneously, I would like to avoid moving/relinking/etc. the default bashrc/tmux.conf configuration file each time, as that seems really hairy.

Instead, I am trying to invoke tmux with a dynamic configuration file that performs the following:

  1. Create a temporary file containing my generated bashrc configuration (say, /tmp/bashrc.12345)
  2. Set the default-command option in tmux similar to bash --rcfile /tmp/bashrc.12345 [-i|-l|...] [ARGV...] (depending on how tmux was invoked)
  3. source-file my user's default tmux.conf

This looks similar to the following shell script:

#!/bin/bash

tmuxrc="${XDG_CONFIG_HOME}/tmux/tmux.conf"

bashrc=$( mktemp )
generate-bashrc > "${bashrc}"

# This is the tmux configuration file's final content
config="set-option -g default-command 'bash --rcfile \"${bashrc}\"' ; source-file '${tmuxrc}' ;"
# Reordering the statements makes no apparent difference
#config="source-file '${tmuxrc}' ; set-option -g default-command 'bash --rcfile \"${bashrc}\"' ;"

exec tmux -f <( echo "${config}" ) "${@}"

When running this script, I can see my default tmux configuration file is being loaded, applying my layout/theme and so on, but the default-command option is definitely not working like I would expect.

Instead, my default shell is started, and when I print the current options in tmux with tmux show -sgwp, it doesn't list default-command as being defined at all!

EDIT ≈ Even if I run tmux set-option -g default-command FOO (with FOO being anything at all) from inside the running tmux session, it still remains undefined according to tmux show -sgwp

Why is this option not persisting?

0

1 Answer 1

2

Why is this option not persisting?

Many issues with tmux become more understandable when you realize and remember the tmux server and each tmux client are separate processes. In some circumstances a client starts a server. .tmux.conf is for the server, not for clients. Similarly -f (that specifies an alternative configuration file) is for the server.

Your exec tmux -f <( echo "${config}" ) "${@}" execs to a tmux client. The client may or may not start a tmux server. It will start a server if there is no server yet. If it starts a server then -f will work.

In your case -f will matter for the first tmux command, the one that doesn't find a server and has to start one. You are "starting multiple sessions simultaneously", so your subsequent tmux commands will find the server and their -f will be irrelevant. The server may exit later and if you run your script after this then it's all over again: one tmux command will start a new server, so its -f will matter; but as long as the server remains, -f in subsequent invocations of your tmux -f … won't matter.

See the manual [emphasis mine]:

-f file
Specify an alternative configuration file. […] tmux loads configuration files once when the server process has started. The source-file command may be used to load a file later.


The command tmux set-option -g default-command FOO defines a global session option. To see global session options you need tmux show -g (i.e. without -s, -w, -p). The whole mechanics of options in tmux is kinda complicated.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .