zsh-users
 help / color / mirror / code / Atom feed
* "expanding" to copy-earlier-word
@ 2015-08-13  9:57 Dominik Vogt
  2015-08-13  9:59 ` Dominik Vogt
  2015-08-13 10:24 ` Peter Stephenson
  0 siblings, 2 replies; 7+ messages in thread
From: Dominik Vogt @ 2015-08-13  9:57 UTC (permalink / raw)
  To: Zsh Users

I've bound ctrl-j to copy-earlier-word, so I can copy the last word
of the previously executed command line to the current command
line.  Is there a way to do the same as part of the expansion
process?

What I'd like to have this something like this:

  $ make foo
  $ ./<copy-earlier-word>

or even

  $ make foo; ./<copy-earlier-word>

(where "<copy-earlier-word>" stands for something to be expanded
to the previous word, i.e. "foo").

In the shell history the command should be stored before expansion
takes place.

Ciao

Dominik ^_^  ^_^

-- 

Dominik Vogt
IBM Germany


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

* Re: "expanding" to copy-earlier-word
  2015-08-13  9:57 "expanding" to copy-earlier-word Dominik Vogt
@ 2015-08-13  9:59 ` Dominik Vogt
  2015-08-13 10:24 ` Peter Stephenson
  1 sibling, 0 replies; 7+ messages in thread
From: Dominik Vogt @ 2015-08-13  9:59 UTC (permalink / raw)
  To: Zsh Users

On Thu, Aug 13, 2015 at 10:57:27AM +0100, Dominik Vogt wrote:
> I've bound ctrl-j to copy-earlier-word,

(Except that I'm talking about "insert-last-word" which is bound
to meta-.)

Ciao

Dominik ^_^  ^_^

-- 

Dominik Vogt
IBM Germany


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

* Re: "expanding" to copy-earlier-word
  2015-08-13  9:57 "expanding" to copy-earlier-word Dominik Vogt
  2015-08-13  9:59 ` Dominik Vogt
@ 2015-08-13 10:24 ` Peter Stephenson
  2015-08-13 11:45   ` Dominik Vogt
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2015-08-13 10:24 UTC (permalink / raw)
  To: Zsh Users

On Thu, 13 Aug 2015 10:57:27 +0100
Dominik Vogt <vogt@linux.vnet.ibm.com> wrote:
> I've bound ctrl-j to copy-earlier-word, so I can copy the last word
> of the previously executed command line to the current command
> line.  Is there a way to do the same as part of the expansion
> process?

Hisotry expansion allows you to select previous words, but you'll need
to count from the left when retrieving words from the current line: you
can use ":$" to indicate the last argument, which with !# is the
previous argument, but "-" indicates a range, not a negative offset.
There was some discussion about this anomaly a couple of months ago;
it's potentially fixable with some new syntax (I've forgotten if there
was a conclusion).

> What I'd like to have this something like this:
> 
>   $ make foo
>   $ ./<copy-earlier-word>

./!:$

> or even
> 
>   $ make foo; ./<copy-earlier-word>

make foo; ./!#:1

(remember history expansion starts with word 0).  Unfortunately !#:$
gets you the ";" because while history expansion knows about dividing
into words it doesn't know which words are syntactically important.

pws


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

* Re: "expanding" to copy-earlier-word
  2015-08-13 10:24 ` Peter Stephenson
@ 2015-08-13 11:45   ` Dominik Vogt
  2015-08-13 12:16     ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Dominik Vogt @ 2015-08-13 11:45 UTC (permalink / raw)
  To: Zsh Users

On Thu, Aug 13, 2015 at 11:24:22AM +0100, Peter Stephenson wrote:
> On Thu, 13 Aug 2015 10:57:27 +0100
> Dominik Vogt <vogt@linux.vnet.ibm.com> wrote:
> > I've bound ctrl-j to copy-earlier-word, so I can copy the last word
> > of the previously executed command line to the current command
> > line.  Is there a way to do the same as part of the expansion
> > process?
> 
> Hisotry expansion allows you to select previous words, but you'll need
> to count from the left when retrieving words from the current line: you
> can use ":$" to indicate the last argument, which with !# is the
> previous argument, but "-" indicates a range, not a negative offset.
> There was some discussion about this anomaly a couple of months ago;
> it's potentially fixable with some new syntax (I've forgotten if there
> was a conclusion).
> 
> > What I'd like to have this something like this:
> > 
> >   $ make foo
> >   $ ./<copy-earlier-word>
> 
> ./!:$

Sort of works for me after "setopt histexpand".  Unfortunately, is
saves "./foo" in the history instead of "./!:$".  It looks like
there's no option to have the unexpanded string stored in the
history?

According to the man page history expansion comes first, followed
by alias expansion, the result of which is not stored in the
history files, so I guess the sequence is

 1) history expansion,
 2) save command line in history,
 3) alias expansion,
 4) everything else.

If there was a way to change that order to

 1) save command line in history,
 2) alias expansion,
 3) history expansion,
 4) everything else.

one could even have history expansion in aliases:

  $ alias -g PW='!:$'
  $ make foo
  $ echo PW

(I guess the global alias wouldn't allow "echo ./PW" anyway).

Ciao

Dominik ^_^  ^_^

-- 

Dominik Vogt
IBM Germany


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

* Re: "expanding" to copy-earlier-word
  2015-08-13 11:45   ` Dominik Vogt
@ 2015-08-13 12:16     ` Peter Stephenson
  2015-08-13 16:22       ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2015-08-13 12:16 UTC (permalink / raw)
  To: Zsh Users

On Thu, 13 Aug 2015 12:45:55 +0100
Dominik Vogt <vogt@linux.vnet.ibm.com> wrote:
> Sort of works for me after "setopt histexpand".  Unfortunately, is
> saves "./foo" in the history instead of "./!:$".  It looks like
> there's no option to have the unexpanded string stored in the
> history?

No, that's not how history works.  It's hard-wired to work only
immediately on input, before anything else happens, and pass the
processed output to the rest of the shell.  It's not possible to
get it to behave any other way.

You can do this...

% zmodload zsh/parameter
% echo foo; print ./${${(z)history[$HISTCMD]}[-4]
foo
./foo

in which:

$history[$HISTCMD] is the full current history line.

(z) splits it into words using shell syntax.  ";" is a word here, too.

Hence "foo" is the fourth word from the right, [-4].

I would have thought a function with a single argument was much simpler...

pws


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

* Re: "expanding" to copy-earlier-word
  2015-08-13 12:16     ` Peter Stephenson
@ 2015-08-13 16:22       ` Bart Schaefer
  2015-08-13 16:48         ` Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2015-08-13 16:22 UTC (permalink / raw)
  To: Peter Stephenson, Zsh Users

On Aug 13,  1:16pm, Peter Stephenson wrote:
}
} I would have thought a function with a single argument was much simpler...

Er, do you mean we should have a function instead of $history[HISTCMD] ?

-- 
Barton E. Schaefer


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

* Re: "expanding" to copy-earlier-word
  2015-08-13 16:22       ` Bart Schaefer
@ 2015-08-13 16:48         ` Peter Stephenson
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2015-08-13 16:48 UTC (permalink / raw)
  To: Zsh Users

On Thu, 13 Aug 2015 09:22:09 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Aug 13,  1:16pm, Peter Stephenson wrote:
> }
> } I would have thought a function with a single argument was much simpler...
> 
> Er, do you mean we should have a function instead of $history[HISTCMD] ?

No, I mean Dominik is probably a lot better off doing

fn()
{
  make $1
  ./$1
}

or something such.

Arguably zsh/parmeters could provide more detail of the current command line.

pws


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

end of thread, other threads:[~2015-08-13 16:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-13  9:57 "expanding" to copy-earlier-word Dominik Vogt
2015-08-13  9:59 ` Dominik Vogt
2015-08-13 10:24 ` Peter Stephenson
2015-08-13 11:45   ` Dominik Vogt
2015-08-13 12:16     ` Peter Stephenson
2015-08-13 16:22       ` Bart Schaefer
2015-08-13 16:48         ` Peter Stephenson

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