tmux: how to open file under cursor

无人久伴 提交于 2019-12-06 09:30:24

To achieve what you want, you need to use the stdin in your command line (xargs can do that) and tell tmux, in a new-window, to open the data with the arguments from the copy buffer:

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

This needs more tuning (getting the right session, the right command, use $EDITOR instead of vim etc.

It is quite dangerous: Think copying /foo/bar/my;rm -rf /.

Also, as-is, this will only work for paths relative to tmux' working directory.

There's a mod for tmux allowing to bind an action of any complexity in 'mode': http://ershov.github.io/tmux/

There's an example of how to mark the word under cursor using that patch:

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 -"
}

Hence, to open the current file in vim:

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

So i got it running with the following binding:

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

notes

  • i changed vim command : to ;
  • i have a open vim in pane 1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!