Skip to content

Add support copying to the clipbaord via osc52 (in particular, for copying from remote hosts over SSH or similar) #747

@cstrahan

Description

@cstrahan

For terminal emulators that implement support for it, the ANSI OSC52 sequence provides a means to copy to the clipboard, without reliance on external tools (like xclip and similar), as the terminal emulator itself performs the requested clipboard manipulations (invoking the necessary APIs for the host it is running on) on behalf of terminal application wishing to modify the clipboard.

It would be great if ov would support OSC52, as this allows for a host-agnostic means of copying to the clipboard, both in local shells and in remote shells (for example, over SSH).

(In fact, I discovered ov because I thought "there surely has to be a modern alternative to less that provides copying via OSC52, which would be great because I often need to copy stuff journalctl and similar, and that would be really convenient" -- support for this alone would make me swap out less for ov)

Depending on the terminal emulator, this can be queried by the CLI/TUI application via XTGETTCAP, so when support is detected, a sort of enable_clipboard="auto" setting could be implemented to default to using OSC52 which, for example, is what neovim does:

System clipboard synchronization

Nvim 0.10 can now use the OSC 52 escape sequence to write to (or read from) the system clipboard. OSC 52 is only used automatically when (1) it is able to determine that the terminal emulator supports it (via XTGETTCAP), (2) it is running in an SSH session, and (3) when the 'clipboard' option is unset. Note that some terminal emulators do not support reading from the system clipboard with OSC 52, or only allow it after prompting the user for confirmation. It is recommended to use the traditional “paste” key binding (e.g. Cmd+V on macOS or Ctrl+Shift+V on Linux) to paste into Nvim from the system clipboard and use "+ to copy to the clipboard inside Nvim (see :h quoteplus).

I know, from reading the readme, that ov uses atotto/clipboard to manage the clipboard, so maybe the actual implementation should live there, but I figure it makes sense to track support here.

In fact, it looks like it's something that's already being requested here: atotto/clipboard#73

While I have yet to verify it, I suspect it's likely not hallucinating when Gemini claims that these terminal emulators support OSC52:

  • Alacritty
  • Contour
  • foot  
  • hterm
  • iTerm2
  • kitty
  • Konsole
  • rxvt
  • st
  • tmux (While primarily a terminal multiplexer, tmux also implements OSC 52 support. It's important to note that tmux manages its own clipboard buffers, which can sometimes lead to unexpected behavior when interacting with the system clipboard via OSC 52.)
  • WezTerm
  • Windows Termina
  • screen
  • urxvt (While it doesn't support OSC 52 out of the box, support can be added with a script.)

I can at least vouch for WezTerm, kitty, and ghostty (which Gemini missed, which is unsurprising given how new it is) having support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions