Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: alias -g

On Aug 3, 10:02pm, Eric Smith wrote:
> Subject: Re: alias -g
> But the nice thing about the alias -g mapping, is that
> the command goes at the end.
> This is more natural usage and I was wondering if there is a way to do 
> this.

There's no way to do it directly with an alias.  Unlike (t)csh aliases,
zsh aliases cannot manipulate the order of other words in the command.

However, you may be able to get what you want via preexec_functions.

 auto_translate() {
   local -a commandline
   commandline=( ${(z)1} )
   if [[ $commandline[-1] == T ]]
     print -R "${commandline[0,-2]}" | translate

 # Important that this comes after function definition
 alias -g T='${auto_translate?command execution suppressed}'

 # Set auto_translate to the empty string to both translate
 # and then execute the original command anyway
 unset auto_translate	# Force T alias to abort
 preexec_functions+=( auto_translate )

The trick with ${...?...} is to introduce a failure into the original
command so that it doesn't execute, because the preexec hooks don't
provide a mechanism to gainsay command execution on their own.

Note that this won't work well for compound commands, e.g., if you do

    rm somefile && echo removed somefile T

then you'll see the transation of the entire command line, but the rm
will execute and the echo will not.  There's no way to intercept every
command execution in a compound expression and stop it.

Messages sorted by: Reverse Date, Date, Thread, Author