[Haifux] [HAIFUX LECTURE] Command-line utilities: Tips and tricks (part II) -- Eli Billauer

[Haifux] [HAIFUX LECTURE] Command-line utilities: Tips and tricks (part II) -- Eli Billauer

Daniel Shahaf d.s at daniel.shahaf.name
Mon Aug 19 17:21:35 IDT 2013


Oleg Goldshmidt wrote on Mon, Aug 19, 2013 at 17:00:40 +0300:
> 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

That looks equivalent to ^foo^bar, which both shells support:

# printf %s\n 04
04
# ^%s^%d
4

> $ fc -n -3 -1
> 
> or similar to edit the last 3 commnds (with $EDITOR, or use -e) 
> and execute them when done editing.

Nice, thanks.  The same invocation works in zsh too.  I also know of an
extension to edit the *current* command line in $EDITOR:

(zsh)
autoload -U edit-command-line
zle -N edit-command-line
bindkey '^Fc' edit-command-line

(vim)
CTRL-F (controlled by :help 'cedit')

> 

We should probably be collecting such tips on a wiki page or something;
on list archives they'll just be lost...

Daniel



More information about the Linux-il mailing list