Skip to content

Instantly share code, notes, and snippets.

@erichs
Last active January 31, 2017 21:13
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 erichs/6ec32a1e735b2e1791563e42e9174891 to your computer and use it in GitHub Desktop.
Save erichs/6ec32a1e735b2e1791563e42e9174891 to your computer and use it in GitHub Desktop.
Fuzzy find, open, edit, etc. from current dir with text preview -- source me!
wut () {
local needle="$*"
local out file key OIFS
OIFS=$IFS
IFS=$'\n' out=($(ag -il $needle | fzf --reverse \
--preview="ag --color -C 5 $needle {}" \
--prompt '(ctrl-{l,o,e,g} to link, open, edit, or browse in github)> ' \
--expect=ctrl-l,ctrl-o,ctrl-e,ctrl-g))
IFS=$OIFS
if [ -z "$out" ]; then
return
fi
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
case $key in
(ctrl-l) linkto $file
return ;;
(ctrl-o) open $file ;;
(ctrl-e) ${EDITOR:-vim} "$file" ;;
(ctrl-g) gh $file ;;
(*) ;;
esac
echo $file
echo -n $file | pbcopy
}
linkto () {
# given relative filepath, find corresponding github URL
# copy this to pastebuffer, and emit to stdout
local origin=$(git ls-remote --get-url | sed -Ee 's#https?://##')
local user=$(echo $origin | sed -Ee 's#^.*[/:]+(.*)/.*$#\1#')
local project=$(echo $origin | sed -Ee 's#^.*[/:]+.*/(.*).git$#\1#')
(
cd $(dirname $*)
local urlpath=$(find $(pwd) -name $(basename $*) -maxdepth 1 | sed -n -e "s/^.*${project}//p")
local url="https://github.com/${user}/${project}/blob/master${urlpath}"
echo -n $url | pb | cat -
)
}
gh () {
# open relative filepath in github
open `linkto $*`
}
pb () {
# 'clipboard DWIM tool'
# example 'pb # paste'
# example 'echo hi | pb # copy'
# example 'echo hi | pb | cat - # copy and paste'
# example 'pb </tmp/file # copy'
# example 'pb >/tmp/file # paste'
# example 'pb </tmp/file1 >/tmp/file2 # copy and paste'
if [[ -p /dev/stdin ]]
then
pbcopy
fi
if [[ ! -t 0 && ! -p /dev/stdin ]]
then
pbcopy
fi
if [[ -t 1 ]]
then
if [[ -t 0 ]]
then
pbpaste
fi
fi
if [[ -p /dev/stdout ]]
then
pbpaste
fi
if [[ ! -t 1 && ! -p /dev/stdout ]]
then
pbpaste
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment