status is-interactive; or exit # necessary because we want to use fish's # cd wrapper function, _not_ the builtin cd! 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." # Skip history in subshells. if status --is-command-substitution builtin cd $argv return $status end 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 if set -q dirprev echo "prev: $dirprev[-1] root: $root" set -x smartdirprev $dirprev[-1] set -x smartroot $root if string match -q "$root*" "$dirprev[-1]" echo We out here __cd_orig - else set -l prompt "This would take you to $dirprev[-1], which is outside $root, are you sure? (y/n): " echo $prompt end end end end function smartcd_update --description "Update smartcd.fish with the latest from the upstream." curl https://git.sr.ht/~ficd/smartcd.fish/blob/main/smartcd.fish \ -o $fish_config/conf.d/smartcd.fish exec fish end