Skip to content

Instantly share code, notes, and snippets.

@erykpiast
Last active March 4, 2020 11:43
Show Gist options
  • Save erykpiast/208cad2b35ef24bd0aa7ec35410add57 to your computer and use it in GitHub Desktop.
Save erykpiast/208cad2b35ef24bd0aa7ec35410add57 to your computer and use it in GitHub Desktop.
Show aliases available for the last command
# I'm a lame, sorry
FIRST_RUN="true";
function show_aliases_available_for_the_last_command {
if [[ "$FIRST_RUN" == "true" ]]; then
FIRST_RUN="false";
return;
fi
local LAST_COMMAND=$(history | tail -1 | cut -d' ' -f2- | xargs);
local FOUND_ALIASES=$(alias | egrep "^[a-z]+='$LAST_COMMAND .*'$");
local FOUND_ALIASES_COUNT=$(echo -n "$FOUND_ALIASES" | grep -c '^');
local bold=$(tput bold);
local normal=$(tput sgr0);
if [[ "$FOUND_ALIASES_COUNT" == "0" ]]; then
return;
fi
echo "" 1>&2;
echo "${bold}$FOUND_ALIASES_COUNT${normal} aliases found for ${bold}${LAST_COMMAND}${normal}" 1>&2;
echo "$FOUND_ALIASES" | sed 's/^/ * /' 1>&2;
}
precmd_functions+=(show_aliases_available_for_the_last_command)

Show aliases available for the last command

Tiny bash script that helps with learning aliases available in your shell. You probably use only a few on a daily basis, but there are many of them!

The script shows a list of aliases containing the command you've just typed.

Example

$ ls 
bar.jpg baz.zip foo.txt

5 aliases found for ls
 * l='ls -CF'
 * la='ls -A'
 * ll='ls -alhF'
 * ls='ls -G'
 * lsa='ls -lah'
$ git status
?? qux.html

2 aliases found for git status
 * gsb='git status -sb'
 * gst='git status -s'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment