zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: "Pull just the text of a single command" (was Re: .zsh_history)
Date: Sat, 15 Apr 2023 12:47:37 -0700	[thread overview]
Message-ID: <CAH+w=7ZkpUQC8gjzoUrxg_mwnKP2xjkr=hGL+u_r-4tMy7buHA@mail.gmail.com> (raw)
In-Reply-To: <fad9a4fb-2507-05ac-db12-8f08bb68bd95@eastlink.ca>

On Sat, Apr 15, 2023 at 8:30 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> I'm 100% certain you can do that, but not with history expansion, you'll
> > need to use zle for that.
> I'd sure like to know how!  Back in the day nobody seemed to know how to
> do it, not even Bart.

I'm not sure which past discussion you're referencing, but it's
possible you're mixing your metaphors here.

"History expansion" is what happens when you use !! or !-2 or similar
references at the prompt.  That happens before pretty much anything
else as soon as you hit enter (or even sooner if you have setopt
magic-space or similar).  In this case you can use !# to refer
backwards to any words already entered:

% echo first !#:1 last !#:3
first first last last
% echo everything so far !# and more
everything so far echo everything so far and more

References to the stored history with "fc" and related commands like
"history" accesses events (don't think in terms of "lines" -- a
multi-line command like if/while/for is usually one event) that have
previously been executed.  This doesn't have access to the event
currently being executed, and those events are stored with history
expansions (like !# in the foregoing, etc.) already replaced.

ZLE has access to what you're entering as you are entering it and
before either of the above things (or any other expansions like
globbing etc.) happen.  So if you want the unaltered text as you work
with it, ZLE is where you get it.  The best place to grab it after
hitting enter is probably in the zle-line-finish hook, but that won't
always give you full multi-line events (e.g., when the PS2 prompt is
active, you get it a line at a time).

Finally, the preexec function (and hook) gives access to the full
event with only history expansions already done (in $1), before it is
entered into the "fc"-able history, along with the event with all the
other expansions done (in $3).  There's nowhere other than ZLE that
can give you the raw ! references, and preexec can't give you $1 in a
shell script where history isn't available at all.

What you can't do is defer expansions (other than globbing, via
noglob) until "inside" the execution of the command (e.g., your shell
function).  I have a vague recollection that this may relate to you
being accustomed to DOS/Windows where expansion of the arguments is
done by calling a library after the fact, as opposed to the shell
doing all of it first.

Anyway, everything you asked about is there, you just need to adjust
your thinking about the sequence of events to find the right place to
accomplish what you want.

Of course this has NOTHING to do with how SAVING the history is configured.


  reply	other threads:[~2023-04-15 19:48 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-08 16:16 .zsh_history Perry Smith
2023-04-08 16:53 ` .zsh_history Roman Perepelitsa
2023-04-08 17:23   ` .zsh_history Ray Andrews
2023-04-08 17:27     ` .zsh_history Roman Perepelitsa
2023-04-08 17:35       ` .zsh_history Perry Smith
2023-04-08 17:54       ` .zsh_history Ray Andrews
2023-04-14 14:36         ` .zsh_history Felipe Contreras
2023-04-14 15:27           ` .zsh_history Ray Andrews
2023-04-15  4:49             ` .zsh_history Felipe Contreras
2023-04-15 15:29               ` .zsh_history Ray Andrews
2023-04-15 19:47                 ` Bart Schaefer [this message]
2023-04-15 22:47                   ` "Pull just the text of a single command" (was Re: .zsh_history) Ray Andrews
2023-04-15 23:26                     ` Bart Schaefer
2023-04-16 15:53                       ` Bart Schaefer
2023-04-16 16:37                         ` Ray Andrews
2023-04-16 19:24                         ` Ray Andrews
2023-04-16  7:38                 ` .zsh_history Roman Perepelitsa
2023-04-16 14:53                   ` .zsh_history Ray Andrews
2023-04-16 15:28                     ` .zsh_history Bart Schaefer
2023-04-16 15:29                     ` .zsh_history Roman Perepelitsa
2023-04-14 14:28     ` .zsh_history Felipe Contreras
2023-04-14 15:18       ` .zsh_history Ray Andrews
2023-04-14 14:00   ` .zsh_history Felipe Contreras
2023-04-14 14:51 ` .zsh_history Felipe Contreras

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=7ZkpUQC8gjzoUrxg_mwnKP2xjkr=hGL+u_r-4tMy7buHA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=rayandrews@eastlink.ca \
    --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).