zsh-users
 help / color / mirror / code / Atom feed
* retrieving the results of last command?
@ 2005-05-20 12:18 Francisco Borges
  2005-05-20 12:54 ` Peter Stephenson
  2005-05-20 14:49 ` Bart Schaefer
  0 siblings, 2 replies; 13+ messages in thread
From: Francisco Borges @ 2005-05-20 12:18 UTC (permalink / raw)
  To: Zsh User

Often I'm in the situation of using say:

% locate a-file
/usr/share/prog/a-file
%

Then I want to use the pathname to copy the file. I Know of 3 somewhat
cumbersome ways to do this:

1. Copying and pasting with the mouse.

2. Using `locate a-file` (which is a bit more cumbersome to me than to
most because I swapped the keys to ` and ~).

3. Using $(comm).


Is there a "faster" way than `` to perform command expansion?

I tried to code a widget to do this, but I this is as far as I got:

insert-last-command-output(){
    com="`fc -ln -1`"
    RBUFFER=`$com`
}
zle -N insert-last-command-output
bindkey "^X^L" insert-last-command-output

#(^X^L was the first free key I could remember...)

but is fails with:
% locate ipython.el
/usr/bla/ipython.el
% insert-last-command-output:1: command not found: locate ipython.el

Any hints?

Cheers,
Francisco


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

* Re: retrieving the results of last command?
  2005-05-20 12:18 retrieving the results of last command? Francisco Borges
@ 2005-05-20 12:54 ` Peter Stephenson
  2005-05-20 13:44   ` Mikael Magnusson
  2005-05-20 14:24   ` [zsh] " Francisco Borges
  2005-05-20 14:49 ` Bart Schaefer
  1 sibling, 2 replies; 13+ messages in thread
From: Peter Stephenson @ 2005-05-20 12:54 UTC (permalink / raw)
  To: Zsh users list

Francisco Borges wrote:
> I tried to code a widget to do this, but I this is as far as I got:
> 
> insert-last-command-output(){
>     com="`fc -ln -1`"
>     RBUFFER=`$com`
> }
> zle -N insert-last-command-output
> bindkey "^X^L" insert-last-command-output
> 
> #(^X^L was the first free key I could remember...)
> 
> but is fails with:
> % locate ipython.el
> /usr/bla/ipython.el
> % insert-last-command-output:1: command not found: locate ipython.el

You get a complete command line, which needs to be reevaluated.  Your
$com is just a string with the line in it, not the command line as
the shell would parse it.

You can simplify retrieving the command by using the parameter history
from the zsh/parameter library, which contains the history indexed
by the history line number, and the standard parameter HISTCMD which
gives the current history line number.

Normal "insert" widgets append stuff to the left of the cursor
rather than replacing RBUFFER.

You whould be able to do this:

zmodload -i zsh/parameter
insert-last-command-output() {
  LBUFFER+="$(eval $history[$((HISTCMD-1))])"
}

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


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

* Re: retrieving the results of last command?
  2005-05-20 12:54 ` Peter Stephenson
@ 2005-05-20 13:44   ` Mikael Magnusson
  2005-05-20 13:55     ` Peter Stephenson
  2005-05-20 14:24   ` [zsh] " Francisco Borges
  1 sibling, 1 reply; 13+ messages in thread
From: Mikael Magnusson @ 2005-05-20 13:44 UTC (permalink / raw)
  To: Zsh users list; +Cc: Peter Stephenson

On 5/20/05, Peter Stephenson <pws@csr.com> wrote:
> Francisco Borges wrote:
> > I tried to code a widget to do this, but I this is as far as I got:
> >
> > insert-last-command-output(){
> >     com="`fc -ln -1`"
> >     RBUFFER=`$com`
> > }
> > zle -N insert-last-command-output
> > bindkey "^X^L" insert-last-command-output
> >
> > #(^X^L was the first free key I could remember...)
> >
> > but is fails with:
> > % locate ipython.el
> > /usr/bla/ipython.el
> > % insert-last-command-output:1: command not found: locate ipython.el
> 
> You get a complete command line, which needs to be reevaluated.  Your
> $com is just a string with the line in it, not the command line as
> the shell would parse it.
> 
> You can simplify retrieving the command by using the parameter history
> from the zsh/parameter library, which contains the history indexed
> by the history line number, and the standard parameter HISTCMD which
> gives the current history line number.
> 
> Normal "insert" widgets append stuff to the left of the cursor
> rather than replacing RBUFFER.
> 
> You whould be able to do this:
> 
> zmodload -i zsh/parameter
> insert-last-command-output() {
>   LBUFFER+="$(eval $history[$((HISTCMD-1))])"
> }

Hello,
Is there a way to ask for confirmation here? It's not always history
contains what you expect, for example if you share history and just
ran rm -rf . in another terminal, and press this bind you might not be
so happy with the outcome. Ie something like
zsh: are you sure you want to run $history[$((HISTCMD-1))] again? [yn]
Is the read builtin what i want? I never quite got the hang of that one.

-- 
Mikael Magnusson


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

* Re: retrieving the results of last command?
  2005-05-20 13:44   ` Mikael Magnusson
@ 2005-05-20 13:55     ` Peter Stephenson
  2005-05-21 10:17       ` Mikael Magnusson
  0 siblings, 1 reply; 13+ messages in thread
From: Peter Stephenson @ 2005-05-20 13:55 UTC (permalink / raw)
  To: Zsh users list

Mikael Magnusson wrote:
> On 5/20/05, Peter Stephenson <pws@csr.com> wrote:
> > insert-last-command-output() {
> >   LBUFFER+=3D"$(eval $history[$((HISTCMD-1))])"
> > }

> Hello,
> Is there a way to ask for confirmation here? It's not always history
> contains what you expect, for example if you share history and just
> ran rm -rf . in another terminal, and press this bind you might not be
> so happy with the outcome. Ie something like
> zsh: are you sure you want to run $history[$((HISTCMD-1))] again? [yn]
> Is the read builtin what i want? I never quite got the hang of that one.

With 4.2, the best way of prompting from within a command line is to use
the supplied function read-from-minibuffer.  Something like this:

insert-last-command-output() {
  local hist=$history[$((HISTCMD-1))] REPLY

  autoload -U read-from-minibuffer
  read-from-minibuffer -k1 "Do you want to execute
  $hist
again y/[n]? " || [[ $REPLY = [yY]* ]] || return 1

  LBUFFER+="$(eval $hist)"
}

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


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

* Re: [zsh] Re: retrieving the results of last command?
  2005-05-20 12:54 ` Peter Stephenson
  2005-05-20 13:44   ` Mikael Magnusson
@ 2005-05-20 14:24   ` Francisco Borges
  1 sibling, 0 replies; 13+ messages in thread
From: Francisco Borges @ 2005-05-20 14:24 UTC (permalink / raw)
  To: Zsh users list

» On Fri, May 20, 2005 at 01:54PM +0100, Peter Stephenson wrote:

> You can simplify retrieving the command by using the parameter history
> from the zsh/parameter library, which contains the history indexed
> by the history line number, and the standard parameter HISTCMD which
> gives the current history line number.
>
> Normal "insert" widgets append stuff to the left of the cursor
> rather than replacing RBUFFER.
>
> You whould be able to do this:
>
> zmodload -i zsh/parameter
> insert-last-command-output() {
>   LBUFFER+="$(eval $history[$((HISTCMD-1))])"
> }

Thanks a lot for the corrections and explantion!

Cheers,
Francisco.


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

* Re: retrieving the results of last command?
  2005-05-20 12:18 retrieving the results of last command? Francisco Borges
  2005-05-20 12:54 ` Peter Stephenson
@ 2005-05-20 14:49 ` Bart Schaefer
  2005-05-20 17:11   ` Christian Taylor
  2005-05-23  6:45   ` Felix Rosencrantz
  1 sibling, 2 replies; 13+ messages in thread
From: Bart Schaefer @ 2005-05-20 14:49 UTC (permalink / raw)
  To: Zsh User

On May 20,  2:18pm, Francisco Borges wrote:
}
} Often I'm in the situation of using say:
} 
} % locate a-file
} /usr/share/prog/a-file
} %
} 
} Then I want to use the pathname to copy the file.

The least computationally expensive way to do this is to save the output
in the first place.

E.g., Functions/Zle/keeper in the current dev versions of zsh, available
from the SourceForge CVS server.  You might even want to add

    locate() { command locate "$@" | keep }

Another trick that I do is

    # Imaginative function naming here, I know
    esc-backquote() { BUFFER='$( '"$BUFFER"' )'; CURSOR=0 }
    zle -N esc-backquote
    bindkey '\e`' esc-backquote

Then I can search through the history any way I like and when I find the
command whose output I want to capture, type ESC-` to wrap it in $( ... ).
I also have

    bindkey -s '\ev' '\e`vi '

so that ESC-v leaves me with "vi $( ... )" but I don't use that one much
any more, for some reason.


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

* Re: retrieving the results of last command?
  2005-05-20 14:49 ` Bart Schaefer
@ 2005-05-20 17:11   ` Christian Taylor
  2005-05-23  6:45   ` Felix Rosencrantz
  1 sibling, 0 replies; 13+ messages in thread
From: Christian Taylor @ 2005-05-20 17:11 UTC (permalink / raw)
  To: zsh-users

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

On Friday 20 May 2005 16:49, Bart Schaefer wrote:
> On May 20,  2:18pm, Francisco Borges wrote:
> }
> } Often I'm in the situation of using say:
> }
> } % locate a-file
> } /usr/share/prog/a-file
> } %
> }
> } Then I want to use the pathname to copy the file.
>
> [snip]
>
> I also have
>
>     bindkey -s '\ev' '\e`vi '
>
> so that ESC-v leaves me with "vi $( ... )" but I don't use that one much
> any more, for some reason.

I have something similar that I use for purposes mentioned in the original posting:

bindkey -s '\ei' '^U\e>`^Y`'

In the example it would work like this:

% locate a-file
/usr/share/prog/a-file
% cp [now I go to the history entry I want and press ESC-i]
% cp `locate a-file`

I guess it's not very elegant, since it depends on the default keybindings ^U, ESC-> and ^Y, but I hope it may be of use to someone.

Christian

[-- Attachment #2: Type: text/html, Size: 1283 bytes --]

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

* Re: retrieving the results of last command?
  2005-05-20 13:55     ` Peter Stephenson
@ 2005-05-21 10:17       ` Mikael Magnusson
  0 siblings, 0 replies; 13+ messages in thread
From: Mikael Magnusson @ 2005-05-21 10:17 UTC (permalink / raw)
  To: Zsh users list

On 5/20/05, Peter Stephenson <pws@csr.com> wrote:
> Mikael Magnusson wrote:
> > On 5/20/05, Peter Stephenson <pws@csr.com> wrote:
> > > insert-last-command-output() {
> > >   LBUFFER+=3D"$(eval $history[$((HISTCMD-1))])"
> > > }
> 
> > Hello,
> > Is there a way to ask for confirmation here? It's not always history
> > contains what you expect, for example if you share history and just
> > ran rm -rf . in another terminal, and press this bind you might not be
> > so happy with the outcome. Ie something like
> > zsh: are you sure you want to run $history[$((HISTCMD-1))] again? [yn]
> > Is the read builtin what i want? I never quite got the hang of that one.
> 
> With 4.2, the best way of prompting from within a command line is to use
> the supplied function read-from-minibuffer.  Something like this:
> 
> insert-last-command-output() {
>   local hist=$history[$((HISTCMD-1))] REPLY
> 
>   autoload -U read-from-minibuffer
>   read-from-minibuffer -k1 "Do you want to execute
>   $hist
> again y/[n]? " || [[ $REPLY = [yY]* ]] || return 1
> 
>   LBUFFER+="$(eval $hist)"
> }

Thanks, that works fine (i assume the first || should be a && though).

(ps, sorry about the ccing on the last mail, gmail still does the
wrong thing when replying to a mailing lists and i didn't quite fix it
right.)
-- 
Mikael Magnusson


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

* Re: retrieving the results of last command?
  2005-05-20 14:49 ` Bart Schaefer
  2005-05-20 17:11   ` Christian Taylor
@ 2005-05-23  6:45   ` Felix Rosencrantz
  2005-05-23 15:33     ` Bart Schaefer
  2005-05-23 15:45     ` Oliver Kiddle
  1 sibling, 2 replies; 13+ messages in thread
From: Felix Rosencrantz @ 2005-05-23  6:45 UTC (permalink / raw)
  To: Zsh User

Is there a way to configure completion, so that it will first attempt
to only complete items that it would normally complete but are only in
the keep list of the keeper function, and if there is nothing there
complete as it normally would.

Said another way, take the intersection of the kept values and the
values the completer would normally complete in a given context.  Use
that intersection as the possible completions offered to the user.  If
the intersection is empty, the completer would complete as it does
normally.

-FR


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

* Re: retrieving the results of last command?
  2005-05-23  6:45   ` Felix Rosencrantz
@ 2005-05-23 15:33     ` Bart Schaefer
  2005-05-23 15:45     ` Oliver Kiddle
  1 sibling, 0 replies; 13+ messages in thread
From: Bart Schaefer @ 2005-05-23 15:33 UTC (permalink / raw)
  To: Zsh User

On May 22, 11:45pm, Felix Rosencrantz wrote:
}
} Is there a way to configure completion, so that it will first attempt
} to only complete items that it would normally complete but are only in
} the keep list of the keeper function, and if there is nothing there
} complete as it normally would.

You'd need a trick like the one done in _expand_word_and_keep, where
you redefine compadd and then call completion.  The redefined compadd
would compare its arguments to $kept and call the real compadd only
for those that are present.

However, that'd be very difficult to do accurately, because of the
wide variety of ways that potential matches can be passed to compadd.
Your wrapper for compadd would have to take into account prefixes and
suffixes, arrays passed by name rather than value, etc.


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

* Re: retrieving the results of last command?
  2005-05-23  6:45   ` Felix Rosencrantz
  2005-05-23 15:33     ` Bart Schaefer
@ 2005-05-23 15:45     ` Oliver Kiddle
  2005-05-23 15:56       ` Bart Schaefer
  1 sibling, 1 reply; 13+ messages in thread
From: Oliver Kiddle @ 2005-05-23 15:45 UTC (permalink / raw)
  To: zsh-users

Felix Rosencrantz wrote:
> Is there a way to configure completion, so that it will first attempt
> to only complete items that it would normally complete but are only in
> the keep list of the keeper function, and if there is nothing there
> complete as it normally would.

You could try doing something with ignored-patterns and zstyle -e:

Does keeper just keep it's words in an array? If so something like
'(${(j.|.)~keptwords})' may be a start. You may also need to remove
$IPREFIX and $ISUFFIX from the beginning and end of each word in the
array. You would also have problems if you want to use other
ignore-patterns styles.

Oliver


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

* Re: retrieving the results of last command?
  2005-05-23 15:45     ` Oliver Kiddle
@ 2005-05-23 15:56       ` Bart Schaefer
  2005-05-23 16:06         ` Oliver Kiddle
  0 siblings, 1 reply; 13+ messages in thread
From: Bart Schaefer @ 2005-05-23 15:56 UTC (permalink / raw)
  To: zsh-users

On May 23,  5:45pm, Oliver Kiddle wrote:
}
} Felix Rosencrantz wrote:
} > only complete items that it would normally complete but are only in
} > the keep list of the keeper function
} 
} You could try doing something with ignored-patterns and zstyle -e:

Hmm.  Interesting idea, but how would that work?  ignored-patterns is
for things to leave out of the results, and here we have a list of
things we want to preserve.

} Does keeper just keep it's words in an array?

Yep, it's just called $kept.


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

* Re: retrieving the results of last command?
  2005-05-23 15:56       ` Bart Schaefer
@ 2005-05-23 16:06         ` Oliver Kiddle
  0 siblings, 0 replies; 13+ messages in thread
From: Oliver Kiddle @ 2005-05-23 16:06 UTC (permalink / raw)
  To: zsh-users

Bart wrote:
> 
> Hmm.  Interesting idea, but how would that work?  ignored-patterns is
> for things to leave out of the results, and here we have a list of
> things we want to preserve.

Negate the pattern: ^(${(j.|.)~kept})


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

end of thread, other threads:[~2005-05-23 16:06 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-05-20 12:18 retrieving the results of last command? Francisco Borges
2005-05-20 12:54 ` Peter Stephenson
2005-05-20 13:44   ` Mikael Magnusson
2005-05-20 13:55     ` Peter Stephenson
2005-05-21 10:17       ` Mikael Magnusson
2005-05-20 14:24   ` [zsh] " Francisco Borges
2005-05-20 14:49 ` Bart Schaefer
2005-05-20 17:11   ` Christian Taylor
2005-05-23  6:45   ` Felix Rosencrantz
2005-05-23 15:33     ` Bart Schaefer
2005-05-23 15:45     ` Oliver Kiddle
2005-05-23 15:56       ` Bart Schaefer
2005-05-23 16:06         ` Oliver Kiddle

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