Bash – Falling to git checkout on non-found bash commands

bashhook

When I type (in bash) a command which linux does not find, it usually does something like this:

$ x
x: command not found

but when I type a command which is not found but which is similar in some sense to other possible commands, the reply may look like this:

$ pyp
No command 'pyp' found, did you mean:
 Command 'pcp' from package 'pcp' (universe)
 Command 'pype' from package 'pype' (universe)
 Command 'pgp' from package 'pgpgpg' (universe)
 Command 'pp' from package 'libpar-packer-perl' (universe)
 Command 'php' from package 'php5-cli' (main)
 Command 'gyp' from package 'gyp' (universe)
 Command 'pip' from package 'python-pip' (universe)
 Command 'pap' from package 'netatalk' (universe)
pyp: command not found

That means that some hook checked pyp before it let bash reply with pyp: command not found.

I'd like to write a hook like that, that will check if the "command" is a name of a branch in a current repository (if we're indeed in a repository), and will try to checkout into it before replying with command not found, but only if the command was really not found.

To this end, I need to understand something about the flow; when I type the command (and press enter), where does it go to? What is the program which replies with "did you mean", and how does it get the command string from bash?

Best Answer

You have to replace / change the command_not_found_handle shell function:

type command_not_found_handle
Related Question