zsh-workers
 help / color / mirror / code / Atom feed
* implicit previous command, only state what should change
@ 2014-07-13 22:06 Emanuel Berg
  2014-07-13 22:24 ` Frank Terbeck
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Emanuel Berg @ 2014-07-13 22:06 UTC (permalink / raw)
  To: zsh-workers

Let's say I write:

$ grep user-home-directory *.el

After that, I realize that it should have been
`user-emacs-directory'.

Is there a way to tell zsh to "execute the previous
command again, only substitute the first argument for
user-emacs-directory"? Like

$ !!:1->user-emacs-directory

if you see what I mean.

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-13 22:06 implicit previous command, only state what should change Emanuel Berg
@ 2014-07-13 22:24 ` Frank Terbeck
  2014-07-13 22:50   ` Emanuel Berg
  2014-07-15  6:46 ` Bart Schaefer
  2014-07-15 16:58 ` Peter Stephenson
  2 siblings, 1 reply; 15+ messages in thread
From: Frank Terbeck @ 2014-07-13 22:24 UTC (permalink / raw)
  To: zsh-workers

Emanuel Berg wrote:
> $ grep user-home-directory *.el
>
> After that, I realize that it should have been
> `user-emacs-directory'.
>
> Is there a way to tell zsh to "execute the previous
> command again, only substitute the first argument for
> user-emacs-directory"? Like
>
> $ !!:1->user-emacs-directory

Like this:

% ^-home-^-emacs-


Regards, Frank


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

* Re: implicit previous command, only state what should change
  2014-07-13 22:24 ` Frank Terbeck
@ 2014-07-13 22:50   ` Emanuel Berg
  2014-07-14  2:08     ` Mikael Magnusson
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg @ 2014-07-13 22:50 UTC (permalink / raw)
  To: zsh-workers

Frank Terbeck <ft@bewatermyfriend.org> writes:

>> $ grep user-home-directory *.el
>>
>> After that, I realize that it should have been
>> `user-emacs-directory'.
>>
>> Is there a way to tell zsh to "execute the previous
>> command again, only substitute the first argument for
>> user-emacs-directory"? Like
>>
>> $ !!:1->user-emacs-directory
>
> Like this:
>
> % ^-home-^-emacs-

I seems that is substitution of the first match sort of
like the sed substitution command without the g option,
which actually for this example is much better than my
suggestion (because only part of the argument is
incorrect), so it is great to know, yes.

Still, one can imagine a case when the whole argument
is wrong, and for a long argument it would be better to
just be able to refer to it and state the replacement.

I tried ^!:1^replacement but that wasn't it.

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-13 22:50   ` Emanuel Berg
@ 2014-07-14  2:08     ` Mikael Magnusson
  2014-07-14 13:09       ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: Mikael Magnusson @ 2014-07-14  2:08 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: zsh workers

On 14 July 2014 00:50, Emanuel Berg <embe8573@student.uu.se> wrote:
> Frank Terbeck <ft@bewatermyfriend.org> writes:
>
>>> $ grep user-home-directory *.el
>>>
>>> After that, I realize that it should have been
>>> `user-emacs-directory'.
>>>
>>> Is there a way to tell zsh to "execute the previous
>>> command again, only substitute the first argument for
>>> user-emacs-directory"? Like
>>>
>>> $ !!:1->user-emacs-directory
>>
>> Like this:
>>
>> % ^-home-^-emacs-
>
> I seems that is substitution of the first match sort of
> like the sed substitution command without the g option,
> which actually for this example is much better than my
> suggestion (because only part of the argument is
> incorrect), so it is great to know, yes.
>
> Still, one can imagine a case when the whole argument
> is wrong, and for a long argument it would be better to
> just be able to refer to it and state the replacement.
>
> I tried ^!:1^replacement but that wasn't it.
>
> --
> underground experts united

I would just navigate to the line and word, delete-whole-word-match,
and type the new argument there. (You'll need to autoload, zle -N and
bindkey that to something).

-- 
Mikael Magnusson


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

* Re: implicit previous command, only state what should change
  2014-07-14  2:08     ` Mikael Magnusson
@ 2014-07-14 13:09       ` Emanuel Berg
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2014-07-14 13:09 UTC (permalink / raw)
  To: zsh-workers

Mikael Magnusson <mikachu@gmail.com> writes:

> I would just navigate to the line and word,
> delete-whole-word-match, and type the new argument
> there. (You'll need to autoload, zle -N and bindkey
> that to something).

Yeah, I thought this was something already in. But if
it isn't I'll do it one way or another, of course.

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-13 22:06 implicit previous command, only state what should change Emanuel Berg
  2014-07-13 22:24 ` Frank Terbeck
@ 2014-07-15  6:46 ` Bart Schaefer
  2014-07-15 15:42   ` Emanuel Berg
  2014-07-15 16:58 ` Peter Stephenson
  2 siblings, 1 reply; 15+ messages in thread
From: Bart Schaefer @ 2014-07-15  6:46 UTC (permalink / raw)
  To: Emanuel Berg, zsh-workers

To add to what's already been said ...

On Jul 14, 12:06am, Emanuel Berg wrote:
}
} Is there a way to tell zsh to "execute the previous
} command again, only substitute the first argument for
} user-emacs-directory"? Like
} 
} $ !!:1->user-emacs-directory

There's no shorthand quite like for arbitrary word positions.  To drop
just the *last* word, you can use !!:-

However, you can always do the obvious:

zsh% !:0 user-emacs-directory !:2*

Or for e.g. the third word:

zsh% !:0-2 new-third-word !:4*

The default event is the previous command so you don't need to double
the "!" -- but note that subsequent "!" refer to the same command as
the preceding reference. So if you want to swap the second word of the
fifth preceding command:

zsh% !-5:0-1 new-second-word !:3* 

There, because !-5 has already selected the fifth previous command, !:3*
selects the third and remaining words of that same command.

You can automate this for some simple cases (the following assumes you
are using the default non-vi key bindings):

bindkey -s ^X1 '^U!!:0 \e!^  !!:2*^X^X\e!'
bindkey -s ^X2 '^U!!:0-1 \e!^  !!:3*^X^X\e!'
bindkey -s ^X3 '^U!!:0-2 \e!^  !!:4*^X^X\e!'
bindkey -s ^X4 '^U!!:0-3 \e!^  !!:5*^X^X\e!'
bindkey -s ^X5 '^U!!:0-4 \e!^  !!:6*^X^X\e!'

With this, typing ctrl+x 1 will re-insert the previous line with the first
argument omitted, leaving the cursor where the new first argument should
be typed.  Similarly for ctrl+x 2 through 5, though they don't work quite
ideally if there are no arguments following the one you're replacing (try
it to see what I mean).


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

* Re: implicit previous command, only state what should change
  2014-07-15  6:46 ` Bart Schaefer
@ 2014-07-15 15:42   ` Emanuel Berg
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2014-07-15 15:42 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

Bart Schaefer <schaefer@brasslantern.com> writes:

> zsh% !:0 user-emacs-directory !:2*

Yes, only, if the other arguments (and the command) are
short or medium-length words I'd say it is in most
cases faster and more comfortable to just retype them.

> You can automate this for some simple cases (the
> following assumes you are using the default non-vi
> key bindings):
>
> bindkey -s ^X1 '^U!!:0 \e!^ !!:2*^X^X\e!'  bindkey -s
> ^X2 '^U!!:0-1 \e!^ !!:3*^X^X\e!'  bindkey -s ^X3
> ^U!!:0-2 \e!^ !!:4*^X^X\e!'  bindkey -s ^X4 '^U!!:0-3
> \e!^ !!:5*^X^X\e!'  bindkey -s ^X5 '^U!!:0-4 \e!^
> !!:6*^X^X\e!'
>
> With this, typing ctrl+x 1 will re-insert the
> previous line with the first argument omitted,
> leaving the cursor where the new first argument
> should be typed.  Similarly for ctrl+x 2 through 5,
> though they don't work quite ideally if there are no
> arguments following the one you're replacing (try it
> to see what I mean).

OK, get back to you.

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-13 22:06 implicit previous command, only state what should change Emanuel Berg
  2014-07-13 22:24 ` Frank Terbeck
  2014-07-15  6:46 ` Bart Schaefer
@ 2014-07-15 16:58 ` Peter Stephenson
  2014-07-16  0:36   ` Emanuel Berg
  2 siblings, 1 reply; 15+ messages in thread
From: Peter Stephenson @ 2014-07-15 16:58 UTC (permalink / raw)
  To: zsh-workers

On Mon, 14 Jul 2014 00:06:46 +0200
Emanuel Berg <embe8573@student.uu.se> wrote:
> Let's say I write:
>
> $ grep user-home-directory *.el
>
> After that, I realize that it should have been
> `user-emacs-directory'.
>
> Is there a way to tell zsh to "execute the previous
> command again, only substitute the first argument for
> user-emacs-directory"?

The read-from-minibufer change is a bug fix: NUMERIC was being
propagated down to the first command read within the recursive edit.
I suspect that might be regarded as a bug in recursive-edit.

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 2fcfbbd..4ee404c 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -2328,6 +2328,31 @@ The name is a slight misnomer, as in fact the shell's own minibuffer is
 not used.  Hence it is still possible to call tt(executed-named-cmd) and
 similar functions while reading a value.
 )
+tindex(replace-argument)
+tindex(replace-argument-edit)
+item(tt(replace-argument), tt(replace-argument-edit))
+(
+The function tt(replace-argument) can be used to replace a command
+line argument in the current command line or, if the current command
+line is empty, in the last command line executed (the new command line
+is not executed).  Arguments are as delimited by standard shell syntax,
+
+If a numeric argument is given, that specifies the argument to be
+replaced.  0 means the command name, as in history expansion.
+
+If no numeric argument is given, the current argument is replaced;
+this is the last argument if the previous history line is being used.
+
+The function prompts for a replacement argument.
+
+If the widget contains the string tt(edit), for example is defined as
+
+example(zle -N replace-argument-edit replace-argument)
+
+then the function presents the current value of the argument for
+editing, otherwise the editing buffer for the replacement is
+initially empty.
+)
 tindex(replace-string)
 tindex(replace-string-again)
 tindex(replace-pattern)
diff --git a/Functions/Zle/read-from-minibuffer b/Functions/Zle/read-from-minibuffer
index 57e9268..19d1bd5 100644
--- a/Functions/Zle/read-from-minibuffer
+++ b/Functions/Zle/read-from-minibuffer
@@ -39,6 +39,8 @@ local readprompt="$1" lbuf_init="$2" rbuf_init="$3"
     read -k $keys
     stat=$?
   else
+    local NUMERIC
+    unset NUMERIC
     zle recursive-edit -K main
     stat=$?
     (( stat )) || REPLY=$BUFFER
diff --git a/Functions/Zle/replace-argument b/Functions/Zle/replace-argument
new file mode 100644
index 0000000..b43fc39
--- /dev/null
+++ b/Functions/Zle/replace-argument
@@ -0,0 +1,46 @@
+# Replace an argument to a command, delimited by normal shell syntax.
+# Prompts for the replacement.
+# With no numeric argument, replace the current argument.
+# With a numeric argument, replace that argument: 0 = command word,
+# as in history expansion.
+# If editing buffer is empty, use previous history line.
+
+autoload -Uz split-shell-arguments read-from-minibuffer
+
+if (( ${#BUFFER} == 0 )); then
+  (( HISTNO-- ))
+  CURSOR=${#BUFFER}
+fi
+
+local widget=$WIDGET
+integer numeric cursor=CURSOR
+if (( ${+NUMERIC} )); then
+  numeric=$NUMERIC
+else
+  numeric=-1
+fi
+local reply REPLY REPLY2
+integer index
+split-shell-arguments
+
+if (( numeric >= 0 )); then
+  index=$(( 2 + 2*numeric ))
+else
+  index=$((REPLY & ~1 ))
+fi
+
+local edit
+if [[ $widget = *edit* ]]; then
+  edit=$reply[$index]
+fi
+read-from-minibuffer "Replace $reply[$index] with: " $edit || return 1
+
+integer diff=$(( ${#REPLY} - ${#reply[$index]} ))
+reply[$index]=$REPLY
+
+BUFFER=${(j..)reply}
+if (( cursor > REPLY2 )); then
+  (( CURSOR = cursor + diff ))
+else
+  (( CURSOR = REPLY2 ))
+fi

pws


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

* Re: implicit previous command, only state what should change
  2014-07-15 16:58 ` Peter Stephenson
@ 2014-07-16  0:36   ` Emanuel Berg
  2014-07-16 13:54     ` Peter Stephenson
  0 siblings, 1 reply; 15+ messages in thread
From: Emanuel Berg @ 2014-07-16  0:36 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson <p.stephenson@samsung.com> writes:

>> Let's say I write: $ grep user-home-directory *.el
>> After that, I realize that it should have been
>> user-emacs-directory'.  Is there a way to tell zsh
>> to "execute the previous command again, only
>> substitute the first argument for
>> user-emacs-directory"?
>
> The read-from-minibufer change is a bug fix: NUMERIC
> was being propagated down to the first command read
> within the recursive edit.  I suspect that might be
> regarded as a bug in recursive-edit.

???

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-16  0:36   ` Emanuel Berg
@ 2014-07-16 13:54     ` Peter Stephenson
  2014-07-16 20:45       ` Emanuel Berg
  0 siblings, 1 reply; 15+ messages in thread
From: Peter Stephenson @ 2014-07-16 13:54 UTC (permalink / raw)
  Cc: zsh-workers

On Wed, 16 Jul 2014 02:36:03 +0200
Emanuel Berg <embe8573@student.uu.se> wrote:
> Peter Stephenson <p.stephenson@samsung.com> writes:
> 
> >> Let's say I write: $ grep user-home-directory *.el
> >> After that, I realize that it should have been
> >> user-emacs-directory'.  Is there a way to tell zsh
> >> to "execute the previous command again, only
> >> substitute the first argument for
> >> user-emacs-directory"?
> >
> > The read-from-minibufer change is a bug fix: NUMERIC
> > was being propagated down to the first command read
> > within the recursive edit.  I suspect that might be
> > regarded as a bug in recursive-edit.
> 
> ???

The bit you quote is a reference to the details of the patch, which you
can ignore unless you're particularly interested in the implementation.

What I should have said but didn't was, here's a new function called
replace-argument that does that.  That was in the body of the patch, but
unless you're programmed to look at zsh code patches when a new message
arrives you wouldn't notice.

I'll commit it when I have a couple of minutes spare.

pws


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

* Re: implicit previous command, only state what should change
  2014-07-16 13:54     ` Peter Stephenson
@ 2014-07-16 20:45       ` Emanuel Berg
  2014-07-16 20:53         ` Bart Schaefer
  2014-07-17  8:17         ` Peter Stephenson
  0 siblings, 2 replies; 15+ messages in thread
From: Emanuel Berg @ 2014-07-16 20:45 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson <p.stephenson@samsung.com> writes:

> The bit you quote is a reference to the details of
> the patch, which you can ignore unless you're
> particularly interested in the implementation.
>
> What I should have said but didn't was, here's a new
> function called replace-argument that does that.
> That was in the body of the patch, but unless you're
> programmed to look at zsh code patches when a new
> message arrives you wouldn't notice.

Aha, yes, now I see it. I have configured Gnus to
auto-fill messages, which works great for text, but
scrambles code, so I didn't see it (it looks like a PGP
key or something).

Are you suggesting I apply it to the zsh source and
recompile?

If so, how do I use it after that (i.e., what
notation)?

When I upgrade zsh, wont it disappear?

> I'll commit it when I have a couple of minutes spare.

You mean to the official zsh source or do you have your
own branch?

/The hairdresser

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-16 20:45       ` Emanuel Berg
@ 2014-07-16 20:53         ` Bart Schaefer
  2014-07-16 21:08           ` Emanuel Berg
  2014-07-17  8:17         ` Peter Stephenson
  1 sibling, 1 reply; 15+ messages in thread
From: Bart Schaefer @ 2014-07-16 20:53 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: zsh-workers

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

On Jul 16, 2014 1:45 PM, "Emanuel Berg" <embe8573@student.uu.se> wrote:
>
> Aha, yes, now I see it. I have configured Gnus to
> auto-fill messages, which works great for text, but
> scrambles code, so I didn't see it (it looks like a PGP
> key or something).

You're asking these questions on the zsh-workers list, so you should sort
of expect to see code.  If you're not interested in code, hop on over to
zsh-users instead (which isn't entirely code-free, but you'll get less of
it).

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

* Re: implicit previous command, only state what should change
  2014-07-16 20:53         ` Bart Schaefer
@ 2014-07-16 21:08           ` Emanuel Berg
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2014-07-16 21:08 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer <schaefer@brasslantern.com> writes:

>> Aha, yes, now I see it. I have configured Gnus to
>> auto-fill messages, which works great for text, but
>> scrambles code, so I didn't see it (it looks like a
>> PGP key or something).
>
> You're asking these questions on the zsh-workers
> list, so you should sort of expect to see code. If
> you're not interested in code, hop on over to
> zsh-users instead (which isn't entirely code-free,
> but you'll get less of it).

If I'm not interested in code?! :-o

Here is some 35 or so files of zsh that I wrote, and
I'm not even that into shell programming, as I do most
things from Emacs with Elisp:

http://user.it.uu.se/~embe8573/conf/.zsh/

Of course there is code on the zsh listbots!

-- 
underground experts united


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

* Re: implicit previous command, only state what should change
  2014-07-16 20:45       ` Emanuel Berg
  2014-07-16 20:53         ` Bart Schaefer
@ 2014-07-17  8:17         ` Peter Stephenson
  2014-07-17 13:01           ` Emanuel Berg
  1 sibling, 1 reply; 15+ messages in thread
From: Peter Stephenson @ 2014-07-17  8:17 UTC (permalink / raw)
  To: zsh-workers

On Wed, 16 Jul 2014 22:45:35 +0200
Emanuel Berg <embe8573@student.uu.se> wrote:
> > I'll commit it when I have a couple of minutes spare.
> 
> You mean to the official zsh source or do you have your
> own branch?

It'll appear in the zsh source, so you can just wait.  It's just a
couple of hundred bytes of disk space, so we can err on the generous
side with contributed functions.

There are (brief) generic instructions in the ZLE Functions section of
the zshcontrib manual as to how to use this sort of thing.

pws


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

* Re: implicit previous command, only state what should change
  2014-07-17  8:17         ` Peter Stephenson
@ 2014-07-17 13:01           ` Emanuel Berg
  0 siblings, 0 replies; 15+ messages in thread
From: Emanuel Berg @ 2014-07-17 13:01 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson <p.stephenson@samsung.com> writes:

> It'll appear in the zsh source, so you can just wait.
> It's just a couple of hundred bytes of disk space, so
> we can err on the generous side with contributed
> functions.
>
> There are (brief) generic instructions in the ZLE
> Functions section of the zshcontrib manual as to how
> to use this sort of thing.

OK! Thank you.

-- 
underground experts united


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

end of thread, other threads:[~2014-07-17 13:01 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-13 22:06 implicit previous command, only state what should change Emanuel Berg
2014-07-13 22:24 ` Frank Terbeck
2014-07-13 22:50   ` Emanuel Berg
2014-07-14  2:08     ` Mikael Magnusson
2014-07-14 13:09       ` Emanuel Berg
2014-07-15  6:46 ` Bart Schaefer
2014-07-15 15:42   ` Emanuel Berg
2014-07-15 16:58 ` Peter Stephenson
2014-07-16  0:36   ` Emanuel Berg
2014-07-16 13:54     ` Peter Stephenson
2014-07-16 20:45       ` Emanuel Berg
2014-07-16 20:53         ` Bart Schaefer
2014-07-16 21:08           ` Emanuel Berg
2014-07-17  8:17         ` Peter Stephenson
2014-07-17 13:01           ` Emanuel Berg

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