Skip to content

Instantly share code, notes, and snippets.

@graham
Last active December 6, 2015 13:33
Show Gist options
  • Save graham/c31d8f2b10be08ee2d90 to your computer and use it in GitHub Desktop.
Save graham/c31d8f2b10be08ee2d90 to your computer and use it in GitHub Desktop.
Code Search with Bash
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 | cut -c 1-$SCREEN_WIDTH'
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 \$1 \" \" \$3}'"
# 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 conflicts='ga_code_search "<<<<<<<<<"'
# Find anything below your CWD.
alias cs='ga_code_search'
@graham
Copy link
Author

graham commented Nov 4, 2015

alias todo='ga_code_search "TODO\(`whoami`\)"'
alias cs="ga_code_search"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment