From 76920ea53e9a87b34f9c5c27ae911daeabb6080b Mon Sep 17 00:00:00 2001 From: druckdev <63563978+druckdev@users.noreply.github.com> Date: Thu, 5 Nov 2020 14:42:33 +0100 Subject: [PATCH] zsh: Add cd wrapper for calls without arguments Add a cd wrapper that when called without arguments, moves into the root of the current repository instead of HOME. (Except when already there) --- .config/zsh/plugins/functions.zsh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.config/zsh/plugins/functions.zsh b/.config/zsh/plugins/functions.zsh index 7d80c7a..be1b9d3 100644 --- a/.config/zsh/plugins/functions.zsh +++ b/.config/zsh/plugins/functions.zsh @@ -323,3 +323,20 @@ mvln() { ln -s "${2:A}" "$1" fi } + +## cd wrapper that when called without arguments, moves into the root of the +## current repo instead of HOME. (Except when already there) +cd() { + if [[ $# -gt 0 ]]; then + builtin cd "$@" + return + fi + + local toplevel + toplevel="$(git rev-parse --show-toplevel 2>/dev/null)" + if (( $? )) || [[ $PWD = $toplevel ]]; then + builtin cd + else + builtin cd "$toplevel" + fi +}