2 replaced http://superuser.com/ with https://superuser.com/
source | link

I have a solution in bash. You need a function that is exectued before every command. Since bash doesn't support that natively you have to work around that using the DEBUG trap.

See the bash's manual page part of the DEBUG trap:

If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

So the function could look like this (the first part is copied from that superuser answersuperuser answer):

preexec () {
  [ -n "$COMP_LINE" ] && return  # do nothing if completing
  [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
  local this_command=$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")
  # check if the command occures as an alias
  a=$(alias | grep -F "='""$this_command""'" | grep -oP '(?<=alias\ )[^=]+')
  # if yes, print a notice
  [ -n "$a" ] && echo -e "You can use an alias you have for that: \033[01;32m$a\033[00m"
}
trap 'preexec' DEBUG

When typing:

$ git checkout master

It prints:

You can use an alias you have for that: gcm

and then comtinues with the command.

I have a solution in bash. You need a function that is exectued before every command. Since bash doesn't support that natively you have to work around that using the DEBUG trap.

See the bash's manual page part of the DEBUG trap:

If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

So the function could look like this (the first part is copied from that superuser answer):

preexec () {
  [ -n "$COMP_LINE" ] && return  # do nothing if completing
  [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
  local this_command=$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")
  # check if the command occures as an alias
  a=$(alias | grep -F "='""$this_command""'" | grep -oP '(?<=alias\ )[^=]+')
  # if yes, print a notice
  [ -n "$a" ] && echo -e "You can use an alias you have for that: \033[01;32m$a\033[00m"
}
trap 'preexec' DEBUG

When typing:

$ git checkout master

It prints:

You can use an alias you have for that: gcm

and then comtinues with the command.

I have a solution in bash. You need a function that is exectued before every command. Since bash doesn't support that natively you have to work around that using the DEBUG trap.

See the bash's manual page part of the DEBUG trap:

If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

So the function could look like this (the first part is copied from that superuser answer):

preexec () {
  [ -n "$COMP_LINE" ] && return  # do nothing if completing
  [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
  local this_command=$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")
  # check if the command occures as an alias
  a=$(alias | grep -F "='""$this_command""'" | grep -oP '(?<=alias\ )[^=]+')
  # if yes, print a notice
  [ -n "$a" ] && echo -e "You can use an alias you have for that: \033[01;32m$a\033[00m"
}
trap 'preexec' DEBUG

When typing:

$ git checkout master

It prints:

You can use an alias you have for that: gcm

and then comtinues with the command.

1
source | link

I have a solution in bash. You need a function that is exectued before every command. Since bash doesn't support that natively you have to work around that using the DEBUG trap.

See the bash's manual page part of the DEBUG trap:

If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function

So the function could look like this (the first part is copied from that superuser answer):

preexec () {
  [ -n "$COMP_LINE" ] && return  # do nothing if completing
  [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
  local this_command=$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//")
  # check if the command occures as an alias
  a=$(alias | grep -F "='""$this_command""'" | grep -oP '(?<=alias\ )[^=]+')
  # if yes, print a notice
  [ -n "$a" ] && echo -e "You can use an alias you have for that: \033[01;32m$a\033[00m"
}
trap 'preexec' DEBUG

When typing:

$ git checkout master

It prints:

You can use an alias you have for that: gcm

and then comtinues with the command.