Skip to content

Instantly share code, notes, and snippets.

@justinline
Last active November 7, 2024 00:06
Show Gist options
  • Save justinline/e11ac0f08f267502b9e96362457d03c6 to your computer and use it in GitHub Desktop.
Save justinline/e11ac0f08f267502b9e96362457d03c6 to your computer and use it in GitHub Desktop.
1password command line clipboard copying
# Short alias to allow automated copying of the 1password cli tool command 'op get somewebsite | jq ...' and then a timed clear on the gnome clipboard
# TODO: specify designation as an argument and maybe a command to open the website in chrome
pass=$(op get item $1 | jq -r '.details.fields[] | select(.designation=="password").value')
if [ -n "$pass" ]
then
echo $pass | xclip -selection clipboard
echo "Password was copied - clipboard will be wiped in 15 seconds"
sleep 15
echo "" | xclip -selection clipboard
echo "Clipboard reset."
fi
@kercdbh
Copy link

kercdbh commented Jul 28, 2023

Tested on a MacBook Pro M1 Ventura 13.5, this works:

pass=$(op item get $1 --format json | jq -r '.fields[] | select(.id=="password").value')

if [ -n "$pass" ]
    then 
         echo $pass | pbcopy
         echo "Password was copied - clipboard will be wiped in 15 seconds"
         sleep 15
         echo "" | pbcopy
         echo "Clipboard reset."
fi

@j1mmie
Copy link

j1mmie commented Nov 7, 2024

I made a version that works for Mac and:

  1. Restores the clipboard contents to what it was before running the command
  2. Removes the newline that echo was adding to the clipboard
  3. Is a function that can be pasted into .zshrc

pw() {
  pass=$(op item get $1 --format json | jq -r '.fields[] | select(.id=="password").value')

  if [ -z "$pass" ]; then
    echo "Password not found"
    return
  fi

  old_clip=$(pbpaste)

  echo -n $pass | pbcopy
  echo "Password was copied - clipboard will be restored in 15 seconds"
  sleep 15
  echo -n $old_clip | pbcopy
  echo "Clipboard reset."
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment