Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wuruxu
V2EX  ›  Linux

分享 tmux 快捷方式

  •  
  •   wuruxu ·
    wuruxu · 14h 9m ago · 975 views

    在 .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
    }
    
    5 replies    2026-05-19 08:11:24 +08:00
    hefish
        1
    hefish  
       13h 52m ago
    我还是每次都输一下吧。反正也是难得。
    tlze
        2
    tlze  
       13h 36m ago via Android
    马上抄一个,谢谢!
    rainsho
        3
    rainsho  
       13h 25m ago
    oh-my-zsh 有 tmux 插件,启用后 `to [session]`
    jimyag
        4
    jimyag  
       13h 24m ago
    https://github.com/jimyag/dotfiles/blob/33b6a38854ec7694eea85b3bd78ebc7bf3cad7ee/home/dot_profile.d/function#L28-L35

    ```bash
    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 就好了。
    3dward
        5
    3dward  
       4h 42m ago via Android
    tmux new-session -A -s main
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3461 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 04:54 · PVG 12:54 · LAX 21:54 · JFK 00:54
    ♥ Do have faith in what you're doing.