zsh-users
 help / color / mirror / code / Atom feed
* Re: Get the result of the last glob
       [not found] <918ED85F-9DE5-4365-B962-45AD4AADDF46__5329.09398186661$1566393766$gmane$org@icloud.com>
@ 2019-08-21 15:17 ` Stephane Chazelas
  2019-08-21 15:43   ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Stephane Chazelas @ 2019-08-21 15:17 UTC (permalink / raw)
  To: Aryn Starr; +Cc: zsh-users

2019-08-21 17:51:11 +0430, Aryn Starr:
> Is there a way to get the result of the last glob?
> I want to do sth like this in a folder with only one file, somebook.mobi:
> 
> ebook-convert * *.epub
> 
> Which I want expanded to:
> 
> ebook-convert somebook.mobi somebook.mobi.epub
[...]

You could use zargs for that:

autoload -Uz zargs # in ~/.zshrc

zargs -I@ ./* -- ebook-convert @ @.epub

Though I generally find that using a loop is generally quicker,
shorter and safer (and you have more control over what's going
on):

(for f (./*) ebook-convert $f $f.epub)

(using a subshell to get a more deterministic behaviour upon
^C/^Z).

Here, assuming that "ebook-convert" supports the --
end-of-option delimiter (if it doesn't replace -p with -P and *
with ./*), you could also do:

autoload -Uz zmv # in ~/.zshrc

zmv -p ebook-convert '*' '$f.epub'

Or 

alias wzmv='noglob zmv -W' # in ~/.zshrc

wzmv -p ebook-convert * *.epub

zmv does some sanity checks which you could find useful here,
like that none of the destination files exist before starting
the moves.

-- 
Stephant

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

* Re: Get the result of the last glob
  2019-08-21 15:17 ` Get the result of the last glob Stephane Chazelas
@ 2019-08-21 15:43   ` Bart Schaefer
  0 siblings, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2019-08-21 15:43 UTC (permalink / raw)
  To: Zsh Users

On Wed, Aug 21, 2019 at 8:18 AM Stephane Chazelas
<stephane.chazelas@gmail.com> wrote:
>
> 2019-08-21 17:51:11 +0430, Aryn Starr:
> > Is there a way to get the result of the last glob?

In addition to zmv, you might also look at Functions/Zle/keeper in the
distribution.  It's set up to work with some ZLE key bindings, but it
also can be used standalone.

% source $^fpath/keeper(N[1])
% keep *
% for k in $kept; ebook-convert $k $k.epub

Or

% find . -name \*.mobi | keep

etc.

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

* Re: Get the result of the last glob
  2019-08-22  7:32         ` Stephane Chazelas
@ 2019-08-22  7:35           ` Aryn Starr
  0 siblings, 0 replies; 11+ messages in thread
From: Aryn Starr @ 2019-08-22  7:35 UTC (permalink / raw)
  To: Stephane Chazelas; +Cc: zsh-users

Thanks. zsh continues to have all kinds of weird features:D

> On Aug 22, 2019, at 12:02 PM, Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> 
> 2019-08-22 10:06:51 +0430, Aryn Starr:
>> What’s top posting?
> 
> See https://en.wikipedia.org/wiki/Posting_style
> 
> Your lines are also too long (72 recommended maximum, so it
> takes a few quoting layers to get to 80, the standard terminal
> width), but that's probably down to you using an Apple mail
> client. And I suppose those recommendations (including
> top-posting) make less sense now that people use mobile phones
> to read their emails (maybe not their emails about zsh though).
> 
>> 
>> Can’t $lastglob be available in the same command? It would be in the same fork, I imagine …
>> `command * -d $lastglob`
> [...]
> 
> $ lastglob=(); print -r -- *(e['lastglob+=($REPLY)']) /(e['reply=($^lastglob.epub)'])
> file1 file2 file1.epub file2.epub
> 
> See also:
> 
> $ print -r -- *(e['reply=($REPLY $REPLY.epub)'])
> file1 file1.epub file2 file2.epub
> 
> -- 
> Stephane


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

* Re: Get the result of the last glob
       [not found]       ` <6B392E5A-528B-4E73-8DF4-A10D6775D5FB__35805.0242810108$1566452299$gmane$org@icloud.com>
@ 2019-08-22  7:32         ` Stephane Chazelas
  2019-08-22  7:35           ` Aryn Starr
  0 siblings, 1 reply; 11+ messages in thread
From: Stephane Chazelas @ 2019-08-22  7:32 UTC (permalink / raw)
  To: Aryn Starr; +Cc: Mikael Magnusson, zsh-users

2019-08-22 10:06:51 +0430, Aryn Starr:
> What’s top posting?

See https://en.wikipedia.org/wiki/Posting_style

Your lines are also too long (72 recommended maximum, so it
takes a few quoting layers to get to 80, the standard terminal
width), but that's probably down to you using an Apple mail
client. And I suppose those recommendations (including
top-posting) make less sense now that people use mobile phones
to read their emails (maybe not their emails about zsh though).

> 
> Can’t $lastglob be available in the same command? It would be in the same fork, I imagine …
> `command * -d $lastglob`
[...]

$ lastglob=(); print -r -- *(e['lastglob+=($REPLY)']) /(e['reply=($^lastglob.epub)'])
file1 file2 file1.epub file2.epub

See also:

$ print -r -- *(e['reply=($REPLY $REPLY.epub)'])
file1 file1.epub file2 file2.epub

-- 
Stephane

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

* Re: Get the result of the last glob
  2019-08-21 20:54     ` Mikael Magnusson
@ 2019-08-22  5:36       ` Aryn Starr
       [not found]       ` <6B392E5A-528B-4E73-8DF4-A10D6775D5FB__35805.0242810108$1566452299$gmane$org@icloud.com>
  1 sibling, 0 replies; 11+ messages in thread
From: Aryn Starr @ 2019-08-22  5:36 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 897 bytes --]

What’s top posting?

Can’t $lastglob be available in the same command? It would be in the same fork, I imagine …
`command * -d $lastglob`

> On Aug 22, 2019, at 1:24 AM, Mikael Magnusson <mikachu@gmail.com> wrote:
> 
>> On 8/21/19, Aryn Starr <whereislelouch@icloud.com> wrote:
>> Thanks. I needed this to be non-interactive, and I indeed created a function
>> for it, I was just wondering if it was possible to get the glob in a magic
>> variable or sth, which it seems it is not.
> [don't top post]
> 
> Yes, it's not possible to get anything like $lastglob, because globs
> are expanded after forking¹ to run the external command, so no results
> are available to the interactive shell for subsequent use.
> 
> ¹ if you're not running an external command it would theoretically be
> possible, but it would be an inconsistent interface.
> 
> -- 
> Mikael Magnusson

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

* Re: Get the result of the last glob
  2019-08-21 14:40   ` Aryn Starr
  2019-08-21 14:47     ` Daniel Shahaf
@ 2019-08-21 20:54     ` Mikael Magnusson
  2019-08-22  5:36       ` Aryn Starr
       [not found]       ` <6B392E5A-528B-4E73-8DF4-A10D6775D5FB__35805.0242810108$1566452299$gmane$org@icloud.com>
  1 sibling, 2 replies; 11+ messages in thread
From: Mikael Magnusson @ 2019-08-21 20:54 UTC (permalink / raw)
  To: Aryn Starr; +Cc: Daniel Shahaf, zsh-users

On 8/21/19, Aryn Starr <whereislelouch@icloud.com> wrote:
> Thanks. I needed this to be non-interactive, and I indeed created a function
> for it, I was just wondering if it was possible to get the glob in a magic
> variable or sth, which it seems it is not.
[don't top post]

Yes, it's not possible to get anything like $lastglob, because globs
are expanded after forking¹ to run the external command, so no results
are available to the interactive shell for subsequent use.

¹ if you're not running an external command it would theoretically be
possible, but it would be an inconsistent interface.

-- 
Mikael Magnusson

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

* Re: Get the result of the last glob
  2019-08-21 14:40   ` Aryn Starr
@ 2019-08-21 14:47     ` Daniel Shahaf
  2019-08-21 20:54     ` Mikael Magnusson
  1 sibling, 0 replies; 11+ messages in thread
From: Daniel Shahaf @ 2019-08-21 14:47 UTC (permalink / raw)
  To: Aryn Starr; +Cc: zsh-users

Aryn Starr wrote on Wed, 21 Aug 2019 14:40 +00:00:
> Thanks. I needed this to be non-interactive, and I indeed created a 
> function for it, I was just wondering if it was possible to get the 
> glob in a magic variable or sth, which it seems it is not.

Of course it's possible:

() { typeset -p argv } *
set -- *; typeset -p argv
l=( * )
for i in * ; typeset -p $i

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

* Re: Get the result of the last glob
  2019-08-21 14:31 ` Daniel Shahaf
  2019-08-21 14:34   ` Daniel Shahaf
@ 2019-08-21 14:40   ` Aryn Starr
  2019-08-21 14:47     ` Daniel Shahaf
  2019-08-21 20:54     ` Mikael Magnusson
  1 sibling, 2 replies; 11+ messages in thread
From: Aryn Starr @ 2019-08-21 14:40 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-users

Thanks. I needed this to be non-interactive, and I indeed created a function for it, I was just wondering if it was possible to get the glob in a magic variable or sth, which it seems it is not.

> On Aug 21, 2019, at 7:01 PM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> 
> Aryn Starr wrote on Wed, 21 Aug 2019 13:22 +00:00:
>> ebook-convert * *.epub
> 
> Assuming emacs keybindings, you can do:
> 
> ebook-convert *<TAB>{,.epub}
> 
> (where <TAB> will expand the glob)
> 
> or
> 
> ebook-convert * *<Ctrl+X>*.epub
> 
> (where "<Ctrl+X>*" is the default binding for the expand-word widget, which will expand the glob)
> 
> or even
> 
> () { for 1; do ebook-convert $1 $1.epub; done } *
> 
> which, unlike the previous examples, works for any number of files.  (It's an anonymous function with arguments.)
> 
>> Which I want expanded to:
>> 
>> ebook-convert somebook.mobi somebook.mobi.epub
>> 


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

* Re: Get the result of the last glob
  2019-08-21 14:31 ` Daniel Shahaf
@ 2019-08-21 14:34   ` Daniel Shahaf
  2019-08-21 14:40   ` Aryn Starr
  1 sibling, 0 replies; 11+ messages in thread
From: Daniel Shahaf @ 2019-08-21 14:34 UTC (permalink / raw)
  To: Aryn Starr, zsh-users

Daniel Shahaf wrote on Wed, 21 Aug 2019 14:33 +00:00:
> Aryn Starr wrote on Wed, 21 Aug 2019 13:22 +00:00:
> > ebook-convert * *.epub
> 
> Assuming emacs keybindings, you can do:

Clarification: this assumption is only important for the expand-word example, since vi mode uses different keybindings.

> ebook-convert * *<Ctrl+X>*.epub
> 
> (where "<Ctrl+X>*" is the default binding for the expand-word widget, 
> which will expand the glob)

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

* Re: Get the result of the last glob
  2019-08-21 13:21 Aryn Starr
@ 2019-08-21 14:31 ` Daniel Shahaf
  2019-08-21 14:34   ` Daniel Shahaf
  2019-08-21 14:40   ` Aryn Starr
  0 siblings, 2 replies; 11+ messages in thread
From: Daniel Shahaf @ 2019-08-21 14:31 UTC (permalink / raw)
  To: Aryn Starr, zsh-users

Aryn Starr wrote on Wed, 21 Aug 2019 13:22 +00:00:
> ebook-convert * *.epub

Assuming emacs keybindings, you can do:

ebook-convert *<TAB>{,.epub}

(where <TAB> will expand the glob)

or

ebook-convert * *<Ctrl+X>*.epub

(where "<Ctrl+X>*" is the default binding for the expand-word widget, which will expand the glob)

or even

() { for 1; do ebook-convert $1 $1.epub; done } *

which, unlike the previous examples, works for any number of files.  (It's an anonymous function with arguments.)

> Which I want expanded to:
> 
> ebook-convert somebook.mobi somebook.mobi.epub
>

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

* Get the result of the last glob
@ 2019-08-21 13:21 Aryn Starr
  2019-08-21 14:31 ` Daniel Shahaf
  0 siblings, 1 reply; 11+ messages in thread
From: Aryn Starr @ 2019-08-21 13:21 UTC (permalink / raw)
  To: zsh-users

Is there a way to get the result of the last glob?
I want to do sth like this in a folder with only one file, somebook.mobi:

ebook-convert * *.epub

Which I want expanded to:

ebook-convert somebook.mobi somebook.mobi.epub

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

end of thread, other threads:[~2019-08-22  7:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <918ED85F-9DE5-4365-B962-45AD4AADDF46__5329.09398186661$1566393766$gmane$org@icloud.com>
2019-08-21 15:17 ` Get the result of the last glob Stephane Chazelas
2019-08-21 15:43   ` Bart Schaefer
2019-08-21 13:21 Aryn Starr
2019-08-21 14:31 ` Daniel Shahaf
2019-08-21 14:34   ` Daniel Shahaf
2019-08-21 14:40   ` Aryn Starr
2019-08-21 14:47     ` Daniel Shahaf
2019-08-21 20:54     ` Mikael Magnusson
2019-08-22  5:36       ` Aryn Starr
     [not found]       ` <6B392E5A-528B-4E73-8DF4-A10D6775D5FB__35805.0242810108$1566452299$gmane$org@icloud.com>
2019-08-22  7:32         ` Stephane Chazelas
2019-08-22  7:35           ` Aryn Starr

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).