There are a couple ways. Some of the ways that have been mentioned include (I think) tmux, screen, vim, emacs, and the shell. I don't know emacs or screen, so I'll go over the other three.
Tmux
While not an X selection, tmux has a copy mode accessible via prefix-[
(prefix
is Ctrl+B by default). The buffer used for this mode is separate and exclusive to tmux, which opens up quite a few possibilities and makes it more versatile than the X selections in the right situations.
To exit this mode, hit q; to navigate, use your vim
or emacs
binding (default = vim), so hjkl
for movement, v/V/C-v
for character/line/block selection, etc. When you have your selection, hit Enter to copy and exit the mode.
To paste from this buffer, use prefix-]
.
Shell
Any installation of X11
seems to come with two programs by default: xclip
and xsel
(kinda like how it also comes with both startx
and xinit
). Most of the other answers mention xclip
, and I really like xsel
for its brevity, so I'm going to cover xsel
.
From xsel(1x):
Input options
-a, --append
append standard input to the selection. Implies -i.
-f, --follow
append to selection as standard input grows. Implies -i.
-i, --input
read standard input into the selection.
Output options
-o, --output
write the selection to standard output.
Action options
-c, --clear
clear the selection. Overrides all input options.
-d, --delete
Request that the current selection be deleted. This not only clears the selection, but also requests to the program in which the selection resides that the selected contents be deleted. Overrides all input options.
Selection options
-p, --primary
operate on the PRIMARY selection (default).
-s, --secondary
operate on the SECONDARY selection.
-b, --clipboard
operate on the CLIPBOARD selection.
And that's about all you need to know. p
(or nothing) for PRIMARY
, s
for SECONDARY
, b
for CLIPBOARD
, o
for output.
Example: say I want to copy the output of foo
from a TTY and paste it to a webpage for a bug report. To do this, it would be ideal to copy to/from the TTY/X session. So the question becomes how do I access the clipboard from the TTY?
For this example, we'll assume the X session is on display :1
.
$ foo -v
Error: not a real TTY
details:
blah blah @ 0x0000000040abeaf4
blah blah @ 0x0000000040abeaf8
blah blah @ 0x0000000040abeafc
blah blah @ 0x0000000040abeb00
...
$ foo -v | DISPLAY=:1 xsel -b # copies it into clipboard of display :1
Then I can Ctrl-V
it into the form as per usual.
Now say that someone on the support site gives me a command to run to fix the problem. It's complicated and long.
$ DISPLAY=:1 xsel -bo
sudo foo --update --clear-cache --source-list="http://foo-software.com/repository/foo/debian/ubuntu/xenial/164914519191464/sources.txt"
$ $(DISPLAY=:1 xsel -bo)
Password for braden:
UPDATING %%%%%%%%%%%%%%%%%%%%%%% 100.00%
Clearing cache...
Fetching sources...
Reticulating splines...
Watering trees...
Climbing mountains...
Looking advanced...
Done.
$ foo
Thank you for your order. A pizza should arrive at your house in the next 20 minutes. Your total is $6.99
Pizza ordering seems like a productive use of the command line.
...moving on.
Vim
If compiled with +clipboard
(This is important! Check your vim --version
), Vim should have access to the X PRIMARY
and CLIPBOARD
selections. The two selections are accessible from the *
and +
registers, respectively, and may be written to and read from at your leisure the same as any other register. For example:
:%y+ ; copy/yank (y) everything (%) into the CLIPBOARD selection (+)
"+p ; select (") the CLIPBOARD selection (+) and paste/put it
ggVG"+y ; Alternative version of the first example
If your copy of vim doesn't directly support access to X selections, though, it's not the end of the world. You can just use the xsel
technique as described in the last section.
:r ! xsel -bo ; read (r) from the stdout of (!) `xsel -bo`
:w ! xsel -b ; write (w) to the stdin of (!) `xsel -b`
Bind a couple key combos and you should be good.
:%y+
in vim, which is vim-speak for "yank (copy) all the lines into the '+' register (the XPRIMARY
clipboard)". You can replace%
with a range if you want to be specific. But there's three caveats: 1. Now, you have to save whatever text to a file before you can copy it. This is in contrast to thexclip
command mentioned in the answers. 2. If you don't already know how to vim, this might be tedious. 3. You can only do this if a certain feature is enabled when compiling vim. If you install GVim, it should be enabled by default in both GUI and terminal instances of vim. – Braden Bestxclip
. Your answered worked like a charm. – HankCa:%y+
one but ok, you've got all bases covered! Good one. – HankCa