对bash用户, 只须要将下面命令添加到本身家目录下的.bashrc, 要注意这句命令须要在alias配置以前.对其它shell的配置也是相似的shell
~/.bashrc
# If not running interactively, do not do anything [[ $- != *i* ]] && return [[ -z "$TMUX" ]] && exec tmux
下面的配置会尝试只启动一个会话, 当你登陆时, 若是以前启动过会话, 那么它会直接attach, 而不是新开一个. 想要新开一个session要么是由于以前没有会话, 要么是你手动启动一个新的会话.bash
# TMUX if which tmux >/dev/null 2>&1; then #if not inside a tmux session, and if no session is started, start a new session test -z "$TMUX" && (tmux attach || tmux new-session) fi
下面的配置实现的功能类似, 可是他会在启动tmux以前先检查一下tmux是否已经安装. 它也会在你登出以前帮你从新链接上未关闭的session, 这样可让你手动关闭会话并保存相应的工做,避免数据丢失,进程异常中断.session
# TMUX if which tmux >/dev/null 2>&1; then # if no session is started, start a new session test -z ${TMUX} && tmux # when quitting tmux, try to attach while test -z ${TMUX}; do tmux attach || break done fi
另一种配置, 同样能够实现自动链接已存在的会话,不然会新开一个:socket
if [[ -z "$TMUX" ]] ;then ID="`tmux ls | grep -vm1 attached | cut -d: -f1`" # get the id of a deattached session if [[ -z "$ID" ]] ;then # if not available create a new one tmux new-session else tmux attach-session -t "$ID" # if available attach to it fi fi