Skip to content

Instantly share code, notes, and snippets.

@rjames86
Created January 12, 2016 05:25
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 rjames86/1351952bdc55d206d939 to your computer and use it in GitHub Desktop.
Save rjames86/1351952bdc55d206d939 to your computer and use it in GitHub Desktop.
function ga_code_search() {
# alias todo='ga_code_search "TODO\(`whoami`\)"'
SCREEN_WIDTH=`stty size | awk '{print $2}'`
SCREEN_WIDTH=$((SCREEN_WIDTH-4))
# Given a spooky name so you can alias to whatever you want.
# (cs for codesearch)
# AG is WAY faster but requires a binary
# (try brew install the_silver_searcher)
AG_SEARCH='ag "$1" | sort -k1 | cat -n | cut -c 1-$SCREEN_WIDTH'
# egrep is installed everywhere and is the default.
GREP_SEARCH='egrep -nR "$1" * | sort -k1 | cat -n'
SEARCH=$AG_SEARCH
if [ $# -eq 0 ]; then
echo "Usage: ga_code_search <search> <index_to_edit>"
echo ""
echo "Examples:"
echo " ga_code_search TODO"
echo " ga_code_search TODO 1"
echo " ga_code_search \"TODO\\(graham\\)\""
echo " ga_code_search \"TODO\\(graham\\)\" 4"
echo ""
return
fi
if [ $# -eq 1 ]; then
# There are no command line argumnets.
eval $SEARCH
else
# arg one should be a line from the output of above.
LINE="$SEARCH | sed '$2q;d' | awk -F':' '{print +\$2 \" \" \$1}' | awk -F' ' '{print \$3 \":\" \$1}'"
# Modify with your editor here.
emacs `eval $LINE`
fi
}
# Find todo items that are assigned to me.
alias todo='ga_code_search "TODO\(`whoami`\)"'
# Find merge conflicts that need to be resolved.
alias conf='ga_code_search "<<<<<<<<<"'
# Find anything below your CWD.
alias ccs='ga_code_search'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment