Skip to content

Instantly share code, notes, and snippets.

@torbiak
Last active April 9, 2016 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torbiak/7f4e50a304fd5ed1f45634950603257d to your computer and use it in GitHub Desktop.
Save torbiak/7f4e50a304fd5ed1f45634950603257d to your computer and use it in GitHub Desktop.
Copy passwords from a vim-encrypted file to the clipboard.
#!/usr/bin/env bash
IFS=$'\t\n'
set -eu
set -o pipefail
PASS_FILE=${PASS_FILE:-~/notes/numbers.md}
PASS_CLIP_TIME=${PASS_CLIP_TIME:-30}
not_found=2
no_password=3
function put {
case $(uname) in
CYGWIN*) putclip;;
Darwin*) pbcopy;;
*) xsel;;
esac
}
function get {
case $(uname) in
CYGWIN*) putclip;;
Darwin*) pbpaste;;
*) xsel;;
esac
}
# remove pairs with capitalized keys or where the key is `pass`.
function scrub {
sed -re 's/pass=[^,]*(,|$)//' -e 's/,$//' -e 's/(^|,)[A-Z][^=]*=[^,]*(,|$)//'
}
function clip {
local line=$(vimdecrypt $PASS_FILE | fgrep -m 1 -i "name=$1")
[[ -z "$line" ]] && exit $not_found
echo $line | scrub
if ! [[ $line =~ pass=([^,]*)(,|$) ]]; then
exit $no_password
fi
local pass=${BASH_REMATCH[1]}
echo $pass | put
(
sleep $PASS_CLIP_TIME
[[ "$(get)" == "$pass" ]] && echo "" | put
) &
}
function list {
vimdecrypt $PASS_FILE | fgrep -i "name=${1:-}" | scrub
}
cmd=${1:-}
case $cmd in
l*) list ${2:-};;
c*) clip ${2:?No pattern given};;
*) echo "\
pass list [PATTERN]
pass clip PATTERN
PATTERN is a prefix of an entry name."
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment