在 my 中tmux.conf
,我需要使用当前用户的大写名称来启动会话(这实际上是一种简化,但它可以作为示例)。
想象一下这样的虚构行,这是非法的:
new-session -n BananaWindow -s "$(echo "${USER}" | tr '[:lower:]' '[:upper:]')" 'bash -l'
我怎样才能达到上面的目的?
答案1
根据我的经验,任何有效的方法都可以让 a.tmux.conf
有条件地执行多个命令,而不依赖于将每个语句包装在一个中,if-shell
从而利用多个文件和source-file
命令。
以下是我如何进行基于主机的配置文件:
# 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"
内容${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}"'
现在您只需将特定于主机的配置放入 中${HOME}/.tmux/profiles/
,它们就会自动加载。
您可以对命名会话使用类似的技术。
run-shell "tmux set-environment -g SESSION_NAME $(echo "${USER}" | tr '[:lower:]' '[:upper:]')"
source-file "${HOME}/.tmux/functions/named-session.tmux"
内容${HOME}/.tmux/functions/named-session.tmux
:
new-session -n BananaWindow -s "${SESSION_NAME}" 'bash -l'