From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14224 invoked by alias); 4 Aug 2010 15:32:37 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 15250 Received: (qmail 578 invoked from network); 4 Aug 2010 15:32:35 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <100804083151.ZM7810@torch.brasslantern.com> Date: Wed, 04 Aug 2010 08:31:51 -0700 In-reply-to: Comments: In reply to Eric Smith "Re: alias -g" (Aug 3, 10:02pm) References: <20100803191028.GA16019@trustfood.org> <20100803193709.GA1012@altlinux.org> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: alias -g MIME-version: 1.0 Content-type: text/plain; charset=us-ascii 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 ]] then print -R "${commandline[0,-2]}" | translate fi } # 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.