Skip to content

Instantly share code, notes, and snippets.

@holmboe
Last active October 19, 2017 09:43
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 holmboe/f82dd3c4f8d743cd4cba4ea3dab26e41 to your computer and use it in GitHub Desktop.
Save holmboe/f82dd3c4f8d743cd4cba4ea3dab26e41 to your computer and use it in GitHub Desktop.
Search Spacemacs junk files
#!/usr/bin/env bash
# -*- coding: utf-8; mode: sh -*-
set -o nounset
set -o errexit
#? junkfind -- list Spacemacs junk files
#? Copyright (C) 2017 Henrik Holmboe
#? License CC0 1.0 Universal
#?
#? 2017-04-24: Initial version
##? Usage: junkfind [options] [PATTERN] [HITNO] [OPEN]
##?
##? --help Show help options.
##? --version Print program version.
##? PATTERN The pattern to search for.
##? HITNO Only output the specified hit number.
##? OPEN Open with OPEN if available, else $EDITOR.
if [[ ! $(type -P "docopts") ]]; then
echo "This script uses docopts, install it from https://github.com/docopt/docopts"
exit 1
fi
help=$(grep "^##?" "$0" | cut -c 5-)
version=$(grep "^#?" "$0" | cut -c 4-)
eval "$(docopts -h "$help" -V "$version" : "$@")"
JUNKPATH="${JUNKPATH:-$(readlink -f ~/junk)}"
if [[ -n "${OPEN}" ]]; then
if [[ -x "$(which ${OPEN})" ]]; then
${OPEN} $(find "$JUNKPATH" -type f -iname "*$PATTERN*" | sort | sed -n "${HITNO}p")
else
${EDITOR} $(find "$JUNKPATH" -type f -iname "*$PATTERN*" | sort | sed -n "${HITNO}p")
fi
else
find "$JUNKPATH" -type f -iname "*$PATTERN*" | sort | sed -n "${HITNO}p"
fi
exit ${PIPESTATUS[0]}
# vim: filetype=sh
#!/usr/bin/env bash
# -*- coding: utf-8; mode: sh -*-
set -o nounset
set -o errexit
#? junkgrep -- search Spacemacs junk files
#? Copyright (C) 2016 Henrik Holmboe
#? License CC0 1.0 Universal
#?
#? 2016-11-11: Initial version
#? 2017-04-06: Add HITNO to only print specified hit number
#? 1. Search, iterate, add HITNO e.g.: junkgrep food 3
#? 2. Execute to open in editor: xdg-open $(!!)
##? Usage: junkgrep [options] PATTERN [HITNO]
##?
##? --help Show help options.
##? --version Print program version.
##? PATTERN The pattern to search for.
##? HITNO Only output the specified hit number.
if [[ ! $(type -P "docopts") ]]; then
echo "This script uses docopts, install it from https://github.com/docopt/docopts"
exit 1
fi
help=$(grep "^##?" "$0" | cut -c 5-)
version=$(grep "^#?" "$0" | cut -c 4-)
eval "$(docopts -h "$help" -V "$version" : "$@")"
JUNKPATH="${JUNKPATH:-$(readlink -f ~/junk)}"
GREPOPTS="-l -r -i"
grep $GREPOPTS -- "$PATTERN" "$JUNKPATH" | sort -r | sed -n "${HITNO}p"
exit ${PIPESTATUS[0]}
# vim: filetype=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment