zsh-users
 help / color / mirror / code / Atom feed
* alias -g
@ 2010-08-03 19:10 Eric Smith
  2010-08-03 19:37 ` Alexey I. Froloff
  2010-08-04  7:22 ` alias -g Kazuo Teramoto
  0 siblings, 2 replies; 7+ messages in thread
From: Eric Smith @ 2010-08-03 19:10 UTC (permalink / raw)
  To: Zsh Users

Hi

I define an alias like so:
alias -g T='|translate'

So when I go
$ echo een vreemde taal T

Then zsh will pipe that foreign phrase to a translator.

How do I drop the `echo` with alias -g or something similiar
so all I need to do is go:
$ een vreemde taal T

Thanks a lot.

-- 
- Eric Smith


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias -g
  2010-08-03 19:10 alias -g Eric Smith
@ 2010-08-03 19:37 ` Alexey I. Froloff
  2010-08-03 20:02   ` Eric Smith
  2010-08-04  7:22 ` alias -g Kazuo Teramoto
  1 sibling, 1 reply; 7+ messages in thread
From: Alexey I. Froloff @ 2010-08-03 19:37 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 348 bytes --]

On Tue, Aug 03, 2010 at 09:10:28PM +0200, Eric Smith wrote:
> How do I drop the `echo` with alias -g or something similiar
> so all I need to do is go:
> $ een vreemde taal T
You don't need alias:

function T() {
  echo $* | translate
}

$ T een vreemde taal

-- 
Regards,    --
Sir Raorn.   --- http://thousandsofhate.blogspot.com/

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias -g
  2010-08-03 19:37 ` Alexey I. Froloff
@ 2010-08-03 20:02   ` Eric Smith
  2010-08-04 15:31     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Eric Smith @ 2010-08-03 20:02 UTC (permalink / raw)
  To: zsh-users

Of course

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.

-- 
- Eric Smith
Alexey I. Froloff said:
> On Tue, Aug 03, 2010 at 09:10:28PM +0200, Eric Smith wrote:
> > How do I drop the `echo` with alias -g or something similiar
> > so all I need to do is go:
> > $ een vreemde taal T
> You don't need alias:
> 
> function T() {
>   echo $* | translate
> }
> 
> $ T een vreemde taal
> 
> -- 
> Regards,    --
> Sir Raorn.   --- http://thousandsofhate.blogspot.com/



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias -g
  2010-08-03 19:10 alias -g Eric Smith
  2010-08-03 19:37 ` Alexey I. Froloff
@ 2010-08-04  7:22 ` Kazuo Teramoto
  2010-08-04  7:24   ` Kazuo Teramoto
  1 sibling, 1 reply; 7+ messages in thread
From: Kazuo Teramoto @ 2010-08-04  7:22 UTC (permalink / raw)
  To: Zsh Users

On Tue, Aug 3, 2010 at 4:10 PM, Eric Smith <es@fruitcom.com> wrote:
> So when I go
> $ echo een vreemde taal T
[...]
> How do I drop the `echo` with alias -g or something similiar
> so all I need to do is go:
> $ een vreemde taal T

Uhh, I think this can be done for example creating a new autocd like
option, but you gonna need to wait for a better zsh user then me to
show you the code.

But, I think that what you like to do come at a highly price. Because
you loose the very definition of valid command. For example you can
forget that 'rm' is a valid command and type

$ rm important thing T

And get 'important' and 'thing' removed. Ok, rm is not a valid world
in a lot of languages, but what about sleep, wait? Or worse 'unlink'
it is a valid world and can remove your files like rm.

This too can be solved, for example, the new autocd-like function can
check if the last arg is T not if the first arg is a valid command,
but I still think that this can be a shoot in the foot some time.

And for me

$ T een vreemde

is more natural than your syntax, because I read it as "translate 'een
vreemde'". I'm prefer postfix for calculators but for this the prefix
notation is more logical for me.

Regards,
Kazuo

-- 
«Dans la vie, rien n'est à craindre, tout est à comprendre»
Marie Sklodowska Curie.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias -g
  2010-08-04  7:22 ` alias -g Kazuo Teramoto
@ 2010-08-04  7:24   ` Kazuo Teramoto
  0 siblings, 0 replies; 7+ messages in thread
From: Kazuo Teramoto @ 2010-08-04  7:24 UTC (permalink / raw)
  To: Zsh Users

On Wed, Aug 4, 2010 at 4:22 AM, Kazuo Teramoto <kaz.rag@gmail.com> wrote:
> And get 'important' and 'thing' removed. Ok, rm is not a valid world

Ops. of course I mean word...

-- 
«Dans la vie, rien n'est à craindre, tout est à comprendre»
Marie Sklodowska Curie.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias -g
  2010-08-03 20:02   ` Eric Smith
@ 2010-08-04 15:31     ` Bart Schaefer
  2010-08-06 11:33       ` alias -g -> SOLVED Eric Smith
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2010-08-04 15:31 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: alias -g -> SOLVED
  2010-08-04 15:31     ` Bart Schaefer
@ 2010-08-06 11:33       ` Eric Smith
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Smith @ 2010-08-06 11:33 UTC (permalink / raw)
  To: zsh-users

NICE

Thanks Bart

- Eric Smith

> 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 )
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2010-08-06 11:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-08-03 19:10 alias -g Eric Smith
2010-08-03 19:37 ` Alexey I. Froloff
2010-08-03 20:02   ` Eric Smith
2010-08-04 15:31     ` Bart Schaefer
2010-08-06 11:33       ` alias -g -> SOLVED Eric Smith
2010-08-04  7:22 ` alias -g Kazuo Teramoto
2010-08-04  7:24   ` Kazuo Teramoto

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).