From 20aabdc1f308e4792e13561a0763078c8570e020 Mon Sep 17 00:00:00 2001 From: ache Date: Sat, 21 Mar 2026 12:23:32 +0100 Subject: feat: Create a better edit function --- functions/e.fish | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'functions') diff --git a/functions/e.fish b/functions/e.fish index 8a14051..fdf1b3f 100644 --- a/functions/e.fish +++ b/functions/e.fish @@ -1,9 +1,37 @@ function e --wraps "$EDITOR" --description "Edit the file and set the working dir to the best usefull location" - set -l gitRoot (git rev-parse --show-toplevel) + set argedit argv + set filePattern $argv[1] + set cwd (pwd) + + # Delete the pattern to search the file and keep the others options + set -e argedit argv[1] + + # It uses `head -n 1` is here to speed up and kill fd when a file is found + set firstFile (fd $filePattern --type file --full-path -1) + if test -z "$firstFile" + set firstFile (fd --full-path --hidden --type file $filePattern -1) + end + + if test -z "$firstFile" + echo No file found matching $filePattern 1>&2 + return 1 + else + echo Editing $firstFile + end + + set toEdit (path resolve $firstFile) + + set dirBase (path dirname $firstFile) + + set gitRoot (fish -c "cd $dirBase; git rev-parse --show-toplevel 2>/dev/null") + if test -n "$gitRoot" - cd $gitRoot + pushd "$gitRoot" else - cd (dirname "$argv[1]") + pushd (dirname "$argv[1]") end - $EDITOR "argv[1]" + + $EDITOR $toEdit $argedit + + popd end -- cgit v1.3-2-g11bf