GoForum🌐 V2EX

分享 tmux 快捷方式

wuruxu · 2026-05-18 22:46 · 0 次点赞 · 4 条回复

在 .bashrc 添加了下面的快捷方式,用不着每次都输入 tmux …

aa() {
    if tmux has-session -t aa 2>/dev/null; then
        tmux attach -t aa
    else
        tmux new -s aa
    fi
}
rr() {
    if tmux has-session -t rr 2>/dev/null; then
        tmux attach -t rr
    else
        tmux new -s rr
    fi
}
4 条回复
hefish · 2026-05-18 23:06
#1

我还是每次都输一下吧。反正也是难得。

tlze · 2026-05-18 23:21
#2

马上抄一个,谢谢!

rainsho · 2026-05-18 23:31
#3

oh-my-zsh 有 tmux 插件,启用后 to [session]

jimyag · 2026-05-18 23:31
#4

https://github.com/jimyag/dotfiles/blob/33b6a38854ec7694eea85b3bd78ebc7bf3cad7ee/home/dot_profile.d/function#L28-L35

function tmux_attach_or_new(){
  # 无参数时用当前目录名作会话名;$PWD 为 / 时 ${PWD##*/} 为空,需兜底
  local name="${1:-${PWD##*/}}"
  if [[ -z $name ]]; then
    name="root"
  fi
  tmux new -A -s "$name"
}

function t(){
  tmux_attach_or_new "$1"
}

可以参考下我这个,在目录中 执行 t 会打开或者创建 目录同名的 session 。

我一般都在 项目目录中 执行 t 或者 t xxx-dev 就好了。

添加回复
你还需要 登录 后发表回复

登录后可发帖和回复

登录 注册
主题信息
作者: wuruxu
发布: 2026-05-18
点赞: 0
回复: 0