6
votes

i am a vim user and got used to the gf command, which opens the file under the cursor.

Now i wanted to ask, if there is something like that for tmux.

I can navigate through a tmux-pane and it happens often that there is a file-path under the cursor. Now i like to have the possibility to open that file under the cursor with vim.

  • A: in the current window
  • B: in another window which includes and opened vim

Maybe there is a possibility to run a sh-script in that navigation-mode when invoking a special key-combination? that would make it possible to write my own scripts like i got used to in vim with vimscript.

I am already using some vi-copy modes in mux .tmux.conf

# 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"
3

3 Answers

1
votes

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.

1
votes

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]]"
0
votes

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