Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a function to aid usage of xclip

  • Loading branch information...
commit bc20e0dc628af0f17e546ef1957803830528c66f 1 parent bd99811
Harry Garrood authored
Showing with 32 additions and 0 deletions.
  1. +32 −0 oh-my-zsh/lib/functions.zsh
32 oh-my-zsh/lib/functions.zsh
View
@@ -30,3 +30,35 @@ function extract () {
echo "\`$1' is not a valid file"
fi
}
+
+# a shortcut function to simplify usage of xclip
+clip() {
+ local _scs_col="\e[0;32m"; local _wrn_col='\e[1;31m'; local _trn_col='\e[0;33m'
+ # Check that xclip is installed.
+ if ! type xclip > /dev/null 2>&1; then
+ echo -e "$_wrn_col""You must have the 'xclip' program installed.\e[0m"
+ # Check user is not root (root doesn't have access to user xorg server)
+ elif [[ "$USER" == "root" ]]; then
+ echo -e "$_wrn_col""Must be regular user (not root) to copy a file to the clipboard.\e[0m"
+ else
+ # If no tty, data should be available on stdin
+ if ! [[ "$( tty )" == /dev/* ]]; then
+ input="$(< /dev/stdin)"
+ # Else, fetch input from params
+ else
+ input="$*"
+ fi
+ if [ -z "$input" ]; then # If no input, print usage message.
+ echo "Copies params or stdin to the clipboard."
+ echo "Usage: clip STRING"
+ echo " clip < FILE"
+ else
+ # Copy input to clipboard
+ echo -n "$input" | xclip -selection c
+ # Truncate text for status
+ if [ ${#input} -gt 80 ]; then input="$(echo $input | cut -c1-80)$_trn_col...\e[0m"; fi
+ # Print status.
+ echo -e "$_scs_col""Copied to clipboard:\e[0m $input"
+ fi
+ fi
+}
Please sign in to comment.
Something went wrong with that request. Please try again.