From 56d9282a3b35940e11b8e121308455cfecc2fe8c Mon Sep 17 00:00:00 2001 From: EnigmaCurry Date: Tue, 12 Nov 2024 11:06:18 -0700 Subject: [PATCH] cd alias --- config/bash/completion.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/config/bash/completion.sh b/config/bash/completion.sh index cfd4d47..4ecd871 100644 --- a/config/bash/completion.sh +++ b/config/bash/completion.sh @@ -14,3 +14,42 @@ fi # https://github.com/casey/just command -v just >/dev/null && source <(just --completions bash) + +## project root `cd` aliases with tab completion: +create_cd_alias() { + local alias_name="$1" + local root_dir="$2" + # Define the function for changing directories + eval " + ${alias_name}() { + cd \"${root_dir}/\$1\" + } + " + # Define the completion function for the alias + local completion_func="_${alias_name}_completion" + eval " + ${completion_func}() { + local cur=\"\${COMP_WORDS[COMP_CWORD]}\" + + if [[ \"\$cur\" != */* ]]; then + # Top-level completion: only show first-level subdirectories with trailing slash + COMPREPLY=( \$(find \"${root_dir}/\" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sed 's|$|/|' | grep -i \"^\$cur\") ) + else + # Nested-level completion: complete within the specified subdirectory path + local parent_dir=\"${root_dir}/\${cur%/*}\" + local partial_subdir=\"\${cur##*/}\" + COMPREPLY=( \$(find \"\$parent_dir\" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | sed 's|$|/|' | grep -i \"^\$partial_subdir\") ) + + # Prepend the current path to each result to maintain the nested path structure + for i in \"\${!COMPREPLY[@]}\"; do + COMPREPLY[\$i]=\"\${cur%/*}/\${COMPREPLY[\$i]}\" + done + fi + } + " + eval "complete -o nospace -F ${completion_func} ${alias_name}" +} + +create_cd_alias cdd "${HOME}/git/vendor/enigmacurry/d.rymcg.tech" +create_cd_alias cdg "${HOME}/git/vendor/enigmacurry" +create_cd_alias cdv "${HOME}/git/vendor"