Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Global Aliases, but as a function?
- X-seq: zsh-users 27427
- From: "Daniel Shahaf" <d.s@xxxxxxxxxxxxxxxxxx>
- To: "Zsh Users" <zsh-users@xxxxxxx>
- Cc: "Zach Riggle" <zachriggle@xxxxxxxxx>
- Subject: Re: Global Aliases, but as a function?
- Date: Mon, 20 Dec 2021 18:42:01 +0000
- Archived-at: <https://zsh.org/users/27427>
- In-reply-to: <CAH+w=7ZFOXk39PiinFqarip+_GfwZ090eVSoy_L1JEX1Wx-x6A@mail.gmail.com>
- List-id: <zsh-users.zsh.org>
- References: <CAMP9c5mpNVRiKMQmHBD9P+S3aUQXXHTYUeWKjU1joN9xcNJNCg@mail.gmail.com> <CAH+w=7ZFOXk39PiinFqarip+_GfwZ090eVSoy_L1JEX1Wx-x6A@mail.gmail.com>
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