diff options
| author | ache <ache@ache.one> | 2026-03-21 12:23:32 +0100 |
|---|---|---|
| committer | ache <ache@ache.one> | 2026-03-21 12:23:32 +0100 |
| commit | 20aabdc1f308e4792e13561a0763078c8570e020 (patch) | |
| tree | f473ff2e49a6deda20e5f9ecc55d93eeb0e5b775 /functions/e.fish | |
| parent | fix: kitty detection in ssh (diff) | |
feat: Create a better edit function
Diffstat (limited to 'functions/e.fish')
| -rw-r--r-- | functions/e.fish | 36 |
1 files changed, 32 insertions, 4 deletions
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 |