Last active
December 13, 2024 01:39
-
-
Save tommyjtl/9c283d95c0c6691972f156139e2431d7 to your computer and use it in GitHub Desktop.
A bash function command that quickly takes me to certain directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
cat "$config_file" | |
return | |
fi | |
# Create quick command | |
if [ "$1" == "-c" ]; then | |
read -p "Enter a short name for this directory: " short_name | |
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 | |
local dir=$(grep "^$1:" "$config_file" | cut -d ' ' -f 2-) | |
if [ -n "$dir" ]; then | |
cd "$dir" | |
else | |
echo "Directory not found for: $1" | |
fi | |
return | |
fi | |
echo "Usage: enter [-l | -c | -e | <short_name>]" | |
} | |
_enter_completion() { | |
local cur opts | |
COMPREPLY=() | |
cur="${COMP_WORDS[COMP_CWORD]}" | |
opts=$(grep -o '^[^:]*' ~/.config/enter/enter.yml) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment