Science and technology

Don’t like IDEs? Try grepgitvi

Like most builders, I search and skim supply code all day lengthy. Personally, I’ve by no means gotten used to built-in growth environments (IDEs), and for years, I primarily used grep and duplicate/pasted file names to open Vi(m).

Eventually, I got here up with this script, slowly refining it as wanted.

Its dependencies are Vim and rlwrap, and it’s open supply beneath the Apache 2.Zero license. To use the script, put it in your PATH, and run it inside a listing of textual content information with:

grepgitvi <grep choices> <grep/vim search sample>

It will return a numbered checklist of search outcomes, immediate you for the variety of the outcome you need to use, and open Vim with that outcome. After you exit Vim, it’s going to present the checklist once more in a loop till you enter something aside from a outcome quantity. You may use the Up and Down arrow keys to pick a file; this makes it simpler (for me) to search out which ends up I’ve already checked out.

It’s easy and primitive in comparison with fashionable IDEs, and even to extra refined makes use of of Vim, however that is what does the job for me.

The script

#!/bin/bash

# grepgitvi - grep supply information, interactively open vim on outcomes
# Doesn't actually need to do a lot with git, aside from ignoring .git
#
# Copyright Yedidyah Bar David 2019
#
# SPDX-License-Identifier: Apache-2.Zero
#
# Requires vim and rlwrap
#
# Usage: grepgitvi <grep choices> <grep/vim sample>
#

TMPD=$(mktemp -d /tmp/grepgitvi.XXXXXX)
UNCOLORED=$TMPD/uncolored
COLORED=$TMPD/coloured

RLHIST=$TMPD/readline-history

[ -z "$" ] && DIRS=.

cleanup()
        rm -rf "$TMPD"

entice cleanup Zero

discover $ -iname .git -prune -o ! -iname "*.min.css*" -type f -print0 > $TMPD/allfiles

cat $TMPD/allfiles | xargs -Zero grep --color=all the time -n -H "$@" > $COLORED
cat $TMPD/allfiles | xargs -Zero grep -n -H "$@" > $UNCOLORED

max=`cat $UNCOLORED | wc -l`
pat="$"

inp=''
whereas true; do
        echo "============================ grep results ==============================="
        cat $COLORED | nl
        echo "============================ grep results ==============================="
        immediate="Enter a quantity between 1 and $max or the rest to stop: "
        inp=$(rlwrap -H $RLHIST bash -c "learn -p "$immediate" inp; echo $inp")
        if ! echo "$inp" | grep -q '^[0-9][0-9]*$' || [ "$inp" -gt "$max" ]; then
                break
        fi

        filename=$(cat $UNCOLORED | awk -F: "NR==$inp"' print $1')
        linenum=$(cat $UNCOLORED | awk -F: "NR==$inp"' ')
        vim +:"$linenum" +"norm zz" +/"$pat" "$filename"
executed

 

Most Popular

To Top