V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
haoliang
V2EX  ›  Linux

如果 tmux 也有类似 vim tabpage-variable 的机制就好了

  •  
  •   haoliang · 2022-09-02 00:32:19 +08:00 · 1695 次点击
    这是一个创建于 573 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我一直这样的需求:在 tmux 中新开个 window ,然后 cd 到某个目录下搞东搞西,不出意外地想再开个同目录下的 split pane 。 但是 tmux 默认是使用 session 的 working dir 来开 split pane 的,这就有些不方便了。 如果它有 vim :tcd 这样的机制多好啊。

    或许有人会建议那你以想要的目录为 working dir 新开个 session 不截了。可以也还行,我现在就一直这么用,不过有时候 session 会特别多,而且每个 session 往往就一个 window + 两三个 pane ;切换起来也有点烦。所以我还是想要个类似 vim tabpage-variable 的机制。

    然后我就爬文档( man tmux ),嘿,找到个章节 GLOBAL AND SESSION ENVIRONMENT,借助 set-env -t session name valueshow-env -t session name 问题得以解决。 (读写某个 session env ; split pane 时读下这个值看本 window 有没有设置 working dir ,有的话加个 -c $workdir 的参数;...)

    不知道有没有其他人有类似痛点,有没有更好的解决方式?


    贴一下帮助文档,省得大家再查一遍

    :h tabpage-variable

    						*tabpage-variable* *t:var* *t:*
    A variable name that is preceded with "t:" is local to the current tab page,
    It is deleted when the tab page is closed.
    

    :h tcd

    							*:tc* *:tcd*
    :tc[d][!] {path}	Like |:cd|, but only set the directory for the current
    			tab.  The current window will also use this directory.
    			The current directory is not changed for windows in
    			other tabs and for windows in the current tab that
    			have their own window-local directory.
    
    9 条回复    2022-09-05 19:56:38 +08:00
    Les1ie
        1
    Les1ie  
       2022-09-02 01:14:26 +08:00
    我也一直都是 split 之后重新 cd 一遍 :(

    看了楼主的思路,结合 stackoverflow 的解决方案,或许可以直接改下快捷键的绑定?

    $ cat ~/.tmux.conf

    # split window
    unbind '"'
    bind - splitw -v -c "#{pane_current_path}"

    unbind %
    bind | splitw -h -c "#{pane_current_path}"


    我试了下是 work 的, 自动在当前目录 split :)


    refer:
    - https://unix.stackexchange.com/questions/12032/how-to-create-a-new-window-on-the-current-directory-in-tmux
    Les1ie
        2
    Les1ie  
       2022-09-02 01:16:04 +08:00
    个人比较习惯 - | 表示横向和纵向的分割,依旧使用原来的 引号百分号肯定也可以的 :P
    haoliang
        3
    haoliang  
    OP
       2022-09-02 01:21:03 +08:00
    @Les1ie 这个方法我想写到正文中来着,它主要不适合这个场景:

    `/playground` 下有好多兴趣项目,然后 `tmux new -t playground -c /playground`, 然后 window index 1 中是专为 xx 开的,a) pane 开个 vim 、b) pane 被 cd 到 tests 下。如果你此时在 b) 下,这个 pane_current_path 很可能就不是你想要的路径,大多数情况下应该都是项目根目录
    haoliang
        4
    haoliang  
    OP
       2022-09-02 01:25:40 +08:00
    不过知道它的局限性,使用时注意一下就好了(虽然有点例外有点费精力)
    * 切换到 a),再 split
    * 就在 b) split ,在 cd 下也没啥嘛
    haoliang
        5
    haoliang  
    OP
       2022-09-02 01:42:39 +08:00
    哈哈,谢谢回复,倒是让我想到个这样的解决方法:
    `tmux display-popup -E 'exec tmux split-window -c $(find "$(git rev-parse --show-toplevel)" -type d | fzf)'`

    (为啥不用 fd ?)
    kaiger
        6
    kaiger  
       2022-09-02 09:27:30 +08:00
    wd 命令,z 命令了解一下?

    切换目录很方便的,假如你以前切换过某个目录:
    ~/Download ,直接 `z dow`
    ~/.config/nvim 直接 `z co nv`

    wd 命令的话,就是添加目录书签,
    在 Download 目录下运行 `wd add d`
    然后键入命令 `wd d` 就可。
    azurefire
        7
    azurefire  
       2022-09-03 13:30:24 +08:00 via iPhone
    想起来我的 tmux 就是这么配的,回去找找
    azurefire
        8
    azurefire  
       2022-09-05 18:01:51 +08:00
    找到了,用的是 `pane_current_path`

    ```
    bind -n M-'\' split-window -h -c "#{pane_current_path}"
    bind -n M-- split-window -v -c "#{pane_current_path}"

    ```
    haoliang
        9
    haoliang  
    OP
       2022-09-05 19:56:38 +08:00
    @azurefire 噗,过时信息了,看下一楼的回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5521 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:04 · PVG 17:04 · LAX 02:04 · JFK 05:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.