commit 70214760c7f8f9bde7a8018165f2e9d402612afe (patch)
parent 2bc63fed38a467155ef21aece37dcd19cfbbee5b
Author: Alex Karle <alex@alexkarle.com>
Date: Sun, 9 Jan 2022 00:20:52 -0500
Add git-grep alias and git-grep-editor tools
This is a fun exploration of UNIX as an IDE. The problem: I want
to be able to quickly jump to occurrences as specified in git-grep.
The solution? I either need to embed git-grep in an IDE (vim), or
I need to write a tool that parses git-grep and knows HOW to open
an editor.
This patch adds such a tool, to stay true to the UNIX philosophy,
warts and all. The tool has two smaller bits--one to parse the
"file:number:desc" and launch the editor, and another to parse
git-grep.
Diffstat:
3 files changed, 55 insertions(+), 0 deletions(-)
diff --git a/.gitconfig b/.gitconfig
@@ -17,6 +17,7 @@
rs = reset
d = diff
c = commit
+ g = grep -n
[pull]
ff = only
diff --git a/bin/fned b/bin/fned
@@ -0,0 +1,30 @@
+#!/bin/sh
+# fned -- file:number editor
+# reads file:number:blah (grep-style) from stdin or ARGV and opens
+# the file in $EDITOR at that point (for vi/m). Other editors just
+# open the file & print the line
+#
+# example:
+#
+# fned "$(git grep -n 'pattern' | fzf)"
+#
+# see also: gge(1)
+#
+die() {
+ echo "$*" 1>&2
+ exit 1
+}
+[ -z "$1" ] && die "usage: fned 'file:line:desc'"
+
+fstr="$*"
+file=${fstr%%:*}
+nofile=${fstr##$file:}
+line=${nofile%%:*}
+
+case ${EDITOR:-vi} in
+ vi*) $EDITOR -c "$line" "$file" ;;
+ *)
+ echo "fned: $EDITOR doesn't support line jumps. Go to $line"
+ $EDITOR "$file"
+ ;;
+esac
diff --git a/bin/gge b/bin/gge
@@ -0,0 +1,24 @@
+#!/bin/sh
+# gge -- git grep -> editor
+#
+# small ease-of-use tool that combines git-grep and fned
+die() {
+ echo "$*" 1>&2
+ exit 1
+}
+
+# Reuse last pattern
+if [ -z "$1" ]; then
+ if [ ! -e /tmp/gge ]; then
+ die "No previous search, run with 'gge PATTERN'"
+ fi
+else
+ git grep -n "$@" > /tmp/gge
+fi
+
+[ ! -s /tmp/gge ] && die "no matches, exiting"
+
+choice=$(fzf < /tmp/gge)
+[ -z "$choice" ] && exit 1
+
+fned "$choice"