dotfiles/.config/shell/bin/sced

25 lines
615 B
Bash
Executable file

#!/bin/sh
# sced (script edit) edit a script or function that's somewhere inside the shell configuration
# Usage: bined <script name>
# Depends: fd
set -eu
above_dir="$(dirname "$(realpath "$0")")"/..
# start in the directory above the script
cd "$above_dir"
# get the path to the file if there was an argument
if [ -n "$1" ]; then
file="$(fd --exact-depth 2 --max-results=1 -at f "$1")"
if [ -z "$file" ]; then
file="$(dirname "$(realpath "$0")")/../$1"
fi
else
# edit >this< script by default
file="$(realpath "$0")"
fi
# edit the file
eval "${EDITOR:-$(which nvim)} ${file:-$(realpath "$0")}"