helix-editor / helix

A post-modern modal text editor.
https://helix-editor.com
Mozilla Public License 2.0
33.6k stars 2.49k forks source link

Clipboard contents lost when `hx` used as scrollback buffer editor within zellij session #5424

Closed gavynriebau closed 1 year ago

gavynriebau commented 1 year ago

Summary

I am using zellij as a terminal multiplexer/session manager (e.g. like tmux), below I will provide info related to zellij but please hear me out before closing this issue because I think the bug lies within hx itself and not zellij and I will explain why.

Zellij contains an "edit scrollback buffer" feature whereby one can press <Ctrl-s> and it dumps the current scrollback buffer to a temporary file before spawning whatever program you've configured as your scrollback editor (defaults to $EDITOR or $VISUAL contents).

I have found that when I use hx as my scrollback buffer editor the contents of the clipboard are lost as described in the bug reproduction steps.

However, when I configure nvim as my scrollback buffer editor the copy/paste behaviour works perfectly fine and clipboard contents are not lost.

I have done some investigation and I believe the problem, which only occurs when using hx as the scrollback editor, is likely due to differences in how xclip is spawned.

Specific differences in how xclip is spawned:

This is a dump of relevant htop output when using nvim to do a copy/paste within scrollback editing:

image

Note that the session ID and process group ID of xclip is different to that of nvim, I believe that is because nvim calls setsid before exec'ing xclip.

Now here is a dump of relevant htop output when using hx to do a copy/paste:

image

Note that the session ID and process group ID of xclip is the same as that of the spawned hx.

My suspicion is that when hx is closed the spawned xclip process is also closed because they are both in the same session / process group. Pasting then fails because xclip has been closed and X11 no longer has a source from which to get the clipboard contents.

Reasons I believe the bug is within hx and not zellij:

  1. The same bug does not occur if I configure nvim as my scrollback editor
  2. I have tried manually patching hx to call fork then setsid before launching xclip for copying and confirmed this fixes the bug.

Reproduction Steps

Environment setup:

  1. Install zellij
  2. Edit the config file, e.g. ~/.config/zellij/config.kdl
  3. Change the value of scrollback_editor to the location of hx, e.g. "/usr/local/bin/hx"

To reproduce the bug itself:

  1. Open new terminal
  2. Enter zellij to start a new session running version 0.34.4 of zellij
  3. Type ls -l to produce some terminal output
  4. Press <Ctrl-s> to trigger scrollback edit mode (this dumps terminal contents to a file and opens hx for editing it).
  5. Use hx to select some text then press <Space>y to yank to clipboard
  6. Enter :q! to quit hx
  7. Attempt to paste the yanked text back into the terminal or any other app

I expected this to happen:

The yanked text would be pasted

Instead, this happened:

Nothing is pasted

Additional info

It is only at step 8 (where hx is closed) that the clipboard contents are lost. If the above steps are taken but step 8 is skipped, the copy/paste process works correctly. This is not ideal because the workflow I want to use is to use scrollback editing to quickly copy a subset of the terminal output text.

Helix log

No response

Platform

Linux (X11 running BSPWM)

Terminal Emulator

Alacritty 0.11.0

Helix Version

helix 22.12 (96ff64a8)

archseer commented 1 year ago

Thanks for the detailed reproduction! Looks like we'd need to call setsid via libc/nix crate on unix platforms.

gavynriebau commented 1 year ago

Some related info: