Command Line to Clipboard

Date: February 22, 2008
Author: Gen2ly
url:http://linuxtidbits.wordpress.com/2008/02/22/command-line-to-clipboard/

HeaderIt’s really something to be learning Linux. The more I learn about Linux the more I learn it’s about manipulating letters and numbers. Well, this is more programming than anything but Linux is alot about that. Bash I’m discovering is great. I’m just getting into it and now have made things a good deal easier by learning how to copy and paste text to/from the xorg server clipboard from the terminal. Here’s a couple commands that can do it and a couple bash scripts that make it easier.

Xsel and xclip are command line programs that can redirect the contents of the xorg server clipboard. The xorg server has two clipboards: the common right-click ‘Copy’ or ‘Edit > Copy’ command, and one for the middle mouse click. For those that don’t know of it yet, the middle-click clipboard allows quick copy and pasting without having to enter a menu or using Ctrl + v. Anytime you select text on the xorg server there is a serperate register that records this text, then clicking the mouse button three (usually clicking down the scroll wheel) will paste the text. The xorg server defines the the middle-click clipboard as ‘primary’ and the right-click clipboard as ’secondary’. xclip

Xclip I prefer to xsel because I have found that xsel can have problems pasting to java apps.

You can use xclip in a variety of ways. For example it can be piped to:

echo "hi" | xclip -selection clipboard

This will copy to the standard clipboard. For abbreviation, you can use ‘c’ instead of ‘clipboard’. You can specify ‘primary’ or ‘p’ here too to copy to the third mouse button, but isn’t necessary as this is the default for xclip:

echo "hello" | xclip

To direct a file to xclip you use the ‘-in’ or ‘-out’ options:

xclip -in -selection c <filename>
xclip -out -selection c <filename>

Which will respectively put a file into the clipboard, and write to a file from the clipboard contents.

To make the process quicker, I’ve created a couple scripts to automate the tasks called ‘cp2clip’ and ‘clippaste’ and can be used like a standard command:

cp2clip <filename>
clippaste <filename>

cp2clip

#!/bin/bash
# cp2clip - copy to the clipboard the contents of a file

# Program name from it's filename
prog=${0##*/}

# Text color variables
bldblu='\e[1;34m'         # blue
bldred='\e[1;31m'         # red
bldwht='\e[1;37m'         # white
txtbld=$(tput bold)       # bold
txtund=$(tput sgr 0 1)    # underline
txtrst='\e[0m'            # text reset
info=${bldwht}*${txtrst}
pass=${bldblu}*${txtrst}
warn=${bldred}!${txtrst}

filename=$@

# Display usage if full argument isn't given
if [[ -z $filename ]]; then
    echo " $prog <filename> - copy a file to the clipboard"
    exit
fi

# Check that file exists
if [[ ! -f $filename ]]; then
  echo -e "$warn File ${txtund}$filename${txtrst} doesn't exist"
  exit
fi

# Check user is not root (root doesn't have access to user xorg server)
if [[ $(whoami) == root ]]; then
  echo -e "$warn Must be regular user to copy a file to the clipboard"
  exit
fi

# Copy file to clipboard, give feedback
xclip -in -selection c < "$filename"
echo -e "$pass ${txtund}"${filename##*/}"${txtrst} copied to clipboard"

clippaste

#!/bin/bash # clippaste - Paste contents of clipboard to file in terminal.

# Program name from it's filename prog=${0##*/}

# Text color variables bldblu='e[1;34m' # blue bldred='e[1;31m' # red bldwht='e[1;37m' # white txtbld=$(tput bold) # bold txtund=$(tput sgr 0 1) # underline txtrst='e[0m' # text reset info=${bldwht}*${txtrst} pass=${bldblu}*${txtrst} warn=${bldred}!${txtrst}

filename=$@ pasteinfo="clipboard contents"

# usage if argument isn't given if [[ -z $filename ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 109)

Unexpected indentation.
echo "clippaste <filename> - paste contents of context-menu clipboard to file" exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 111)

Block quote ends without a blank line; unexpected unindent.

fi

# check if file exists, prompt to append or override, else create new if [[ -f $filename ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 115)

Unexpected indentation.

echo -en "$warn File ${txtund}$filename${txtrst} already exists - (${txtbld}e${txtrst})xit, (${txtbld}a${txtrst})ppend, (${txtbld}o${txtrst})verwrite: " read edit case "$edit" in

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 118)

Unexpected indentation.
[aA] ) xclip -out -selection clipboard >> $filename
echo -e "$pass File ${txtund}$filename${txtrst} appended with clipboard contents" ;;
[oO] ) xclip -out -selection clipboard > $filename
echo -e "$pass File ${txtund}$filename${txtrst} overwrote with clipboard contents" ;;

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 124)

Definition list ends without a blank line; unexpected unindent.
  • ) exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 125)

Bullet list ends without a blank line; unexpected unindent.

esac; else xclip -out -selection clipboard >> $filename echo -e "$pass File ${txtund}"$filename"${txtrst} created with clipboard contents"

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 128)

Block quote ends without a blank line; unexpected unindent.

fi

xsel

To copy to the context-menu clipboard:

xsel --clipboard < /etc/fstab

To copy a text to the middle mouse button clipboard:

xsel < /etc/fstab

Xsel can be piped too:

echo "a-bit-of-text" | xsel -b cat /etc/make.conf | xsel -b

To output directly to the terminal:

xsel --clipboard

And to redirect and append to a file:

xsel --clipboard > Baada-Boom.txt xsel --clipboard >> ~/.Baada-Boom

cp2clip (xsel)

#!/bin/bash # cp2clip - copy to the clipboard the contents of a file

# Program name from it's filename prog=${0##*/}

# Text color variables bldblu='e[1;34m' # blue bldred='e[1;31m' # red bldwht='e[1;37m' # white txtbld=$(tput bold) # bold txtund=$(tput sgr 0 1) # underline txtrst='e[0m' # text reset info=${bldwht}*${txtrst} pass=${bldblu}*${txtrst} warn=${bldred}!${txtrst}

filename=$@

# Display usage if full argument isn't given if [[ -z $filename ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 177)

Unexpected indentation.
echo " $prog <filename> - copy a file to the clipboard" exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 179)

Block quote ends without a blank line; unexpected unindent.

fi

# Check that file exists if [[ ! -f $filename ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 183)

Unexpected indentation.
echo -e "$warn File ${txtund}$filename${txtrst} doesn't exist" exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 185)

Block quote ends without a blank line; unexpected unindent.

fi

# Check user is not root (root doesn't have access to user xorg server) if [[ $(whoami) == root ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 189)

Unexpected indentation.
echo -e "$warn Must be regular user to copy a file to the clipboard" exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 191)

Block quote ends without a blank line; unexpected unindent.

fi

# Copy file to clipboard, give feedback xsel --clipboard < "$filename" echo -e "$pass ${txtund}"${filename##*/}"${txtrst} copied to clipboard"

clippaste (xsel)

#!/bin/bash # clippaste - Paste contents of clipboard to file in terminal. # use 'xclip -out -selection primary' for middle click clipboard

# Program name from it's filename prog=${0##*/}

# Text color variables bldblu='e[1;34m' # blue bldred='e[1;31m' # red bldwht='e[1;37m' # white txtbld=$(tput bold) # bold txtund=$(tput sgr 0 1) # underline txtrst='e[0m' # text reset info=${bldwht}*${txtrst} pass=${bldblu}*${txtrst} warn=${bldred}!${txtrst}

filename=$@ pasteinfo="clipboard contents"

# usage if argument isn't given if [[ -z $filename ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 222)

Unexpected indentation.
echo "clippaste <filename> - paste contents of context-menu clipboard to file" exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 224)

Block quote ends without a blank line; unexpected unindent.

fi

# check if file exists, prompt to append or override, else create new if [[ -f $filename ]]; then

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 228)

Unexpected indentation.

echo -en "$warn File ${txtund}$filename${txtrst} already exists - (${txtbld}e${txtrst})xit, (${txtbld}a${txtrst})ppend, (${txtbld}o${txtrst})verwrite: " read edit case "$edit" in

System Message: ERROR/3 (data/command-line-to-clipboard.txt, line 231)

Unexpected indentation.
[aA] ) xsel --clipboard >> $filename
echo -e "$pass File ${txtund}$filename${txtrst} appended with clipboard contents" ;;
[oO] ) xsel --clipboard > $filename
echo -e "$pass File ${txtund}$filename${txtrst} overwrote with clipboard contents" ;;

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 237)

Definition list ends without a blank line; unexpected unindent.
  • ) exit

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 238)

Bullet list ends without a blank line; unexpected unindent.

esac; else xsel --clipboard >> $filename echo -e "$pass File ${txtund}"$filename"${txtrst} created with clipboard contents"

System Message: WARNING/2 (data/command-line-to-clipboard.txt, line 241)

Block quote ends without a blank line; unexpected unindent.

fi