Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard that executes command once for each match

Alternate title: How to loop without a loop or xargs.

Recently, I switched to zsh because of its many features. I'm curious: Is there a feature which expands wildcards such that the command is executed once for each match instead of only one time for all matches at once.

Example

The command ebook-convert input_file output_file [options] accepts just one input file. When I want to convert multiple files, I have to execute the command multiple times manually or use a loop, for instance:

for i in *.epub; do 
    ebook-convert "$i" .mobi
done

What I'd like is a wildcard that functions like the loop so that I can save a few keystrokes. Let said wildcard be . The command

ebook-convert ⁂.epub .mobi

should expand to

ebook-convert 1stMatch.epub .mobi
ebook-convert 2ndMatch.epub .mobi
ebook-convert 3rdMatch.epub .mobi
...

Still interested in other answers

I accepted an answer that works for me (thanks to Grisha Levit). But if you know other shells with such a feature, alternative commands which are shorter than writing a loop, or even a way to extend zsh with the wanted wildcard your answers are appreciated.

like image 992
Socowi Avatar asked Feb 03 '17 21:02

Socowi


1 Answers

so that I can save a few keystrokes

OK, so let's say you typed out

ebook-convert *.epub .mobi

…and now you realized that this isn't going to work — you need to write a loop. What would you normally do? Probably something like:

  • add ; done to the end of the line
  • hit CtrlA to go the beginning of the line
  • type for i in
  • etc…

This looks like a good fit for readline keyboard macro:

Let's write this out the steps in terms of readline commands and regular keypresses:

end-of-line                    # (start from the end for consistency)
; done                         # type in the loop closing statement
character-search-backward *    # go back to the where the glob is
shell-backward-word            # (in case the glob is in the mid-word)
shell-kill-word                # "cut" the word with the glob
"$i"                           # type the loop variable
beginning-of-line              # go back to the start of the line
for i in                       # type the beginning of the loop opening
yank                           # "paste" the word with the glob
; do                           # type the end of the loop opening

Creating the binding:

For any readline command used above that does not have a key-binding, we need to create one. We also need to create a binding for the new macro that we are creating.

Unless you've already done a lot of readline customization, running the commands below will set the bindings up for the current shell. This uses default bindings like \C-eend-of-line.

bind '"\eB": shell-backward-word'
bind '"\eD": shell-kill-word'

bind '"\C-i": "\C-e; done\e\C-]*\eB\eD \"$i\"\C-afor i in\C-y; do "'

The bindings can also go into the inputrc file for persistence.

Using the shortcut:

After setting things up:

  1. Type in something like

    ebook-convert *.epub .mobi
  2. Press CtrlI
  3. The line will transform into

    for i in *.epub; do ebook-convert "$i" .mobi; done

If you want to run the command right away, you can modify the macro to append a \C-j as the last keypress, which will trigger accept-line (same as hitting Return).

like image 182
Grisha Levit Avatar answered Sep 20 '22 12:09

Grisha Levit