在使用了一阵 tmux 之后(看了一下最早建的 tmux 的笔记,是2013年),逐渐习惯了各种 tmux 的操作,但是每次 ssh 连接服务器后都要手动执行 tmux attach
进入 tmux 会话,有点麻烦,于是就想着能不能自动进入 tmux 会话呢?
简单 Google 一下没一下子找到答案,问了一下 chatGPT 直接就得到了最终答案
编辑vim ~/.bashrc
,增加下边代码
1
2
3
|
if [ -z "$TMUX" ]; then
tmux attach-session || tmux new-session -n $HOSTNAME
fi
|
chatGPT 原文
1
2
3
4
5
6
7
8
9
10
11
12
13
|
可以在SSH连接后自动进入tmux的步骤如下:
打开终端并连接到SSH服务器。
在终端中输入vim ~/.bashrc命令来编辑用户的bash配置文件。
将以下代码添加到文件的末尾:
if [ -z "$TMUX" ]; then
tmux attach-session || tmux new-session -n $HOSTNAME
fi
其中,-n $HOSTNAME参数将创建一个新的tmux会话,并将其命名为主机名。
4. 保存并退出配置文件。
5. 断开当前的SSH连接并重新连接到服务器。此时,您应该会自动进入tmux会话。
请注意,如果您已经在另一个会话中运行了tmux,则不会自动创建新的会话。
|
github copilot 原文
1
2
3
4
5
6
|
Here's a possible solution in Bash:
# Add the following line to your .bashrc or .bash_profile file
if [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then tmux attach; fi
This will automatically attach to an existing tmux session when you SSH into a server, but only if you're not already in a tmux session.
|
chatGPT这轮胜出