Skip to content

Instantly share code, notes, and snippets.

@ElectronicRU
Last active December 14, 2015 19:48
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectronicRU/5138928 to your computer and use it in GitHub Desktop.
Save ElectronicRU/5138928 to your computer and use it in GitHub Desktop.
dmenu script for providing unlimited history sorted by number of uses/most recent use.
#!/bin/sh
if [ $# -lt 1 ] ; then
echo 1>&2 "usage: dmenu_history hist_file dmenu_opts..."
exit 1
fi
f="$1"
tf=`mktemp`
if ! [ -e "$f" ]; then touch "$f"; fi
shift
( sort -r -k 1,1 -n -s "$f" | cut -d' ' -f2- ; cat - ) | dmenu "$@" |
if read x; then
cat "$f" | {
i=1
while read n y; do
if [ "$y" = "$x" ]; then
i=`expr "$n" + 1`
else
echo "$n $y"
fi
done
exit $i
} > "$tf"
i=$?
echo "$i $x" > "$f"
cat "$tf" >> "$f"
rm "$tf"
echo "$x"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment