From: Bart Schaefer <schaefer@brasslantern.com>
To: Zach Riggle <zachriggle@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Global Aliases, but as a function?
Date: Sat, 18 Dec 2021 14:40:12 -0800 [thread overview]
Message-ID: <CAH+w=7ZFOXk39PiinFqarip+_GfwZ090eVSoy_L1JEX1Wx-x6A@mail.gmail.com> (raw)
In-Reply-To: <CAMP9c5mpNVRiKMQmHBD9P+S3aUQXXHTYUeWKjU1joN9xcNJNCg@mail.gmail.com>
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.)
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-18 22:41 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 [this message]
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
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='CAH+w=7ZFOXk39PiinFqarip+_GfwZ090eVSoy_L1JEX1Wx-x6A@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=zachriggle@gmail.com \
--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).