tmux.conf組み込みのシェルスクリプト?

tmux.conf組み込みのシェルスクリプト?

私はtmux.conf現在のユーザーの大文字の名前でセッションを開始する必要があります(実際には単純化しましたが、例として使用されます)。

違法な次の仮想行を想像してみてください。

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

上記をどのように達成できますか?

ベストアンサー1

私の経験では、.tmux.conf各ステートメントif-shellsource-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'

おすすめ記事