zsh-users
 help / color / mirror / Atom feed
* Silly question on :h and = expansion
@ 2021-08-17 17:39 Zach Riggle
  2021-08-17 17:59 ` Bart Schaefer
  2021-08-17 21:31 ` Paul
  0 siblings, 2 replies; 10+ messages in thread
From: Zach Riggle @ 2021-08-17 17:39 UTC (permalink / raw)
  To: Zsh Users

Quick question, the answer is probably "just use dirname".

I really like the flexibilty of Zsh expansion, particularly "=foo" to
effectively expand to the full path, and as best I know it's
functionally equivalent to "${commands[foo]}".

I also like the ":h" modifier that's equivalent to "dirname" (and all
of its siblings, but that's the only relevant one for this post).

I would like to simplify the process of "print the directory
containing" or "cd to directory containing" some file in $PATH.

I know I can do either of

* $(basename =foo)
* ${commands[foo]:h}

But I was hoping there was a way to combine the "=" and ":h" expansion
into one line.  As best I can tell, there isn't -- but I figured it
was worth asking.

Zach Riggle


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 17:39 Silly question on :h and = expansion Zach Riggle
@ 2021-08-17 17:59 ` Bart Schaefer
  2021-08-17 21:35   ` Roman Neuhauser
  2021-08-17 21:31 ` Paul
  1 sibling, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2021-08-17 17:59 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Tue, Aug 17, 2021 at 10:39 AM Zach Riggle <zachriggle@gmail.com> wrote:
>
> I was hoping there was a way to combine the "=" and ":h" expansion

cd ${$(<<<=foo):h}


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 17:39 Silly question on :h and = expansion Zach Riggle
  2021-08-17 17:59 ` Bart Schaefer
@ 2021-08-17 21:31 ` Paul
  2021-08-17 21:39   ` Roman Neuhauser
  1 sibling, 1 reply; 10+ messages in thread
From: Paul @ 2021-08-17 21:31 UTC (permalink / raw)
  To: Zach Riggle, Zsh Users

On Tue Aug 17, 2021 at 12:39 PM CDT, Zach Riggle wrote:
> Quick question, the answer is probably "just use dirname".
>
> But I was hoping there was a way to combine the "=" and ":h" expansion
> into one line. As best I can tell, there isn't -- but I figured it
> was worth asking.

In fact there is!  Modifiers can also be used as globbing flags:

    cd =foo(:h)



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 17:59 ` Bart Schaefer
@ 2021-08-17 21:35   ` Roman Neuhauser
  2021-08-17 21:49     ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Roman Neuhauser @ 2021-08-17 21:35 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

# schaefer@brasslantern.com / 2021-08-17 10:59:19 -0700:
> On Tue, Aug 17, 2021 at 10:39 AM Zach Riggle <zachriggle@gmail.com> wrote:
> >
> > I was hoping there was a way to combine the "=" and ":h" expansion
> 
> cd ${$(<<<=foo):h}

also

* cd ${${:-=dram}:h}
* cd ${${:-dram}:c:h}

the first one is one character shorter than Bart's version. ;)

btw how long has :c been there?  i only noticed it a few weeks ago.

-- 
roman


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 21:31 ` Paul
@ 2021-08-17 21:39   ` Roman Neuhauser
  0 siblings, 0 replies; 10+ messages in thread
From: Roman Neuhauser @ 2021-08-17 21:39 UTC (permalink / raw)
  To: Paul; +Cc: Zach Riggle, Zsh Users

# GammaFunction@vivaldi.net / 2021-08-17 16:31:42 -0500:
> On Tue Aug 17, 2021 at 12:39 PM CDT, Zach Riggle wrote:
> > But I was hoping there was a way to combine the "=" and ":h" expansion
> > into one line. As best I can tell, there isn't -- but I figured it
> > was worth asking.
> 
> In fact there is!  Modifiers can also be used as globbing flags:
> 
>     cd =foo(:h)

Perl: There Is More Than One Way to Do It,
Zsh: There's Always One More Way to Do It.

also, doh!

-- 
roman


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 21:35   ` Roman Neuhauser
@ 2021-08-17 21:49     ` Bart Schaefer
  2021-08-17 23:27       ` Zach Riggle
  0 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2021-08-17 21:49 UTC (permalink / raw)
  To: Roman Neuhauser; +Cc: Zach Riggle, Zsh Users

On Tue, Aug 17, 2021 at 2:35 PM Roman Neuhauser <neuhauser@sigpipe.cz> wrote:
>
> btw how long has :c been there?  i only noticed it a few weeks ago.

2009-03-23 it looks like.

Sadly (?) although you can write foo(:c:h) without getting "illegal
modifier" or "modifier failed", "foo" has to be a file in the current
directory before :c can tell you whether it's also somewhere in the
command path.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 21:49     ` Bart Schaefer
@ 2021-08-17 23:27       ` Zach Riggle
  2021-08-18  0:12         ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Zach Riggle @ 2021-08-17 23:27 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Roman Neuhauser, Zsh Users

Awesome!!! These are all wonderful.  "=foo(:h)" is the most concise
way I see, and fits with my style.

Thank you so much guys, and Paul for showing me a new trick.

Zach Riggle

On Tue, Aug 17, 2021 at 4:50 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Tue, Aug 17, 2021 at 2:35 PM Roman Neuhauser <neuhauser@sigpipe.cz> wrote:
> >
> > btw how long has :c been there?  i only noticed it a few weeks ago.
>
> 2009-03-23 it looks like.
>
> Sadly (?) although you can write foo(:c:h) without getting "illegal
> modifier" or "modifier failed", "foo" has to be a file in the current
> directory before :c can tell you whether it's also somewhere in the
> command path.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-17 23:27       ` Zach Riggle
@ 2021-08-18  0:12         ` Bart Schaefer
  2021-08-18  2:55           ` Mikael Magnusson
  0 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2021-08-18  0:12 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Roman Neuhauser, Zsh Users

On Tue, Aug 17, 2021 at 4:27 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> Awesome!!! These are all wonderful.  "=foo(:h)" is the most concise
> way I see, and fits with my style.

It might be worth noting:  That formulation only works in contexts
where globbing is done, so not e.g. in conditionals.

% [[ -f =zsh ]] && echo yes
yes
% [[ -d =zsh(:h) ]] && echo yes || echo no
no
% [[ -d ${${:-=zsh}:h} ]] && echo yes || echo no
yes


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-18  0:12         ` Bart Schaefer
@ 2021-08-18  2:55           ` Mikael Magnusson
  2021-08-18  3:25             ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Mikael Magnusson @ 2021-08-18  2:55 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zach Riggle, Roman Neuhauser, Zsh Users

On 8/18/21, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Tue, Aug 17, 2021 at 4:27 PM Zach Riggle <zachriggle@gmail.com> wrote:
>>
>> Awesome!!! These are all wonderful.  "=foo(:h)" is the most concise
>> way I see, and fits with my style.
>
> It might be worth noting:  That formulation only works in contexts
> where globbing is done, so not e.g. in conditionals.
>
> % [[ -f =zsh ]] && echo yes
> yes
> % [[ -d =zsh(:h) ]] && echo yes || echo no
> no
> % [[ -d ${${:-=zsh}:h} ]] && echo yes || echo no
> yes

You can use this form to enable globbing in conditionals,
% [[ -d =zsh(#q:h) ]] && echo yes || echo no
yes

but using = in conditionals is not very useful anyway since when it
fails it aborts, rather than returning an error (and (N) doesn't
suppress it since it's not a glob),
% [[ -f =bloo ]] || echo no
zsh: bloo not found!

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Silly question on :h and = expansion
  2021-08-18  2:55           ` Mikael Magnusson
@ 2021-08-18  3:25             ` Bart Schaefer
  0 siblings, 0 replies; 10+ messages in thread
From: Bart Schaefer @ 2021-08-18  3:25 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zach Riggle, Roman Neuhauser, Zsh Users

On Tue, Aug 17, 2021 at 7:55 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> You can use this form to enable globbing in conditionals,
> % [[ -d =zsh(#q:h) ]] && echo yes || echo no
> yes

Requires extendedglob.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-08-18  3:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-17 17:39 Silly question on :h and = expansion Zach Riggle
2021-08-17 17:59 ` Bart Schaefer
2021-08-17 21:35   ` Roman Neuhauser
2021-08-17 21:49     ` Bart Schaefer
2021-08-17 23:27       ` Zach Riggle
2021-08-18  0:12         ` Bart Schaefer
2021-08-18  2:55           ` Mikael Magnusson
2021-08-18  3:25             ` Bart Schaefer
2021-08-17 21:31 ` Paul
2021-08-17 21:39   ` Roman Neuhauser

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ https://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git