Friday, April 5, 2013

Linux Clipboard Snippets


September 4th, 2009 By: Daniel

Whenever I find myself typing the same thing over and over again, I have to come up with some way to avoid the repetition. And one thing I do a lot is fill out web forms that require things like credit card numbers and unique email addresses. For a while I tried using Parcellite (a clipboard manager) but it wasn’t really designed for what I wanted to use it for. Finally with the inspiration of this thread I was able to cobble together a solution that does just what I want.

First I created a snippets directory and filled it with files that contained the text to insert or scripts to generate the desired text:
$ cd ~/.snippets
$ ls -1
credit_card
email
@example.com
phone
RAILS_ENV=test
sql_create
sql_permissions
$ cat credit_card 
4111111111111111
$ cat sql_permissions 
GRANT ALL PRIVILEGES ON .* TO ''@'localhost' IDENTIFIED BY ''
$ cat phone 
#!/usr/bin/env ruby
print (1..12).map {i i == 4  i == 8 ? '-' : rand(10).to_s}.join
Then I put this little script in ~/bin:
#!/bin/bash
# http://bbs.archlinux.org/viewtopic.php?id=71938
DIR=${HOME}/.snippets
DMENU_ARGS="-i -fn -*-terminal-*-*-*-*-18-*-*-*-*-*-*-*" # xfontsel to select font
 
FILE=`/bin/ls $DIR | /usr/bin/dmenu ${DMENU_ARGS}`
 
if [ -x ${DIR}/${FILE} ]; then #executable
echo -n `${DIR}/${FILE}` | xsel -b -i
elif [ -f ${DIR}/${FILE} ]; then
cat ${DIR}/${FILE} | xsel -b -i
fi
xdotool key ctrl+v # for non-console
xdotool key ctrl+shift+v # for console
And then I mapped that script to a handy key combination in compiz. So now whenever I need a test credit card number, phone number, or email address I can just do c, type the first few characters to select the right snippet, hit return and it gets pasted in to whatever I’m working on.
Notes and caveats:
  • I had to aptitude install some packages to get dmenu and xdotool to work.
  • dmenu is ugly and has no mouse support.
  • Pasting into vim has occasional issues.

No comments:

Post a Comment