Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I was recently thrown there from the windows world and I'd like to ask some questions about clipboards in Linux (and Unix?) systems. What is X clipboard? How many clipboards are in Linux? Actually, I'd like to copy command from terminal to clipboard and then paste it in an other application (gedit, skype etc.). I entered echo !! | xclip in terminal. However when I tried to paste the command to other applications (via Shift+Ins or Ctrl+V), actually an old content have been pasted. At the same time I can paste this command in terminal.

share|improve this question
add comment

marked as duplicate by Gilles, Anthon, jasonwryan, Evan Teitelman, slm Jul 24 '13 at 23:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 3 down vote accepted

The xclip manpages (man xclip) say this:

 -selection
      specify which X selection to use, options are "primary" to use XA_PRIMARY
      (default), "secondary" for XA_SECONDARY or "clipboard" for XA_CLIPBOARD

So you can specify the location:

echo !! | xclip -selection <selection>

Where <selection> is one of primary, secondary, clipboard.

Description of these from the ArchWiki: Clipboard

Of the three selections, users should only be concerned with PRIMARY and CLIPBOARD. SECONDARY is only used inconsistently and was intended as an alternate to PRIMARY. Different applications may treat PRIMARY and CLIPBOARD differently; however, there is a degree of consensus that CLIPBOARD should be used for Windows-style clipboard operations, while PRIMARY should exist as a "quick" option, where text can be selected using the mouse or keyboard, then pasted using the middle mouse button (or some emulation of it). This can cause confusion and, in some cases, inconsistent or undesirable results from rogue applications.

That means it depends on your environment. It may have inconsistencies, if the applications use different selections. Though if you use a desktop enivornment like Gnome it should work fine.

Also as mentioned by Kartik, you can copy-paste in most terminals with Ctrl+Shift+C/V.

Most applications in linux also support selection of text with the mouse to store something in the clipboard.

To get the content of from the clipboard use the -o flag:

xclip -o

If you use it often you can create aliases for those commands in you .bashrc:

alias cbcopy='xclip -selection clipboard'
alias cbpaste='xclip -selection clipboard -o'
share|improve this answer
add comment

For the record, in Cygwin you can direct your output to /dev/clipboard like so:

echo !! > /dev/clipboard

This copies the output to the system standard clipboard and you can use Ctrl+V or whatever to paste it.

share|improve this answer
3  
But that's only for Cygwin, isn't it? –  ahilsend Jul 24 '13 at 15:09
    
Oh, how embarassing! I didn't know that. Yeah, I have to use Cygwin, unfortunately. I'll update the answer. Thanks for the info –  Nathan Wallace Jul 24 '13 at 15:23
add comment

To copy anything in linux from the terminal, there are two ways:

  1. Select it with the mouse to copy and paste it with middle click.
  2. Select with mouse and copy it with the keyboard shortcut. (which is different on different terminals, Ctrl-Shift-C/V on Ubuntu)

Sometimes you can copy and paste anything with Ctrl+C/V like windows (but not on the terminals).

EDIT: echo !! | xclip copies only the previous command.

share|improve this answer
    
Thank you. So, there in no way to copy in Linux from terminal without mouse? –  Loom Jul 24 '13 at 12:38
    
@Loom I think you can copy with echo "something" | xcopy and paste with Shift + Ins –  Kartik Jul 24 '13 at 12:42
    
Thanks. What xcopy is and where I can find it? –  Loom Jul 24 '13 at 12:48
    
@Loom Maybe outside terminal you can paste with Ctrl+V –  Kartik Jul 24 '13 at 12:49
    
@Loom I'm sorry i meant to say xclip –  Kartik Jul 24 '13 at 12:51
show 3 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.