ojroques / vim-oscyank

A Vim plugin to copy text through SSH with OSC52
BSD 2-Clause "Simplified" License
615 stars 38 forks source link

Question: Why does this work in neovim #32

Closed unphased closed 1 year ago

unphased commented 1 year ago

So I've already done a lot of consolidating clipboard behavior for my vim/terminal/tmux workflow. I use this script:

❯ cat pbcopy        
#!/bin/bash

# pbcopy impl for linux

### Make a best effort attempt to preserve stdin for later retrieval.

# Save to:
# - system clipboard,
# - our system clipboard sentinel file (for its timestamp) (actually this was determined not to be
# useful, so nevermind)
# - our fallback clipboard file
# - emit OSC52 to active terminal emulator

if [ -n "$DISPLAY" ]; then
    # Sends with EOF newline to ~/.clipboard but prevents it from going into xclip
    # This is done because it is better convention to have a file end in a newline
    tee "${1:-$HOME/.clipboard}" | head -c -1 | xclip -selection clipboard
    # touch "${1:-$HOME/.xclip-set-at}"
else
    cat > "${1:-$HOME/.clipboard}"
fi

# Send OSC52 to terminal emulator. stdin is already consumed. Just use the clipboard file to keep
# this code sane.
echo $'\x1b]52;c;'"$(head -c -1 < "${1:-$HOME/.clipboard}" | base64)"$'\x1b\\'

and I call into this from tmux, and I call it from vim, and i can call it on the zsh terminal, etc., it's very nice because it's a unified implementation.

I really like the way you've gotten the OSC yank working in this plugin for vim and nvim. The last line in my pbcopy script here is inspired by your plugin here, and was recently added to this script.

Curious if you might have insight into why it doesn't work for me in neovim. As you can see my script just emits whatever output needs to go out to the terminal unmolested, and vim appears to succeed with that, but neovim does not. I'm curious if you had to do anything to work around this.

unphased commented 1 year ago

I guess I'm realizing there's little need for me to keep vim sticking to my "standardized" pbcopy clipboard manipulator script. Elegant though it may be sharing that code, all the creature comforts specific to vim which youve provided in this plugin are worth something, and I think the behavior of getting the buffer into the client machine clipboard is always what I want to do with essentially zero exceptions, there's actually no reason for me to follow that premise. And I will be able to eliminate the shenanigans that I did to allow me to adequately pass a visual-selected buffer or individual lines into my pbcopy script (which is already a bit gnarly).

Still, would love to learn why my approach doesnt work in nvim when it actually works in vim.

ojroques commented 1 year ago

I know that Vim and Neovim have different behaviors when you execute a shell command from inside them so it's probably related to that. How exactly do you invoke pbcopy from inside Vim and Neovim?

unphased commented 1 year ago

One test I did was to open :Terminal and execute the shell script.

unphased commented 1 year ago

Sorry I'm a bit all over the place with this topic. I am gonna be totally honest with you -- i read what i wrote again and I don't really think I understand the issue either. Let me hijack my own topic, then:

What I can report right now is that on my macbook (I was testing on my Linux box earlier...), I had to set let g:oscyank_term = 'default' in my vimrc to make this work from inside tmux.

It looks like the problem is that get_OSC52_tmux doesn't work for me. I am not an expert on this, but I think I understand what's going on. This

  return "\ePtmux;\e\e]52;c;" . b64 . "\x07\e\\"

Is to leverage the

allow-passthrough [on | off]
         Allow programs in the pane to bypass tmux using a terminal escape
         sequence (\ePtmux;...\e\\).

feature of tmux. But most people like myself don't have this option enabled, and furthermore the regular OSC52 clipboard ansi escape behavior is handled properly transparently by tmux.

So I suggest let g:oscyank_term = 'default' to be the default behavior of the plugin.

I have a hunch this isn't done for some security reason?

ppwwyyxx commented 1 year ago

I also suggest this plugin by default uses "default mode" inside tmux, instead of using the passthrough.

Basically, there are 2 ways to make this plugin work in tmux:

  1. "default mode" + user enable set-clipboard in tmux.
  2. "tmux mode" + user enable allow-passthrough in tmux.

(2) has security concerns, because it allows ALL escape sequences to pass through tmux. (1) is cleaner because it avoids an extra mode -- the extra mode can be removed in theory if we get rid of (2) completely.

ojroques commented 1 year ago

I've deeply refactored the code, and the default mode + setting set-clipboard is now the default behavior. You can still use the second mode, just check :h oscyank-tmux.