Comment

Comment by Skunkleton

> I don't mind dealing with wonky copy and paste once in a while.

This problem is in no way unique to tmux. You have the same problem with any terminal app that takes over drawing, eg vim. That said it is also easy enough to fix.

The solution is OSC52, a terminal escape sequence that the emulator can use to interact with the system clipboard (kitty, alacritty, iterm2 all support this). The first step is to get you a script that writes out data in that protocol. Its easy enough:

    #!/usr/bin/env python3
    import os, base64, sys
    clip = base64.b64encode(sys.stdin.buffer.read())
    for pid in (os.getpid(), os.getppid()): # so that osc52-copy can be invoked by processes that themselves do not have a tty.
        cty = f"/proc/{pid}/fd/1"
        try:
            fd = os.open(cty, os.O_WRONLY)
            if os.isatty(fd):
                os.write(fd, b'\x1b]52;c;') # the actual escape sequence
                os.write(fd, clip)
                os.write(fd, b'\a')
                break
        except:
            continue
        finally:
            os.close(fd)
    else:
        raise SystemExit(f"no tty")

Now you can do this:

$ grep my_thing < some.txt | osc52-copy

And whatever got into osc52 is now on your system clipboard.

Tmux (set -g clipboard on) and nvim (unset clipboard) both have native osc52 support, and the script above can be used to integrate other places.

parent Skunkleton 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton