Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Global Aliases, but as a function?
Where can I read in the documentation about the preexec wrapper and
specifically how to modify the command being executed?
Zach Riggle
On Mon, Dec 20, 2021 at 12:42 PM Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx> wrote:
>
> Bart Schaefer wrote on Sat, 18 Dec 2021 22:40 +00:00:
> > On Sat, Dec 18, 2021 at 2:38 AM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
> >>
> >> Is there any way to define something similar to a "global alias" but
> >> which can modify the original command?
> >>
> >> There's a lot of use-cases for this, but the one that I'm wondering
> >> about specifically is to pass anything with "--help" in the command
> >> line into a pager that does a best-effort colorization IFF the output
> >> is not already colored.
> >
> > The only way to approximate this is by hooking into the ZLE editor to
> > modify $BUFFER before passing it to the lexer/parser. (Aliases modify
> > the single substituted word, during lexical analysis.)
> >
>
> One could have preexec() install wrapper functions that shadow the
> command words of any simple command that has a «--help» argument.
>
> I'm not sure whether or not that's preferable to the zle approaches.
>
> Cheers,
>
> Daniel
>
> > There are a bunch of ways to do this, but mainly there are three
> > choices, any/all of which involve creating a custom widget:
> >
> > 1/ Explicitly invoke your widget with a key binding. This gives you
> > the most flexibility because you can edit the result or undo it. (In
> > this case, though, you might instead want to consider using the
> > run-help binding with a suitable function.)
> > 2/ Override the accept-line widget (or the keybindings for it) with
> > your custom widget (and then invoke the ".accept-line" widget at the
> > end). This may be tricky to get right, and to avoid having it
> > interfere with other plugins etc. Be prepared to handle $PREBUFFER
> > and other multi-line-input situations.
> > 3/ Add your widget to the zle-line-finish hooks. This is more likely
> > to play well with plugin managers and you don't need .accept-line, but
> > you still need to handle multi-line input.
> >
> > All of these (except run-help) end up putting the whole command
> > including the appended pipeline into the history. If you want to
> > avoid that, look at zshaddhistory hooks.
> >
> > Naive/incomplete example of #3:
> >
> > page-the-help() {
> > if [[ -z $PREBUFFER && $BUFFER = *--help* ]]
> > then RBUFFER+=" | less"
> > fi
> > }
> > zle -N page-the-help
> > add-zle-hook-widget zle-line-finish page-the-help
Messages sorted by:
Reverse Date,
Date,
Thread,
Author