From 8cd490ea0a93896150bf5db004f7bd581959a418 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Sat, 23 Apr 2022 16:59:44 +0200 Subject: [PATCH] zsh: Move 'change directory into repo root' logic Add an alias that switches directories into the repository root, instead of having the normal `cd` command behave like that. Sadly this is not possible (AFAIK) with a git alias as that will always spawn a subshell. --- .config/zsh/zshrc.d/30-alias.zsh | 1 + .config/zsh/zshrc.d/40-functions.zsh | 29 +++++++++------------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/.config/zsh/zshrc.d/30-alias.zsh b/.config/zsh/zshrc.d/30-alias.zsh index e210241..14aa430 100644 --- a/.config/zsh/zshrc.d/30-alias.zsh +++ b/.config/zsh/zshrc.d/30-alias.zsh @@ -60,6 +60,7 @@ fi alias ga='git add' alias gap='git add -p' alias gc='git commit' + alias gcd='cd "$(git rev-parse --show-toplevel)"' alias gch='git checkout' alias gd='git diff' alias gds='git diff --staged' diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index 7d2faa4..08462cb 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -359,28 +359,17 @@ mvln() { return $reg } -## cd wrapper that when called without arguments, moves into the root of the -## current repo instead of HOME. (Except when already there) +## cd-wrapper that recognizes a trailing `ls` behind the path (When not properly +## pressing ) cd() { - if [[ $# -gt 0 ]]; then - # Call `ls` on paths that end on '/ls' and don't exist with the suffix. - # (When not properly pressing ) - if [[ ! -e ${@[-1]} && -e ${@[-1]%%/ls} ]]; then - builtin cd "${@[1,-2]}" "${@[-1]%%ls}" - pwd - ls - else - builtin cd "$@" - fi - return - fi - - local toplevel - toplevel="$(git rev-parse --show-toplevel 2>/dev/null)" - if (( $? )) || [[ $PWD = $toplevel ]]; then - builtin cd + # Call `ls` on paths that end on '/ls' and don't exist with the suffix. + # (When not properly pressing ) + if [[ ! -e ${@[-1]} && -e ${@[-1]%%/ls} ]]; then + builtin cd "${@[1,-2]}" "${@[-1]%%ls}" + pwd + ls else - builtin cd "$toplevel" + builtin cd "$@" fi }