9

How to copy an output on a GNOME terminal to clipboard from command line.

I'd like to be able to paste text from my console textfiles smoothly to Firefox.

4 Answers 4

5

It's an old question but I thought it deserves a working answer. Either xsel or xclip can do this job. Personally I prefer to use xsel, which has a cleaner syntax.

echo "helloworld" | xsel -b

Then change to some app and do a paste (with CTRL-V, the paste option, etc.).

That's it!

What if you are doing the opposite, and copy some text in your browser and want to paste it into your terminal? (Well, of course you can just paste.) Or type:

xsel -b                                           # outputs "helloworld"

X has got 3 different selections or clipboards. The "primary" one is the one that activates with the middle mouse button. That's what you get if you call xsel or xclip without any arguments. To get access to the "clipboard" that is used for copy/paste, CTRL-C/CTRL-V etc, the -b is required.

I recommend you do use the -b option and interoperate with the clipboard rather than the X11 middle-click "primary" selection -- because in the transition from X to Wayland, access to the X11 "primary" selection may not survive in Gnome or Wayland. Here's a relevant Gnome bugzilla thread.

Both xsel and xclip interact with X, not with Gnome directly, but so long as your display manager is running XWayland (which it should do for years to come), then xsel -b will continue to work and be safe to use in scripts. Nothing is 100% future proof but xsel -b is fine for now.

If you prefer to use xclip, the syntax equivalent for the two examples above is:

echo "helloworld" | xclip -selection c
xclip -o -selection c                             # outputs "helloworld"
1
  • While xsel's syntax is cleaner is seems to have a bug which prevents pasting large amounts of text into Chromium based software (Chrome, Electron, etc), so I ended up using xclip. Thank you for the clear examples!
    – arikfr
    Oct 25, 2017 at 7:58
2

You can highlight the text then right click and select copy, or pipe it to xclip:

some-command | xclip

xclip may or may not be available depending on your distribution.

2
  • xsel is similar to xclip and can be used similarly.
    – alfplayer
    Dec 14, 2009 at 23:19
  • Simply calling xclip doesn't do it by default, but if you append it with the following options the -sel clip or -selection clipboard it you can paste the output to your browser etc.. echo "http://superuser.com/" | xclip -sel clip, alt-tab to chrome, ctrl+l, ctrl+v, enter... vola! P.S. I don't know if xsel has similar options to make it work. By default it doesn't work.
    – Timo
    Aug 15, 2012 at 9:45
2

You can also just mark the terminal output you want to copy and it is copied to your clipboard automatically.

1
  • Technically it's called the cut buffer, the clipboard is something else.
    – TRS-80
    Jan 14, 2010 at 2:55
1

After selecting the text, press Ctrl-Shift-C to copy it to the clipboard. The extra Shift is required as Ctrl-Cis already used in shells. Similarly, to paste, press Ctrl-Shift-V.

You can also just select it then middle-click in Firefox to paste it. This uses the cut buffer, which is slightly different to the clibpoard.

Bonus gnome-terminal tip - Ctrl-click a url to automatically open it in your default browser.

You must log in to answer this question.

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