Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

In my tmux.conf, I need to start a session with the name of the current user in capitals (this is actually a simplification but it will do as an example).

Imagine a fictitious line like this one, which is illegal:

new-session -n BananaWindow -s "$(echo "${USER}" | tr '[:lower:]' '[:upper:]')" 'bash -l'

How can I achieve what is intended above?

share|improve this question
I keep my config files in a common git repo and have been looking for a way to change tmux's behaviour based on an if $HOSTNAME… statement of some kind, so I'd be interested in an answer to this, too. – colons Oct 5 '12 at 11:34

1 Answer 1

In my experience, any effective recipe to get a .tmux.conf to execute multiple commands conditionally that doesn't rely on every statement being wrapped in an if-shell makes use of multiple files and the source-file command.

Here is how I do host-based profiles:

# Use `run-shell` to resolve arbitrary shell commands, and set-environment to
# make them available to tmux.
# Note that any variables set with `set-environment` are not available later in
# the same conf file, but will be available interactively once the conf file
# has loaded--and also in any sourced conf files.
run-shell "tmux set-environment -g TMUX_PROFILE $(hostname)"

# Here we're just defining a variable to reduce duplication later...
set-environment -g TMUX_PROFILE_PATH "${HOME}/.tmux/profiles/${TMUX_PROFILE}.tmux"

# The file that contains the instruction to source the proper conf file.  We
# have to put the command contained in this file in a separate file because of
# that `set-environment` issue.
source-file "${HOME}/.tmux/profiles/select-profile.tmux"

Contents of ${HOME}/.tmux/profiles/select-profile.tmux:

if-shell "test -f ${TMUX_PROFILE_PATH}" 'source-file ${TMUX_PROFILE_PATH}' 'display-message "profile not found: ${TMUX_PROFILE_PATH}"'

Now you just need to put host-specific configurations into ${HOME}/.tmux/profiles/, and they'll get loaded automatically.

You could use a similar technique for a named session.

run-shell "tmux set-environment -g SESSION_NAME $(echo "${USER}" | tr '[:lower:]' '[:upper:]')"
source-file "${HOME}/.tmux/functions/named-session.tmux"

Contents of ${HOME}/.tmux/functions/named-session.tmux:

new-session -n BananaWindow -s "${SESSION_NAME}" 'bash -l'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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