Skip to content

Instantly share code, notes, and snippets.

@felix11h
Forked from opennomad/snippy.sh
Created March 12, 2016 21:29
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 felix11h/07bd050431d8f96e76f0 to your computer and use it in GitHub Desktop.
Save felix11h/07bd050431d8f96e76f0 to your computer and use it in GitHub Desktop.
Various scripts
#!/usr/bin/env bash
# video demo at: http://www.youtube.com/watch?v=90xoathBYfk
# written by "mhwombat": https://bbs.archlinux.org/viewtopic.php?id=71938&p=2
# Based on "snippy" by "sessy"
# (https://bbs.archlinux.org/viewtopic.php?id=71938)
#
# You will also need "dmenu", "xsel" and "xdotool". Get them from your linux
# distro in the usual way.
#
# To use:
# 1. Create the directory ~/.snippy
#
# 2. Create a file in that directory for each snippet that you want.
# The filename will be used as a menu item, so you might want to
# omit the file extension when you name the file.
#
# TIP: If you have a lot of snippets, you can organise them into
# subdirectories under ~/.snippy.
#
# TIP: The contents of the file will be pasted asis, so if you
# don't want a newline at the end when the text is pasted, don't
# put one in the file.
#
# 3. Bind a convenient key combination to this script.
#
# TIP: If you're using XMonad, add something like this to xmonad.hs
# ((mod4Mask, xK_s), spawn "/path/to/snippy")
#
DIR=${HOME}/.snippy
APPS="xdotool xsel dmenu"
DMENU_ARGS="-b"
TMPFILE="/tmp/.snippy.tmp"; :>$TMPFILE
# if nothing happens, try "xdotool click 2", "xdotool key ctrl+v" or "xdotool key ctrl+shift+v"
GUIPASTE="xdotool key ctrl+v"
CLIPASTE="xdotool key ctrl+shift+v"
# smarty like template engine which executes inline bash in (bashdown) strings (replaces variables with values e.g.)
# @link http://github.com/coderofsalvation/bashdown
# @dependancies: sed cat
# @example: echo 'hi $NAME it is $(date)' | bashdown
# fetches a document and interprets bashsyntax in string (bashdown) templates
# @param string - string with bash(down) syntax (usually surrounded by ' quotes instead of ")
bashdown(){
while read line; do
line="$(eval "printf \"$( printf "%s" "$line" | sed 's/"/\\"/g')\"")";
echo -e "$line"
done < <(cat "${DIR}/${FILE}")
}
init(){
for APP in $APPS; do
which "$APP" &>/dev/null || {
read -p "install the following required utils? : $APPS (y/n)" reply
[[ "$reply" == "y" ]] && sudo apt-get install "${APPS}";
}
done
[[ ! -d "$DIR" ]] && {
echo -e "created $DIR\n";
mkdir "$DIR";
printf "hi it is \$(date)" > "$DIR/test";
}
return 0
}
run(){
cd "${DIR}"
# Use the filenames in the snippy directory as menu entries.
# Get the menu selection from the user.
FILE=$(find -L . -type f | grep -v '^\.$' | sed 's!\.\/!!' | /usr/bin/dmenu ${DMENU_ARGS})
if [ -f "${DIR}/${FILE}" ]; then
# Put the contents of the selected file into the paste buffer.
content="$(bashdown < "${DIR}/${FILE}")"
if [[ "${#content}" == 0 ]]; then
printf "%s" "${FILE}" > $TMPFILE
else
printf "%s" "$content" > $TMPFILE
fi
else
${FILE} &> $TMPFILE # execute as bashcommand
fi
xsel -b --input < $TMPFILE
# Paste into the current application.
if is_window; then
${GUIPASTE}
else
${CLIPASTE}
fi
}
is_window(){
name="$(xdotool getwindowname "$(xdotool getwindowfocus)" | tr '[:upper:]' '[:lower:]')"
[[ "$name" =~ term|tilda|@${HOSTNAME} ]] && return 1
return 0
}
init && run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment