Skip to content

Instantly share code, notes, and snippets.

@avielsh
Last active December 24, 2019 11:37
Show Gist options
  • Save avielsh/ed72a2817daacf6fba5ab34a7421c8b7 to your computer and use it in GitHub Desktop.
Save avielsh/ed72a2817daacf6fba5ab34a7421c8b7 to your computer and use it in GitHub Desktop.
Simple searchable database a la locate - but much faster and using fzf for selection
#Add this to your .zshrc to search using gl [options] <Search query>
#gl takes a few optional arguments. By default, if gl is not being piped, after selection of file(s) you could cd to it's directory,
#edit with vim, copy to clipboard or open in finder.
#File extentions: aud(io), zip, doc, vid(eo), pic(tures). Searching for all videos: "gl vid". Searching for "matrix" in videos: "gl vid matrix"
#Starting search from current directory: "gl ."
#Search in home directory: "gl adm"
#Search in downloads directory: "gl dl"
#Search for a suffix: "gl -s <suffix>"
#Search for full filename: "gl -f <filename>"
#This script uses fd,rg and fzf, so make sure you have them.
#Inside fzf the following keys are enabled:
#alt-f - open file in finder, alt-s - list the file, alt-a - quick view the file, alt-enter - execute the file,
#ctrl-b - open in macvim, alt-d - select all, shift-down - page down, shift-up - page up
_choose_option() {
[[ -z "$1" ]] && return 0
cat <<EOF
$1
Choose from the following:
1. Cd to $1:h
2. Edit with vim (default)
3. Copy to clipboard
4. Open in Finder $1:h
EOF
printf "Please Choose: "
read ans
case $ans in
1)
fcd "$1"
;;
3)
print -n "$1" | pbcopy
echo Copied to clip
;;
4)
fcd "$1"
open .
;;
*)
FILES=(${(@f)1})
vim -p $FILES[@]
;;
esac
}
gl() {
[[ ! -t 1 ]] && SILENT=1
[[ $1 == "-f" ]] && shift && 1="/${1}$"
[[ $1 == "-s" ]] && shift && 1="\.${1}$"
1=${1:-.}
[[ $2 == "." ]] && 2="-q `pwd`"
aud="aac|ac3|aif|aifc|aiff|au|cda|dts|fla|flac|it|m1a|m2a|m3u|m4a|mid|midi|mka|mod|mp2|mp3|mpa|ogg|ra|rmi|spc|rmi|snd|umx|voc|wav|wma|xm"
zip="7z|ace|arj|bz2|cab|gz|gzip|r00|r01|r02|r03|r04|r05|r06|r07|r08|r09|r10|r11|r12|r13|r14|r15|r16|r17|r18|r19|r20|r21|r22|r23|r24|r25|r26|r27|r28|r29|rar|tar|tgz|z|zip"
doc="c|chm|cxx|doc|docm|docx|dot|dotm|dotx|pdf|potx|potm|ppam|ppsm|ppsx|pps|ppt|pptm|pptx|rtf|sldm|sldx|thmx|txt|vsd|wpd|wps|wri|xlam|xls|xlsb|xlsm|xlsx|xltm|xltx|xml"
vid="3g2|3gp|3gp2|3gpp|amr|asf|avi|flc|flv|m1v|m2ts|m2v|m4b|m4p|m4v|mkv|mp2v|mp4|mpe|mpeg|mpg|mpv2|mov|ogm|vob|vp6|wm|wmp|wmv"
pic="ani|bmp|gif|ico|jpe|jpeg|jpg|pcx|png|psd|tga|tif|tiff|wmf"
case $1 in
"aud"|"zip"|"doc"|"vid"|"pic") ARG="\.(${(P)1})$" ;;
".") ARG="`pwd`" ;;
"dl") ARG="$HOME/Downloads" ;;
"adm") ARG="$HOME/" ;;
*) ARG="$1" ;;
esac
shift
DBNAME=~/mydb
RES=$(\rg -Ni $ARG $DBNAME | ffzf $@)
[[ -z $SILENT ]] &&
_choose_option "$RES" ||
echo "$RES"
}
fcd() {
1=$(echo "$1" | tr -d "\n" | sed 's/>//'| gsed 's/^\s\+//' )
[[ -f "$1" ]] && cd "$1:h" && return ||
cd "$1"
}
alias fzf='fzf -m --cycle --tiebreak index \
--bind "alt-e:execute-silent(echo {+} >> ~/filelist),\
alt-f:execute-silent(~/finder.sh {+}),\
alt-s:execute(ls -lh {+}),\
alt-a:execute-silent(qlmanage -p {+} >& /dev/null),\
alt-enter:execute-silent(open {+}),\
ctrl-b:execute-silent(open -a /Applications/MacVim.app {+}),\
alt-d:select-all,shift-down:page-down,shift-up:page-up,ctrl-d:preview-page-down,ctrl-u:preview-page-up" \
--history ~/.fzf_history --preview ''[[ $(file --mime {}) =~ binary ]] &&
echo {} is a binary file ||
(pygmentize {} ||
highlight -O ansi -l {} ||
coderay {} ||
rougify {} ||
cat {}) 2> /dev/null | head -500'''
alias ffzf='fzf -e +s --no-preview'
#add this to your cron to update the database
3 0 * * * sudo ~/updb.sh >>~/tmp/cronjobs 2>&1
#!/bin/bash
osascript -e "tell application \"Finder\"" -e activate -e "reveal POSIX file \"$1\"" -e end tell
#!/usr/local/bin/bash -l
DBNAME=~/mydb
echo "$(date) - Starting database update - $(basename $0)"
export LC_ALL='C'
fd -E '/Volumes/*' -IH --color=never . / | gsort -rf > $DBNAME
unset LC_ALL
echo "$(date) - Finished database update - $(basename $0)"
#add this to your visudo, for the cron update job
<your username> ALL=(ALL) NOPASSWD: /Users/<your username>/updb.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment