diff --git a/README.md b/README.md index 53c4c62..fb58e7b 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,26 @@ or goto --list ``` +### Expand an alias + +To expand an alias to its value, use: +```bash +goto -x +``` +or +```bash +goto --expand +``` + +#### Example +```bash +goto -x last_release +``` +or +```bash +goto --expand last_release +``` + ### Cleanup To cleanup the aliases from directories that are no longer accessible in your filesystem, use: diff --git a/goto.sh b/goto.sh index 93c9e90..291fc5f 100644 --- a/goto.sh +++ b/goto.sh @@ -50,6 +50,9 @@ goto() -l|--list) _goto_list_aliases ;; + -x|--expand) # Expand an alias + _goto_expand_alias "$@" + ;; -h|--help) _goto_usage ;; @@ -83,6 +86,8 @@ OPTIONS: goto -u|--unregister -l, --list: lists aliases goto -l|--list + -x, --expand: expands an alias + goto -x|--expand -c, --cleanup: cleans up non existent directory aliases goto -c|--cleanup -h, --help: prints this help @@ -116,6 +121,25 @@ _goto_list_aliases() fi } +# Expands a registered alias. +_goto_expand_alias() +{ + if [ "$#" -ne "1" ]; then + _goto_error "usage: goto -x|--expand " + return + fi + + local resolved + + resolved=$(_goto_find_alias_directory "$1") + if [ -z "$resolved" ]; then + _goto_error "alias '$1' does not exist" + return + fi + + echo "$resolved" +} + # Lists duplicate directory aliases _goto_find_duplicate() { @@ -268,7 +292,7 @@ _complete_goto_commands() local IFS=$' \t\n' # shellcheck disable=SC2207 - COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -c --cleanup -v --version" -- "$1")) + COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -x --expand -c --cleanup -v --version" -- "$1")) } # Completes the goto function with the available aliases @@ -321,7 +345,10 @@ _complete_goto_bash() prev="${COMP_WORDS[1]}" if [[ $prev = "-u" ]] || [[ $prev = "--unregister" ]]; then - # prompt with aliases only if user tries to unregister one + # prompt with aliases if user tries to unregister one + _complete_goto_aliases "$cur" + elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then + # prompt with aliases if user tries to expand one _complete_goto_aliases "$cur" fi elif [ "$COMP_CWORD" -eq "3" ]; then