【问题标题】:tmux: how to open file under cursortmux:如何在光标下打开文件
【发布时间】:2016-05-26 01:29:30
【问题描述】:

我是 vim 用户,习惯了gf 命令,它会打开光标下的文件。

现在我想问一下,tmux 是否有类似的东西。

我可以在 tmux-pane 中导航,并且经常会出现光标下有一个文件路径的情况。现在我喜欢用 vim 打开光标下的那个文件。

  • 答:在当前窗口中
  • B: 在另一个包含并打开 vim 的窗口中

在调用特殊组合键时,是否有可能在导航模式下运行 sh 脚本?这样就可以编写自己的脚本,就像我习惯在 vim 中使用 vimscript 一样。

我已经在 mux .tmux.conf 中使用了一些 vi 复制模式

# VIM
# ===================================================================

# Vimlike copy mode.
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -t vi-copy 'v' begin-selection
bind -t vi-copy 'y' copy-selection

# Enable vi keys.
setw -g mode-keys vi

# https://coderwall.com/p/4b0d0a/how-to-copy-and-paste-with-tmux-on-ubuntu
bind -t vi-copy y copy-pipe "xclip -sel clip -i"

【问题讨论】:

    标签: vim sh zsh tmux tmuxinator


    【解决方案1】:

    要实现您想要的,您需要在命令行中使用标准输入(xargs 可以做到这一点)并告诉tmux,在new-window 中,使用来自复制缓冲区的参数打开数据:

    bind -t vi-copy y copy-pipe "xargs -I{} tmux new-window 'vim {}'"
    

    这需要更多的调整(获得正确的会话、正确的命令、使用$EDITOR 而不是 vim 等。

    相当危险:想想抄袭/foo/bar/my;rm -rf /

    此外,按原样,这仅适用于相对于 tmux 工作目录的路径。

    【讨论】:

    • "open" plugin 具有此功能及更多功能。
    • 很好,这就是我要找的东西
    • 所以有一个新要求。我已经有一个打开的窗格,其中打开了 vim。我意识到通过bind -t vi-copy y copy-pipe "xargs -I{} tmux select-pane -t 1" 切换到那个。现在我的下一个问题是:如何将 ":edit file" 命令发送到我切换到的窗格中已经打开的 vim 会话?
    【解决方案2】:

    tmux 有一个 mod,允许在“模式”中绑定任何复杂的操作:http://ershov.github.io/tmux/

    有一个示例说明如何使用该补丁标记光标下的单词:

    proc is_word_char {c} {
        print [scan $c %c]
        return [expr {$c > " " && $c != "\x7f"}]
    }
    
    proc mark-current-word {} {
        clear-selection
        set l [copy-mode-screenline]
        set x [copy-mode-get-cx]
        if {![is_word_char [string range $l $x $x]]} return
        incr x
        while {[is_word_char [string range $l $x $x]]} {
            cursor-right
            incr x
        }
        incr x -2
        begin-selection
        while {[is_word_char [string range $l $x $x]]} {
            cursor-left
            if {$x < 1} return
            incr x -1
        }
    }
    
    # Open selection in a vim mini-window (no shell and files)
    bind-key -t vi-copy y tcl {
        split-window -c [f #{pane_current_path}] -l 5 "
            echo -n [shell-quote [copy-mode-selection]] | vim -R -"
    }
    

    因此,在 vim 中打开当前文件:

    mark-current-word
    split-window -c [f #{pane_current_path}] -l 5 "vim -R [shell-quote [copy-mode-selection]]"
    

    【讨论】:

      【解决方案3】:

      所以我使用以下绑定运行它:

      bind -t vi-copy y copy-pipe "xargs -I{} tmux send-keys -t 1 ';edit {}' Enter && tmux select-pane -t 1"
      

      注释

      • 我将 vim 命令 : 更改为 ;
      • 我在窗格 1 中有一个打开的 vim

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        相关资源
        最近更新 更多