04 March 2013

xcl: X Clipboard Helper

As a pragmatic command-line user, I just found a new way to easily interact with the clipboard.

Yes, yes, I knew about 'xclip' before now -- but it was just way too hard to use because it made me type (and remember) lots of options for simple clipboard operations.

Introducing 'xcl', a simple, helpful wrapper around 'xclip':
#!/bin/bash
#
# While 'xclip' is helpful, it's way too verbose,
# and the defaults are unhelpful for common
# "Ctrl-C/V" type usages.
#
# This 'xcl' utility transforms 'xclip' into a
# helpful cmdline friend.
#
# usage:
# - cat some_file | xcl
# - xcl > some_file
#
# read from clipboard if stdin is a tty, otherwise write to clipboard
[ -t 0 ] && xclip -o -selection clipboard || xclip -i -selection clipboard
view raw xcl hosted with ❤ by GitHub

This uses a little-known trick from bash (really from the 'test' or '[' builtin), which allows me to detect whether a file descriptor is attached to a live terminal.

Hope this is helpful to you.

No comments:

Post a Comment