tmux: how to open file under cursor
Asked Answered
R

4

7

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"
Roslyn answered 15/2, 2016 at 8:7 Comment(0)
H
2

I've been searching for an answer for years and ended up making a tmux plugin: https://github.com/artemave/tmux_super_fingers. It still baffles me how such in integral part of terminal based workflow is not a solved problem (well, it is now).

Harsho answered 29/7, 2022 at 17:56 Comment(0)
J
1

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.

Judaist answered 15/2, 2016 at 14:56 Comment(3)
There's an "open" plugin that has this functionality and more.Judaist
nice, thats what i was searching forRoslyn
so there is one new requirement. i already have one open pane with vim opened in it. i realized to switch to that via bind -t vi-copy y copy-pipe "xargs -I{} tmux select-pane -t 1". now my next question: how i can send the ":edit file" command to that already open vim session in the pane i switched to?Roslyn
D
1

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]]"
Delitescent answered 18/4, 2016 at 15:32 Comment(0)
R
0

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
Roslyn answered 15/2, 2016 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.