function e --wraps "$EDITOR" --description "Edit the file and set the working dir to the best usefull location" 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" pushd "$gitRoot" else pushd (dirname "$argv[1]") end $EDITOR $toEdit $argedit popd end