Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: "Pull just the text of a single command" (was Re: .zsh_history)



[Bringing in other responses from the "was Re:" thread]

On Sun, Apr 16, 2023 at 7:53 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> On 2023-04-16 00:38, Roman Perepelitsa wrote:
> > On Sat, Apr 15, 2023 at 5:31 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> >> $ my_function $path $(eval 'ls *') one two three ! < > ``.."" &>^!
> >>
> >> my tail, exactly as typed, is: $path $(eval 'ls *') one two three ! < >
> >> ``.."" &>^!
> > What would this do?
> >
> >      % list=(my_function arg)
> >      % $list
> >
> > What I typed is `$list`, but what is "tail"?
> >
> > Roman.
>
> I don't understand.  You're assigning a variable no?

Right, you don't understand.  Roman's point is that

% $list

is going to run "my_function arg" as a command, so there's nothing in
"its tail as raw keystrokes" that corresponds to what "my_function"
will receive in its positional parameters.  The assignment is just
setting up the case of interest.

If Roman had written

% list=$(my_function arg)

then your follow-up would be closer to the mark, because then
my_function would actually run during the assignment instead of during
the expansion of $list.

> There's just that: "% echo one; echo two; echo> three" problem -- I'd like the middle 'echo' to know that it's tail is
> 'two' and nothing more or nothing less but of course recall from history
> gives all three commands in one serving.

It's much worse than that, consider

% my_function one && my_function $PATH || my_function three > filename

Or pipelines, or backgrounding, or if/then/else/fi, etc.  You will
need to define what you intend in each case (please, not by writing it
in prose here).

The ${(z)...} expansion does a best-case job of mimicking the parser,
so you can start by using ${(z)1} in preexec and work forward from
there.  The simple case with semicolons can be handled by

chain=( ${(s:; :)${(z)1}} )




Messages sorted by: Reverse Date, Date, Thread, Author