Skip to content

Commit

Permalink
cd alias
Browse files Browse the repository at this point in the history
  • Loading branch information
EnigmaCurry committed Nov 12, 2024
1 parent 24a46fb commit 56d9282
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions config/bash/completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 56d9282

Please sign in to comment.