Search code examples
bashvimterminalvitmux

How to have tmux apply an option only when vim is not running?


I would like to have an option in the tmux configuration file to be activated only if vim is not running in the current panel/window. In particular, the line is the following:

bind-key -n MouseDown2Pane run "tmux set-buffer \"$(xclip -o -sel primary)\"; tmux paste-buffer"

which allows me to use the middle click to paste directly from the primary clipboard to tmux. It works great, the only problem is that it has a weird behaviour inside vim (pasting in wrong place, strange skipping of lines...). If I comment out that line, pasting with middle click into vim works as espected, but I lose the pasting functionality in tmux, obviously.

So, my question is: how do I make tmux apply that keybinding ONLY if vim is not running?

By doing some research, I found the following approach that looks promising, but I cannot make it work.

is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

if-shell "$is_vim" {command here}

Edit: Alternatively, I would also be happy to have the "tmux set-buffer \"$(xclip -o -sel primary)\"; tmux paste-buffer" command to work properly when pasting in vim. But that, for some reason, looks like a more difficult task.


Solution

  • For the first part of your problem, you can either use something like #{?#{m:\*vim\*,#{pane_current_command}},0,1} to invert the match, or you can use the second argument of if-shell instead of the first, so you would do if -F '...' '' 'mycommand' where the second argument is just empty.

    if-shell arguments ARE tmux commands, but they do need to be one argument. So you need to put them in quotes - you will want something like:

    bind -n MouseDown2Pane if -F '#{?#{m:\*vim\*,#{pane_current_command}},0,1}' 'run "xclip -o -sel primary | tmux load-buffer - ; tmux paste-buffer"'