我有一个特定于项目的 shell 脚本,它创建环境变量、别名并运行一些命令。
我想为此项目创建一个 tmux 会话,该会话将在 tmux 会话中创建新窗格或窗口时获取我的 shell 脚本。
如何指定 tmux 在特定会话的每个新窗格或窗口中获取文件或运行命令?
设置default-command会话选项来运行 shell(我假设是bash),并--rcfile选择使用 shell 脚本作为初始化文件
set-option -g default-command "bash --rcfile yourscript.sh"
Run Code Online (Sandbox Code Playgroud)
由于--rcfile替换.bashrc,您添加source .bashrc到 的开头yourscript.sh。如果您通常在tmux窗口中启动登录 shell,请添加source .bash_profile。
要为不同的会话设置单独的默认命令,您需要先创建会话,然后设置其默认命令。
tmux new-session -s projectA
tmux set-option -s -t projectA default-command "bash --rcfile projectA.sh"
Run Code Online (Sandbox Code Playgroud)
您可能想要定义一个 shell 函数来简化新会话的设置,例如
new_session () {
tmux new-session -s "$1"
tmux set-option -s -t "$1" default-command "bash --rcfile $1.sh"
}
Run Code Online (Sandbox Code Playgroud)