From 36f79c415a1abaf2ef6ebdc18b6a4cd38b33ec08 Mon Sep 17 00:00:00 2001 From: Jens Nolte <git@queezle.net> Date: Sat, 4 Sep 2021 00:18:07 +0200 Subject: [PATCH] Add cd alias to cd to commands and files --- layers/zsh.nix | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/layers/zsh.nix b/layers/zsh.nix index 89c1f63..31ef9ec 100644 --- a/layers/zsh.nix +++ b/layers/zsh.nix @@ -279,5 +279,22 @@ in $filter "$file" | curl -F'file=@-' "$pastebin" } + + cd () { + if [ "$1" = - ] || [ -z "$1" ] || [ -d "$1" ]; then + # normal cd + builtin cd "$@" + elif command -v "$1" >/dev/null; then + # resolve commands and cd to the directory that contains them + local p="$(dirname "$(realpath "$(command -v "$1")")")" + # for nix store paths, go to derivation root instead of the bin directory + [[ "$p" =~ /nix/store/ ]] && p="$(cut -d "/" -f-4 <<< "$p")" + builtin cd "$p" + elif [ -e "$1" ]; then + builtin cd "$(dirname "$@")" + else + builtin cd "$@" + fi + } ''; } -- GitLab