Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active June 5, 2025 11:23
Show Gist options
  • Select an option

  • Save coderofsalvation/46549e3788ade2f3a938 to your computer and use it in GitHub Desktop.

Select an option

Save coderofsalvation/46549e3788ade2f3a938 to your computer and use it in GitHub Desktop.
modified version of snippy which features easier installation, follows symlinks, bashdown templates and automatic detection of cli vs window
#!/bin/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"
XSEL_ARGS="--clipboard --input"
# 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(){
IFS=''; cat - | while read line; do
[[ "$line" =~ '$' ]] && line="$(eval "echo \"$( echo "$line" | sed 's/"/\\"/g')\"")";
echo "$line"
done
}
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 . -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.
cat ${DIR}/${FILE} | bashdown > $TMPFILE
xsel ${XSEL_ARGS} < $TMPFILE
# Paste into the current application.
[[ -t 0 ]] && xdotool key ctrl+shift+v || xdotool key ctrl+v # cli or gui paste
fi
}
init && run
@pyriand3r
Copy link

Really usefull. I was looking for something like that for a long time.
Nevertheless there's a litte bug that caused some confusion for me. When you close the dmenu with esc to abort snippet insertion the script inserted itself. I fixed that behaviour adding

# If dmenu is closed with esc do nothing
  if [ "$?" = "1" ]; then
    exit
  fi

between line 72 and 73 ;)

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