Skip to content

Instantly share code, notes, and snippets.

@mustpax
Created May 20, 2024 22:04
Show Gist options
  • Save mustpax/c43f907e42275d4408e1c564badb6757 to your computer and use it in GitHub Desktop.
Save mustpax/c43f907e42275d4408e1c564badb6757 to your computer and use it in GitHub Desktop.
Bash script to edit contents of your clipboard with $EDITOR on macOS
#!/bin/bash
set -o errexit # Exit on error
set -o nounset # Treat unset variables as an error
set -o pipefail # Consider errors in a pipeline
default_editor=${EDITOR:-vim}
temp_file_path=$(mktemp /tmp/clip_temp.XXXXXX)
trap "rm -f $temp_file_path" EXIT
pbpaste > $temp_file_path
file_md5_hash=$(md5 $temp_file_path)
$default_editor $temp_file_path
new_file_md5_hash=$(md5 $temp_file_path)
if [ "$file_md5_hash" != "$new_file_md5_hash" ]; then
echo "File has been modified. Updating clipboard with editor contents"
pbcopy < $temp_file_path
else
echo "No changes detected, ignoring output"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment