3

It occurred to me that there are multiple clipboards on any given Linux console:

  • First is the bash clipboard, this can be invoked by Ctrl-U / K, cut all line before (U) or after (K) cursor into clipboard, or Ctrl-W, to cut the word on the left side of the cursor (is there a corresponding "cut word on right side"?), and then Ctrl-Y to paste somewhere else.

  • Then we have the X clipboard, which is setup as follows (I don't know where the names pbcopy and pbpaste come from, but I have seen these names dozens of times, so it seems a lot of people use that nomenclature for some reason):

sudo apt install xclip xsel alias pbcopy='xclip -selection clipboard' # To use xclip for copy alias pbpaste='xclip -selection clipboard -o' # To use xclip for paste alias pbcopy='xsel --clipboard --input' # To use xsel for copy alias pbpaste='xsel --clipboard --output' # To use xsel for paste 
  • Then, additionally, if you are on WSL, there is a third clipboard, from Windows, which you can send to quite simply, e.g. echo 123 | clip.exe. To do the reverse, and paste into the WSL console, you can use powershell.exe -noprofile Get-Clipboard > file.txt.

It would be be good to completely control how information is sent/retrieved from these various clipboards easily.

  • I can pipe information into the X clipboard (with xclip and xsel), but I have not found a way to pipe things into the bash buffer; how do we pipe into the bash buffer programmatically?

  • How can I pipe something into all 4 clipboards (assuming that xclip and xsel are independent?) from one output in single command (I seem to remember that tee can do this, but not certain)? By knowing this, I would then be able to send receive information from any of the 4 clipboards.

echo 123 | <way-to-pipe-the-output-to-all-or-any-clipboards> 

For reference, though does not answer the above. https://stackoverflow.com/questions/5130968/how-can-i-copy-the-output-of-a-command-directly-into-my-clipboard

2
  • 2
    Another one for you: tmux has its own buffer/clipboard Commented Oct 13, 2021 at 12:47
  • I realised that after posting, but thought I'd just focus on these areas (though I know that a lot of people stay in tmux at all times, so for them the tmux buffer is quite important of course). Commented Oct 13, 2021 at 13:52

2 Answers 2

2

I don't know where the names pbcopy and pbpaste come from

These are commandline tools on the Mac.

the bash clipboard

This is not a really a clipboard, but an internal buffer in the readline library that bash uses. It's called the kill ring, following emacs, and works as in emacs. See man bash in the readline section for more details.

Then we have the X clipboard, which is setup as follows

Actually X itself works with a primary and secondary selection, and the selections are managed by the applications, not the X server. The X clipboard is an addition to that.

To make things worse, libraries like Gtk do have their own clipoard.

I have not found a way to pipe things into the bash buffer

The man page doesn't mention any way to manipulate the kill ring somehow (at least I am not aware of one), though you can re-bind the various commands that work on the kill ring.

How can I pipe something into all 4 clipboards

You can't, unless you write your own command, or use different existing commands.

If you want to keep the X selection and the Windows clipboard synchronized (so changing one would change the other), I think I have seen tools for this, but I'd have to search myself again.

Keeping the kill ring in all bash instances synchronized with something just isn't going to work, given the architecture. And nothing prevents you to paste from X or Windows in a terminal.


If you are thinking "but I can use a single clipboard under Windows, so I want the same under WSL": That's not how it works.

2
  • I wasn't thinking about a single clipboard in that way, it was more just "if I write a script, and I want to inject the output into the/a clipboard, I want to be able to send it the right clipboard or all clipboards depending on what is most useful". I guess that I can send data to Windows via clip.exe or to X via xclip or xsel and that's all I need really. Thanks for the very interesting info on the kill ring etc (it sounds dangerous in there! 😮), shame we can't programmatically put/get information to/from that. Commented Oct 13, 2021 at 14:41
  • 1
    The mnemonic for Ctrl-K is "kill", hence "kill ring". And if you've not already read up on it, it's a ring, so several buffers, and you can keep accessing older "cuts" (or "kills", if you want). Commented Oct 13, 2021 at 15:43
2

Several questions there, but I think it boils down to:

  • How to pipe to the Bash (technically Readline) kill ring
  • How to pipe to multiple clipboard commands at once

Pipe to Bash/Readline kill ring

Readline doesn't offer any direct manipulation commands. As far as I can tell from looking at the documentation, it doesn't even offer any API access to the kill ring, other than a function rl_kill_text that requires that the text already be entered (or inserted via rl_insert_text).

In theory you could use a "sendkeys"-like mechanism to insert the text you want to load on the kill ring and then "sendkeys" the CtrlU. But I haven't been able to get that to work with xdotool in a WSLg xterm window under Windows 11.

Also see this question for a fairly detailed discussion on inserting text (which, in theory, could include the control codes for CtrlU) into bash.

But for now, I'm going to have to leave this as "maybe this is possible, but to my knowledge no one has figured it out yet."

Bonus: Pipe to the tmux paste buffer

I know this came up in the comments, so I figured I'd add it in since I couldn't give you a Bash/Readline solution.

This one is pretty easy:

echo "Hello, World" | tmux load-buffer - 

Pipe output to multiple commands

While this is pretty well covered in this answer with the syntax, there are some nuances when it comes to xclip's behavior in a pipeline.

The general structure as listed in the accepted answer to that question is:

command | tee >(pipe1) >(pipe2) >(pipe3) 

But there seem to be some inconsistencies when xclip is one of those commands. I believe it has to do with the fact that xclip detaches from the terminal when run.

As a workaround, just output the end result of the pipeline to xclip:

echo "Hello, World" | tee >(clip.exe) >(tmux load-buffer -) | xclip 

Test script:

#!/usr/bin/bash echo "1" | xclip echo "2" | clip.exe echo "3" | tmux load-buffer - echo "Hello, World" | tee >(clip.exe) >(tmux load-buffer -) | xclip xclip -o powershell.exe -noprofile Get-Clipboard | tr -s '\r' '\n' tmux show-buffer 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.