From e931f20a7ec3ed28357d075c6cca6aaa7a405c9e Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Wed, 13 Jul 2022 17:49:45 +0200 Subject: [PATCH] zsh:funcs:mkcd: Support `-` and `--` arguments --- .config/zsh/zshrc.d/40-functions.zsh | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index 52d8b4f..222f435 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -28,18 +28,27 @@ mkcd () { # Remove flags and their arguments nargs="$#" for ((i = 0; i < nargs; i++)); do - if [[ ${1[1]} = '-' ]]; then + if [[ ${1[1]} != '-' || $1 = '-' ]]; then + # Skip all non-flags + set -- "${@:2}" "$1" + else # When `-m` is given, shift it's MODE argument as well ! [[ $1 =~ ^-([^-].*)?m$ ]] || { shift; i+=1 } + # Stop the loop on `--` + [[ $1 != '--' ]] || { shift; break } + shift - else - set -- "${@:2}" "$1" fi done # cd into the created directory if only one was specified [[ $# -eq 1 && -d $1 ]] || return 0 + + # append a slash to change into the new directory instead of back to the + # last visited one + [[ $1 != '-' ]] || 1='-/' + cd "$1" pwd }