Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Global Aliases, but as a function?



On Mon, Dec 20, 2021 at 4:56 PM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
>
> Where can I read in the documentation about the preexec wrapper and
> specifically how to modify the command being executed?

Very little of this is just going to fall out of the documentation for you.

The preexec function (and the preexec_functions array) are described
in 9.3.1 Hook Functions, which is in "man zshmisc".  There is also doc
about the add-zsh-hook helper function in 26.2.5 Manipulating Hook
Functions (man zshcontrib).

You'd be looking at the third argument passed to preexec ("... the
full text that is being executed") which you will have to take apart
with something like cmdline=(${(z)3}) to figure out whether it's a
simple command and whether one of the arguments is "--help".

Assuming it is just one command and has that argument, you've got the
command name in $cmdline[1] (in my foregoing example) so you're going
to create function with that name that ends by deleting itself; e.g.
(naively again, ignoring the "is this a simple command" test):

preexec() {
  local cmdline=(${(z)3})
  if [[ -n $cmdline[(R)--help] ]]; then
    function $cmdline[1] {
     $cmdline[1] "$@" | $LESS
     unfunction $cmdline[1]
    }
  fi
}

And you're done.  This works as long as $cmdline[1] isn't already
defined as a different function, in which case you have to figure out
how to save and restore that.




Messages sorted by: Reverse Date, Date, Thread, Author