There are two programs helping to handle both methods.
They appear not to be installed by default (at least on OpenSUSE and Fedora).
But the package managers should provide them both.
One is xsel
the other is xclip.
Technical background
Note, that there are two distinct mechanisms to copy and paste in X11:
the selection and the clipbard.
The selection is used by highlighting some text with the mouse
and pasted with the middle button (which is, on most mice, the wheel).
To be precise, there are primary and secondary selections,
but the latter is hardly used.
The clipboard uses Ctrl-C and Ctrl-V to copy and paste (or the menu entries below Edit).
If I understood correctly, the selection is from the X server.
It is not stored in the server, but only mediated between source and target process.
If the source process ends, the selection can no longer be pasted.
Desktop environments have a clipboard daemon (xclipboard, Klipper on KDE)
that handles the clipboard.
Both programs xsel
and xclip
keep running in the background to provide their selection content
and exit when another application takes over.
xsel
Xsel is easier to use, because it detects if you're using it as input
or ouput:
xsel < file # reads the content of file into the primary selection
xsel > file # writes the primary selection to the file
By default, the program uses the primary selection (that one with the middle mouse button).
With the parameter -b
(or --clipboard
), the clipboard (Ctrl-C, Ctrl-V) is used.
The secondary selection can be addressed with -s
(--secondary
).
The content of primary and secondary selection can be exchanged with xsel -x
.
It's also possible to delete the current content of the selection with -c
(only if the source is xsel itself) and -d
(requests the source program to discard the selection).
Input can also be appended to the current content with -a
.
To keep the content after the program ends, use -k
.
According the the manual page,
this requests the data from the current source process and stores it for pasting
while running in the background.
There are options for the X11 interaction (display, etc.),
please refer to the man page if you need them.
For me, the programs works without.
xclip
The other program, xclip
, is less convenient in day-to-day use.
To get the content of selection or clipbaord, it requires the command line option -o
.
Further, the non-default secondary selection and clipboard are addressed with the
long options -selection secondary
and -selection clipboard
(both can be abbreviated -se s
and -se c
).
With -l
(--loops
),
it can be instructed to wait in the background only for a number of requests from other applications
before terminating.
More Examples
Both should work as expected in shell pipes:
they place their input in the selection/clipboard and print its content when called.
Examples for the selection
Read a file into the selection:
or
Put something in the selection using a pipe:
some command | xsel
some command | xclip
Append the output to the selection (xclip can't do that)
Retreive the selection to the standard output
Redirect the selection to a file:
xsel > file
xclip -o > file
Append the selection to the file:
xsel >> file
xclip -o >> file
Examples for the clipboard
Put something in the clipboard (e.g. to paste it in a GUI via Ctrl-V):
some command | xsel -b
some command | xclip -selection clipboard
Retreive something from the clipboard (that was copied by Ctrl-C or Ctrl-X):
xsel -b
xclip -o -selection clipbaord
Network
It is even possible, to use them over X11-forwarding SSH sessions:
A file read into xsel
(or xclip
) on a remote system can be retrieved locally and vice-versa:
erde$ ssh -X sonne
sonne$ xsel < .bashrc
sonne$ exit
erde$ xsel > .bashrc
Note for gvim
The selection is in the register "*
, the clipboard in "+
.
Sources: commandlinefu.com,
xsel project page,
xclip project page and the man pages.