Manipulate X clipboard from terminal using xclip
utility which is a command line interface to X selections to easily copy and paste text from terminal to GUI applications.
Prerequisites
Install xclip
utility.
$ sudo apt-get install xclip
Usage
Copy command output to clipboard selection.
$ free -m | xclip -selection clipboard -in
Display clipboard selection.
$ xclip -selection clipboard -out
total used free shared buff/cache available Mem: 7.7G 3.6G 656M 382M 3.4G 3.4G Swap: 975M 682M 293M Total: 8.6G 4.3G 949M
Copy file contents to clipboard selection.
$ xclip -selection clipboard -in /etc/hostname
Display clipboard selection and store it to specified file.
$ xclip -selection clip -out | tee /tmp/myhostname
milosz-XPS-13-9343
Chain multiple xclip
commands to copy command output to primary and clipboard selections.
$ pwd | xclip -selection primary -in -f | xclip -selection clipboard -in
Display primary and clipboard selections.
$ xclip -selection clipboard -out
/home/milosz/
$ xclip -selection primary -out
/home/milosz/
Copy command output to clipboard selection and wait for exactly one request.
$ uptime | xclip -selection clipboard -in -loops 1
Display clipboard selection for the first time.
$ xclip -selection clip -out
20:53:47 up 66 days, 7:58, 17 users, load average: 0,73, 0,55, 0,48
Display clipboard selection for the second time.
$ xclip -selection clip -out
Error: target STRING not available
Inspect xclip
behavior.
$ uptime | xclip -selection clipboard -in -loops 2 -verbose
Loops: 2 Connected to X server. Using selection: XA_CLIPBOARD Using UTF8_STRING. Waiting for 2 selection requests, Control-C to quit Waiting for selection request 1 of 2. Waiting for selection request 2 of 2.
Additional notes
Read clipboard specification to learn about selections which are just different clipboards and Inter-Client Communication Conventions Manual for more details.
Install xsel
utility if you want something slightly different.