安装 tmuxp 替代 tmuxinator

文章目录

    为何要抛弃 tmuxinator

    • tmuxinator 升级需要升级 ruby
    • 升级 ruby 需要安装 rvm
    • 安装 rvm 又需要更新 gpg
    • 然而 gpg 更新永远连不上服务器

    我对 ruby 升级的麻烦程度感到了无比的厌恶,而我电脑上唯一需要用到 ruby 的地方就是这个 tmuxinator。

    $ sudo gem install tmuxinator
    Fetching: thor-1.2.1.gem (100%)
    Successfully installed thor-1.2.1
    Fetching: xdg-2.2.5.gem (100%)
    Development of this project has been transfered here: https://github.com/bkuhlmann/xdg. This will be the last version supported within the 2.2.x series. Be aware major breaking changes will be introduced in the upcoming 3.0.0 release (2019-06-01).
    Successfully installed xdg-2.2.5
    Fetching: tmuxinator-3.0.1.gem (100%)
    ERROR:  Error installing tmuxinator:
            The last version of tmuxinator (>= 0) to support your Ruby & RubyGems was 1.1.5. Try installing it with `gem install tmuxinator -v 1.1.5`
            tmuxinator requires Ruby version >= 2.6.7. The current ruby version is 2.5.0.
    

    tmuxinator 的报错,总是非常无厘头,无法定位问题根源。

    所以,我决定替换掉 tmuxinator。

    tmuxp

    tmuxp 是用 python 实现的。tmuxp 中 p 大概代表 python 吧。

    https://github.com/tmux-python/tmuxp

    从 tox.ini 看,支持 python 3.7 及以上。而我一直在 Ubuntu 上开发,都预装了 python 3.8,所以不会存在 ruby 这样的依赖问题。即便没有 python 3.8,也可以方便的升级 python 到 3.8

    安装 tmuxp

    sudo apt install tmuxp
    

    不要使用官方推荐的 pip install tmuxp, 安装完了也找不到命令。

    确认安装成功:

    > tmuxp -V
    tmuxp 1.3.5
    

    原则

    • 新项目使用 tmuxp
    • 旧有项目继续使用 tmuxinator
    • daily config 用 tmuxp 改写
    • 整理 yaml snippet

    写个 daily 配置练练手

    原有的 tmuxinator 配置,YAML 格式:

    $ cat ~/.tmuxinator/daily.yml
    # ~/.tmuxinator/daily.yml
    # you can make as many tabs as you wish...
    
    name: daily
    root: ~/
    
    windows:
      - editor: cd /mnt/d/notes; ls
      - shell: cd /mnt/d/notes; ls
      - snippet: cd ~/.vim/bundle/vim-zhongwei-snippets/UltiSnips/
    

    参考官方的示例,写了一份 daily 的 tmuxp 版本:

    session_name: daily 
    start_directory: /mnt/d/notes/ 
    windows:
      - window_name: editor 
        panes:
        - shell_command: ls 
      - window_name: shell 
        panes:
        - shell_command: ls 
      - window_name: snippet 
        start_directory: ~/.vim/bundle/vim-zhongwei-snippets/UltiSnips/
        panes:
        - shell_command: ls 
    

    看起来是啰嗦了一点,倒是可以接受。

    加载配置文件:

    $ tmuxp load daily.yml
    [Loading] /home/zhongwei/work/config/tmuxp/daily.yml
    

    效果如下:

    tmuxp

    tmuxp 支持哪些命令

    $ tmuxp
    Usage: tmuxp [OPTIONS] COMMAND [ARGS]...
    
      Manage tmux sessions.
    
      Pass the "--help" argument to any command to see detailed help. See
      detailed documentation and examples at:
      http://tmuxp.readthedocs.io/en/latest/
    
    Options:
      -V, --version     Show the version and exit.
      --log_level TEXT  Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
      --help            Show this message and exit.
    
    Commands:
      convert  Convert a tmuxp config between JSON and YAML.
      freeze   Snapshot a session into a config.
      import   Import a teamocil/tmuxinator config.
      load     Load tmuxp workspaces.
    

    看起来主要是 freeze, import, load 这三个命令。

    tmuxp import tmuxinator 配置文件

    $ tmuxp import tmuxinator ~/.tmuxinator/daily.yml
    Convert to [yaml]:
    session_name: daily
    start_directory: ~/
    windows:
    - panes:
      - cd /mnt/d/notes; ls
      window_name: editor
    - panes:
      - cd /mnt/d/notes; ls
      window_name: shell
    - panes:
      - cd ~/.vim/bundle/vim-zhongwei-snippets/UltiSnips/
      window_name: snippet
    ---------------------------------------------------------------
    Configuration import does its best to convert files.
    
    The new config *WILL* require adjusting afterwards. Save config? [y/N]: y
    Save to [/home/zhongwei/tmp]:
    Save to [/home/zhongwei/tmp]:
    Save to [/home/zhongwei/tmp]: ./
    Error: /home/zhongwei/tmp exists. Pick a new filename.
    Save to [/home/zhongwei/tmp]: daily.yml
    Save to /home/zhongwei/tmp/daily.yml? [y/N]: y
    Saved to /home/zhongwei/tmp/daily.yml.
    

    真是太方便了,无痛切换。

    tmuxp 不好的地方

    用了几天 tmuxp,发现 tmuxp 唯一让我不爽的地方是,第一次启动有点慢。
    是肉眼可见的慢,相对 tmuxinator 而言。

    有时间我要试试 golang 的一个实现:

    https://github.com/ivaaaan/smug

    最终我还是放弃了 tmuxp,换到了 go 实现的 smug

    进阶配置

    TypeError: Arguments take exactly one parameter declaration, got 2.

    新机器上,执行 tmuxp 报错:

    File "/home/zhongwei/.local/lib/python3.8/site-packages/click/core.py", line 2982, in _parse_decls
        raise TypeError(
    TypeError: Arguments take exactly one parameter declaration, got 2.
    

    这个报错的原因是 click 这个库的版本不兼容问题,需要安装低版本的 click。解决方法:

    pip3 install 'click<7'
    

    参考

    • tmuxp 配置示例 https://tmuxp.git-pull.com/examples.html

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式