Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: copy/paste to system clipboard #559

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

stevalkr
Copy link
Contributor

@stevalkr stevalkr commented Jan 14, 2025

This PR
Closes #556
Closes #569
Closes #156

Known bugs:

  • When transferring files between different operating systems, CJK text may become garbled due to encoding issues. I found X11 fails to output the correct (oil.nvim recognizable) address when pasting. But I don’t intend to address this issue in this pull request. It's related to functions like fs.posix_to_os_path or similar.

@github-actions github-actions bot requested a review from stevearc January 14, 2025 18:02
@stevalkr stevalkr marked this pull request as draft January 15, 2025 09:30
@stevalkr
Copy link
Contributor Author

Hi @stevearc, I’m having a bit of trouble with the sshFs.load function. It works great for SSH connections that don’t require passwords, but for those that do, the float window can’t open because vim.wait is blocking it. Is there a way to let this function wait for the connection without blocking vim.api.nvim_open_win?

@stevearc
Copy link
Owner

This PR looks like it's doing two things: yanking/pasting using osascript, and changing the buffer parsing logic to allow for oil:// urls. The first is fine, the second, as you have discovered, adds a lot of complexity. Especially this handling of oil-ssh:// urls; isn't that case impossible to encounter with the copy/paste? The only way it could occur is if people started manually inputting or copying oil urls, which is an entirely new workflow that I'm not convinced we should support.

As an alternative approach to support pasting, what if we found the parent directory of the pasted file, performed an async read of that directory, and then properly inserted the correct line into the current buffer? It's more logic on paste, but it leaves the rest of the internals the same.

@stevalkr stevalkr force-pushed the copy_to_system_clipboard branch from 1cfc643 to 3c8c533 Compare January 30, 2025 10:30
@stevalkr
Copy link
Contributor Author

Hi @stevearc! I’ve implemented the method you suggested. I haven’t thoroughly tested all the cases, but it should work fine. However, the code might need some cleaning up :)

@stevalkr stevalkr marked this pull request as ready for review January 30, 2025 10:38
@stevalkr
Copy link
Contributor Author

stevalkr commented Feb 4, 2025

I’ve also added support for Linux (but it needs some testing). But the default file explorer 'nautilus' on Ubuntu doesn’t support the MIME type 'text/uri-list'. I think we could add a config option so the user can specify their own command for reading and writing the clipboard. Also, include an example of supporting 'nautilus’s special protocol in the readme.

copy:

echo -en 'copy\\nfile://%s\\0' | xclip -i -selection clipboard -t x-special/gnome-copied-files

paste:

xclip -o -selection clipboard -t x-special/gnome-copied-files | grep --text --color=never file://

Copy link
Owner

@stevearc stevearc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm...I tested it on my mac but I can't seem to get it working. Copying a file in finder and pasting produces

...tevearc/dotfiles/vimplugins/oil.nvim/lua/oil/actions.lua:503: bad argument #2 to 'fs_realpath' (function or callable table expected, got number)
stack traceback:
^I[C]: in function 'fs_realpath'
^I...tevearc/dotfiles/vimplugins/oil.nvim/lua/oil/actions.lua:503: in function <...tevearc/dotfiles/vimplugins/oil.nvim/lua/oil/actions.lua:501>

If I copy to the system clipboard, there's no feedback or anything but I can't seem to paste anything into finder. Any ideas?

@github-actions github-actions bot requested a review from stevearc February 14, 2025 08:14
@stevalkr
Copy link
Contributor Author

stevalkr commented Feb 14, 2025

Copying a file in finder and pasting produces

...tevearc/dotfiles/vimplugins/oil.nvim/lua/oil/actions.lua:503: bad argument #2 to 'fs_realpath' (function or callable table expected, got number)
stack traceback:
^I[C]: in function 'fs_realpath'
^I...tevearc/dotfiles/vimplugins/oil.nvim/lua/oil/actions.lua:503: in function <...tevearc/dotfiles/vimplugins/oil.nvim/lua/oil/actions.lua:501>

My bad, should be resolved in the latest commit

If I copy to the system clipboard, there's no feedback or anything but I can't seem to paste anything into finder. Any ideas?

I added a notify when file is copied to system clipboard, but I can copy and paste as expected. Could you provide me your macOS and shell version? It might be an issue with shell unquoting or a change in osascript

Note: none of the commands would fail if the input is not valid. It’s only a notify, doesn’t mean it succeeded

cmd =
"osascript -e 'on run args' -e 'set the clipboard to POSIX file (first item of args)' -e 'end run' '%s'"
elseif fs.is_linux then
cmd = "echo -en '%s\\n' | xclip -i -selection clipboard -t text/uri-list"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xclip is only for X11, for wayland you would have to use wl-clipboard, right?

Copy link
Contributor Author

@stevalkr stevalkr Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Could you help me find and test the equivalent command using wl-clipboard? I’m not using Linux anymore, so it’s difficult for me to test these things out.

Edit: I found it quite straightforward. Wayland is now supported in the latest commit. Please give it a try! :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm on MacOS unfortunately, just spotted this and thought I'd let you know :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants