From: Zach Riggle <zachriggle@gmail.com>
To: Daniel Shahaf <d.s@daniel.shahaf.name>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Global Aliases, but as a function?
Date: Mon, 20 Dec 2021 18:56:29 -0600 [thread overview]
Message-ID: <CAMP9c5nQS7nwx5LD5WjjSc4f6-jE9xUP+ZDvWPaTMtm=fqzQPA@mail.gmail.com> (raw)
In-Reply-To: <64ee9965-6517-481f-9fb5-683bd48cf220@www.fastmail.com>
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@daniel.shahaf.name> 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@gmail.com> 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
next prev parent reply other threads:[~2021-12-21 0:57 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-18 10:38 Zach Riggle
2021-12-18 22:40 ` Bart Schaefer
2021-12-20 18:42 ` Daniel Shahaf
2021-12-20 20:04 ` Bart Schaefer
2021-12-20 20:11 ` Roman Perepelitsa
2021-12-20 20:24 ` Bart Schaefer
2021-12-21 0:57 ` Zach Riggle
2021-12-21 0:56 ` Zach Riggle [this message]
2021-12-21 1:55 ` Bart Schaefer
2021-12-22 20:14 ` Daniel Shahaf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAMP9c5nQS7nwx5LD5WjjSc4f6-jE9xUP+ZDvWPaTMtm=fqzQPA@mail.gmail.com' \
--to=zachriggle@gmail.com \
--cc=d.s@daniel.shahaf.name \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).