42

Is it possible to copy the output into the clipboard without using the mouse?

For example, I would like to do something like this:

$ pwd >> clipboard

1
  • cat /etc/passwd | xsel --clipboard Apr 10, 2012 at 7:25

9 Answers 9

46

You can get and use the xclip and xsel commands. Instructions for obtaining and using them are here.

Without any arguments, xclip copies into the primary (middle-click-paste) clipboard instead of the Ctrl-C/Ctrl-V/right-click-context-menu clipboard.

Try echo foo | xclip -selection clipboard to copy some text into the latter.

To output the clipboard to standard out: xclip -selection clipboard -o

The equivalent utility for MacOS is pbcopy (and pbpaste)

8
  • 2
    @brauliobo: Where's "here"? Seriously, what do you mean? It doesn't work in what way, exactly? Apr 25, 2014 at 18:26
  • 2
    @DennisWilliamson I've tried cat file | xclip and it doesn't work
    – brauliobo
    Apr 25, 2014 at 18:44
  • 4
    @brauliobo: It doesn't work in what way, exactly? Error message? Misbehavior? Magnetic field anomalies? Apr 25, 2014 at 18:46
  • 9
    @brauliobo: I presume klipper is a clipboard viewer. Try cat file | xclip -selection clipboard Without the extra arguments, xclip copies into the primary (middle-click-paste) clipboard instead of the Ctrl-C/Ctrl-V/right-click-context-menu clipboard. Apr 25, 2014 at 19:48
  • 4
    @Dennis, maybe you can add the last comment to the answer as this is rather important and not obvious and a great addition.
    – Kvothe
    Oct 31, 2018 at 16:25
7

If you are on a MAC you can use pbcopy like this:

pbcopy < thing_to_write_to_clipboard
1
  • 1
    Or your_command | pbcopy, won't output anything but will copy the result into the clipboard. Dec 6, 2023 at 13:30
3

I'm afraid there is no such thing as "clipboard" in most linux shells, at least none I know of. There's a clipboard in the X server (if your system has X11, you may access it using xclip command). If you connect to your shell account using some ssh/terminal emulator software, like PuTTy, you can use the clipboard of the system you are using, but that still would be an external clipboard and you might not be able to avoid using mouse.

What I have found is this little neat bash script, found at http://geek.phatus.com/2010/01/create-a-simple-yet-very-useful-bash-clipboard/ . It basically creates a temporary file which serves as a clipboard - you can clear, execute, write etc. your temporary data using a set of commands, which is actually what a clipboard does ;)

1
  • the op was just a little bit unclear about his setup: he meant the clipboard of the xserver.
    – akira
    Feb 5, 2010 at 8:44
3

on Wayland xcopy doesn't seem to work, use wl-clipboard instead. e.g. on fedora

sudo dnf install wl-clipboard

tree | wl-copy

wl-paste > file
1

The bash variable $PWD contains the path directory of your terminal window. you might only want to pipe the command pwd into the clipboard so:

echo $PWD | pbcopy

also: you can pipe the output of a command to a file.

like pwd > file.txt

so if you save a variable to your bash file you can :

pwd > $variable

cat $variable |pbcopy
3
  • 1
    Welcome to SuperUser! How is this better than the other answers? Aug 6, 2022 at 7:24
  • I don't have xclip.
    – S.Doe_Dude
    Aug 9, 2022 at 21:28
  • But that works only under macOS/Darwin. Feb 22, 2023 at 12:22
1

In addition to the answers above I would add that MSYS2 and Cygwin have neither xclip nor xsel by default, but implement /dev/clipboard:

$ pwd >> /dev/clipboard
0

I made a small tool providing similar functionality, without using xclip or xsel. Std out is copied to a clipboard and can be pasted again in the terminal, see:

https://sourceforge.net/projects/commandlinecopypaste/

Note, that this tool does not need an x-session. The clipboard can just be used within terminal and not be pasted by ctrl+v or middle-mouse-click into other x-windows

1
  • Really — your code is not using 'the clipboard' per se, but rather just copying the contents from the stdin to a file, and reading from it. Feb 22, 2023 at 12:33
0

And an ugly way, for cases when you want to copy terminal output to clipboard but don't have a mouse connected to your machine — using "mouse keys" feature of X.

Press Shift+NumLock, and your numpad keys will work to control cursor movement. You can then press Num 0 to simulate press & hold LMB, and move the cursor as you need, then press Num 5 to simulate release of LMB. Now you've selected your text. To simulate RMB you press Num - to change active mouse button, then Num 5 to get context menu. Now you want to return to LMB mode: press Num /. Now you can move your cursor to "Copy" menu entry and press Num 5. That's all. You have your text in clipboard. Press Shift+NumLock to get your numpad to its original function.

PS: In some distros (e.g. Kubuntu) mouse keys shortcut appears to be disabled by default or somehow hidden.

0

If you run the shell within a screen or byobu session, you can do this:

  1. Enter scrollback mode: Ctrl+A, [ or F7 in byobu.
  2. Move the cursor to the start of the text you want to copy, hit space.
  3. Move the cursor to the end of the text you want to copy and hit Enter.
  4. To paste text, hit Ctrl+A, ] or Alt + Insert.

So you can copy not only a command output, but any part of the terminal window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .