fe - Search for a file and edit

#!/usr/bin/env bash
help_text="
NAME
    fe - Search for a file and edit in gvim or VSCode.

USAGE
    fe <search words>

OPTIONS
    -h|--help
        Show help text.

    -f|--fuzzy
        Run a fuzzy search - adds wildcards before and after each word.

    -g|--use-grep
        Run grep - search content of files instead of filename.

DESCRIPTION
    Search for a file and edit in gvim or VSCode.  A list of matching files will
    be shown with the option to select one for edit.

AUTHOR
    mjnurse.github.io - 2020
"
help_line="Search for a file and edit"
web_desc_line="Search for a file and edit"

try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage="$(echo Usage: ${tmp%%OPTIONS*})"
run_grep=n
fuzzy=n

if [[ "$1" == "" ]]; then
    echo "${usage}"
    echo "${try}"
    exit 1
fi

while [[ "$1" != "" ]]; do
    case $1 in 
        -h|--help) # arg: Show help text.
            echo "$help_text"
            exit
            ;;
        -f|--fuzzy) # arg: Run a fuzzy search - adds wildcards before and after each word.
            fuzzy=y
            ;;
        -g|--use-grep) # arg: Run grep - search content of files instead of filename.
            run_grep=y
            ;;
        ?*)
            break
            ;;
    esac 
    shift
done 

tmp=/tmp/e.tmp
tmp2=/tmp/e.tmp2
rm -f $tmp $tmp2
name="${*}"

# Try which

which "$name" | realpath >> $tmp 2>/dev/null

if [[ $fuzzy == y ]]; then
    name="*${name// /*}*"
    echo Searching for: "$name"
fi

if [[ $run_grep == y ]]; then
  grep -ril "${name}" /c/MJN/drive/github/* >> $tmp
  echo "--------------------------------------------------------------------------------" >> $tmp
fi

for loc in . "$MJNWINROOT/MJN" "$MJNWINROOT/Documents" ~/mjnurse ~/bin; do
    echo "Searching $loc"
    find "$loc" -iname "$name" -type f \
        -not -path '*/.*' -not -name '.*' \
        -exec realpath {} \; >> $tmp 2>/dev/null
done

sort -u $tmp | sed "/mjnurse\.github\.io/d" > $tmp2
mv -f $tmp2 $tmp

let c=1
while read line; do
    if [[ ! $line =~ .*index.md && ! $line =~ ./node_modules/.* ]]; then
        echo $c - $line
        let c=c+1
    fi
done < $tmp

if [[ $run_grep == n ]]; then
    echo
    echo "NOTE: No grep run.  Use -g to run a grep"
fi
echo
read -p "Enter Number (# or c# - MS Code, v# - Vim): " n

editor="code"
if [[ "${n:0:1}" == "v" ]]; then
    editor="gvim"
    n="${n:1}"
elif [[ "${n:0:1}" == "c" ]]; then
    n="${n:1}"
fi

let c=1
while read line; do
    if [[ $c == $n ]]; then
        $editor "$line"
    fi
    let c=c+1
done < $tmp