Last active
January 7, 2020 22:13
-
-
Save nasamuffin/5cdf51a8d0a95586d95816956887f31f to your computer and use it in GitHub Desktop.
vimdef: find function/macro definitions and open them in vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Author: Emily Shaffer (2020) | |
# usage: vimdef <full-fn-name> [<git grep arg>...]" | |
# opens vim to the definiton of the provided function name using fuzzy regex magic | |
# Opens vim to a given file and line number. | |
# $1 = file | |
# $2 = line number | |
# $3+ discarded | |
function open_vim_to_loc { | |
vim +"$2" "$1" | |
} | |
if [[ -z "$@" ]]; then | |
echo "usage: vimdef <full-fn-name> [<git grep arg>...]" | |
exit | |
fi | |
# A definition has some types at the very front of the line, separated by one | |
# whitespace each, then the function name. | |
# It takes the form of <filename>:<line>:<matched-string> | |
DEF="$(git grep -E -e "^([a-z]+ )+$1\(" "${@:2}" -- ":!*.h*")" | |
# If there wasn't a match, this might be a macro. | |
if [[ -z "$DEF" ]]; then | |
DEF="$(git grep -E -e "^#define $1" "${@:2}")" | |
fi | |
if [[ -z "$DEF" ]]; then | |
git grep $1 | |
exit | |
fi | |
# Don't quote $() because we want it interpreted as separate args. | |
open_vim_to_loc $(echo $DEF | tr ':' ' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment