Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@BelkaDev
Last active November 19, 2021 10:27
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 BelkaDev/d449eb0b62671457398ee9e325398122 to your computer and use it in GitHub Desktop.
Save BelkaDev/d449eb0b62671457398ee9e325398122 to your computer and use it in GitHub Desktop.
A script to paste text/ run commands into active window
#!/usr/bin/bash
############
# @Description Paste stdin to current window
# @Author https://github.com/BelkaDev
# @Requires xclip,xdotool
# @Usage ${0} <string>
# @Usage <cmd> | ${0}
# @Example ${0} "hi"
# @Example curl -s "cht.sh/$(xclip -o -sel)?T?Q" | ${0}
############
CONTEXT=$(ps -o stat= -p $PPID)
if [ -p /dev/stdin ]; then
while IFS= read line; do
STDOUT="$STDOUT${line}\n"
done
elif [ "$#" -gt 0 ]; then
STDOUT="$*"
fi
[[ -z $STDOUT ]] && exit 1
TMP="$(xclip -o -sel clip)"
printf "$STDOUT" | xclip -selection c
# Safe paste for shells
if [[ $CONTEXT == "Ss" || $CONTEXT == "Ss+" ]]; then
printf "$STDOUT"
exit 0
fi
#Append to EOF
#xdotool key --clearmodifiers 0xffe3+0xff57
xdotool key --clearmodifiers 0xffe1+0xff9e
#Restore previous clip
echo "$TMP" | xclip -selection c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment