Skip to content

Instantly share code, notes, and snippets.

@tommyjtl
Last active July 2, 2026 05:14
Show Gist options
  • Select an option

  • Save tommyjtl/9c283d95c0c6691972f156139e2431d7 to your computer and use it in GitHub Desktop.

Select an option

Save tommyjtl/9c283d95c0c6691972f156139e2431d7 to your computer and use it in GitHub Desktop.
A bash function command that quickly takes me to certain directory.
enter() {
local config_dir="$HOME/.config/enter"
local config_file="$config_dir/enter.yml"
# Initialize configuration
if [ ! -d "$config_dir" ]; then
mkdir -p "$config_dir"
fi
if [ ! -f "$config_file" ]; then
local user_root=$(pwd )
echo "root: $HOME" > "$config_file"
fi
# List directories
if [ "$1" = "-l" ]; then
local max_len=0
while IFS= read -r line; do
local key="${line%%:*}"
local len=${#key}
(( len > max_len )) && max_len=$len
done < "$config_file"
while IFS= read -r line; do
local key="${line%%:*}"
local value="${line#*:}"
value="${value#"${value%%[![:space:]]*}"}"
printf "%-*s %s\n" "$max_len" "$key" "$value"
done < "$config_file"
return
fi
# Create quick command
if [ "$1" = "-c" ]; then
local short_name
if [ -n "${ZSH_VERSION:-}" ]; then
read "short_name?Enter a short name for this directory: "
else
read -p "Enter a short name for this directory: " short_name
fi
echo "$short_name: $(pwd)" >> "$config_file"
return
fi
# Edit the config file
if [ "$1" = "-e" ]; then
micro "$config_file"
return
fi
# Navigate to directory
if [ -n "$1" ]; then
# Grab everything after the first ":" (more robust than splitting on spaces)
local dir
dir=$(grep -E "^$1:" "$config_file" | head -n 1 | cut -d':' -f2-)
# Trim leading spaces
dir="${dir#"${dir%%[![:space:]]*}"}"
# Strip optional surrounding double quotes
dir="${dir%\"}"
dir="${dir#\"}"
if [ -n "$dir" ]; then
cd "$dir"
else
echo "Directory not found for: $1"
fi
return
fi
echo "Usage: enter [-l | -c | -e | <short_name>]"
}
if [ -n "${BASH_VERSION:-}" ]; then
_enter_completion() {
local cur opts config_file="$HOME/.config/enter/enter.yml"
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$(grep -o '^[^:]*' "$config_file" 2>/dev/null)
if [[ ${cur} == -* ]]; then
COMPREPLY=($(compgen -W "-l -c -e" -- ${cur}))
return 0
elif [[ ${COMP_CWORD} == 1 ]]; then
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}
complete -F _enter_completion enter
elif [ -n "${ZSH_VERSION:-}" ]; then
_enter() {
local -a completions
local config_file="$HOME/.config/enter/enter.yml"
if [[ $words[2] == -* ]]; then
completions=(-l -c -e)
elif [ -f "$config_file" ]; then
completions=(${(f)"$(grep -o '^[^:]*' "$config_file")"})
fi
compadd -a completions
}
_enter_register_completion() {
(( $+functions[compdef] )) || autoload -Uz compdef 2>/dev/null
if compdef _enter enter 2>/dev/null; then
precmd_functions=(${precmd_functions:#_enter_register_completion})
fi
}
if ! _enter_register_completion 2>/dev/null; then
precmd_functions+=(_enter_register_completion)
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment