synthesizerpatel
synthesizerpatel

Reputation: 28036

Is it possible to erase or override zsh's prexec() error behavior?

I recently found out about preexec() and precmd() functions in zsh, and came up with a novel way to use squeeze a little more functionality out of my shell.

This prexec() function checks to see if the 'command' you have just attempted to run begins with 'http' and ends with '.git', if it does, it automatically will cd to my source directory and check the source out. So I just paste it in and hit enter and it's off to the races. Otherwise, any normal (or errors) should look normal.

function preexec() {
  [[ "$1" =~ "^http.*.git\$" ]] && cd /usr/local/src && git clone "$1"
}

The thing that's got me stymied right now is that there doesn't appear to be any way to ignore or overwrite the 'command could not be found' error that you get.

zsh: no such file or directory: https://github.com/someproject.git

I tried overwriting '1', ZLE_LINE_ABORTED, flipping around 2>&1, returning 1, 0, using noglob.. etc etc. no luck.

Any zsh smarties know how to suppress the error message?

Upvotes: 1

Views: 238

Answers (1)

synthesizerpatel
synthesizerpatel

Reputation: 28036

The correct answer is not to use the pre* functions and instead use the command_not_found_handler function. This should also work in newer versions of bash as well.

Credit goes to @chepner and @"Jan Krüger" for both of their efforts on getting this question resolved.

function command_not_found_handle {
        if [[ "$1" =~ "^(https|http|git|(git\\+)?ssh)://.*\$" ]]; then
                echo "Do something: $1"
                return 1
        else
                return 0
        fi
}

Upvotes: 2

Related Questions