[Haifux] [HAIFUX LECTURE] Command-line utilities: Tips and tricks (part II) -- Eli Billauer
Oleg Goldshmidt
pub at goldshmidt.org
Mon Aug 19 17:00:40 IDT 2013
Daniel Shahaf <d.s at daniel.shahaf.name> writes:
> Sure. It's a zsh-specific syntax for an anonymous function with
> arguments. In effect it's an anonymous block. For example:
>
> % (){ printf "$1\n" 04 } "%s"
> 04
> % (){ printf "$1\n" 04 } "%d"
> 4
> % (){ printf "$1\n" 04 } "%e"
> 4.000000e+00
>
> In interactive usage I sometimes find myself wanting to run a command
> several times in a row with one argument changed. When that argument is
> in the middle of a (potentially multi-line) command, I find it easier to
> change it between runs by using an ad-hoc anonymous function to move the
> argument-to-be-changed to the end of the input. Another case where that
> would be useful is when the argument appears in two places in the
> command --- using an anonymous function allows changing the argument in
> just one place rather than two.
In bash, I use fc ("fix command") for this:
$ printf "%s\n" 04
04
$ fc -s %s=%d
printf "%d\n" 04
4
$ fc -s %d=%e
printf "%e\n" 04
4.000000e+00
Very useful. Check out also
$ fc -n -3 -1
or similar to edit the last 3 commnds (with $EDITOR, or use -e)
and execute them when done editing.
--
Oleg Goldshmidt | pub at goldshmidt.org
More information about the Linux-il
mailing list