diff --git a/.config/fish/conf.d/smart_cd.fish b/.config/fish/conf.d/smart_cd.fish new file mode 100644 index 00000000..99a5d93f --- /dev/null +++ b/.config/fish/conf.d/smart_cd.fish @@ -0,0 +1,22 @@ +if not functions -q __cd_orig + functions --copy cd __cd_orig +end + +function cd --wraps=cd --description "Change directory. No args goes to git root or $HOME. If already there, return to previous location." + git rev-parse --is-inside-work-tree &>/dev/null + set -l is_git $status + if test (count $argv) -ne 0 + __cd_orig $argv + else + if test $is_git -eq 0 + set root (git rev-parse --show-toplevel) + else + set root ~ + end + if not test (pwd) = $root + __cd_orig $root + else + __cd_orig - + end + end +end diff --git a/.config/fish/config.fish b/.config/fish/config.fish index e8d7ce81..8046c311 100644 --- a/.config/fish/config.fish +++ b/.config/fish/config.fish @@ -1,6 +1,6 @@ if status is-interactive - source ~/.config/fish/custom_cd.fish + # source ~/.config/fish/custom_cd.fish starship init fish | source direnv hook fish | source diff --git a/.config/fish/custom_cd.fish b/.config/fish/custom_cd.fish deleted file mode 100644 index cf830a63..00000000 --- a/.config/fish/custom_cd.fish +++ /dev/null @@ -1,13 +0,0 @@ -if not functions -q __cd_orig - functions --copy cd __cd_orig -end - -function cd --wraps=cd --description "Change directory. No args goes to git root or $HOME." - git rev-parse --is-inside-work-tree &>/dev/null - set -l is_git $status - if test (count $argv) -ne 0; or not test $is_git -eq 0 - __cd_orig $argv - else - __cd_orig (git rev-parse --show-toplevel) - end -end