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

Re: Unexpected results

> On 22/08/2022 14:16 Perry Smith <pedz@xxxxxxxxxxxxxxxx> wrote:
> This was unexpected:
> % echo 'a/path with spaces/in/quotes'
> a/path/in/quotes
> % echo !:1:h
> quote>
> I was expecting the equivalent of
> % echo 'a/path/in'

The :h modifier on history is pretty brutal, yes.  We store the
full history word internally, but a "history word" is before the
lexical analyser has got to work, i.e. the raw expression with
quotes in it.  What you want is that word after it's been
processed internally.  Unfortunately that's a bit of a mouthful...

echo ${${(Q):-"!:1"}:h}

This takes the whole of that history word inside a parameter
expression (there's no parameter before the ":-" so zsh
uses the value after), strips the quotes, and applies the :h
flag to the result of that.

Someone may have a neater method.

You can get this as a ZLE widget; smart-insert-last-word has all
the bits to extract the word itself --- though it's way over the
top for the current purpose --- then all that's needed is the
substitution above just before inserting it on the line --- except
you'd want to re-quote it perhaps with a ${(q-)...} wrapper in
that case.

(Of course I understand the response is most likely to be "umph,
forget it"...)


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