zsh-users
 help / color / mirror / code / Atom feed
* Interactive search on the command line?
@ 2015-11-26  8:04 Dominik Vogt
  2015-11-26  8:20 ` Mikael Magnusson
                   ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: Dominik Vogt @ 2015-11-26  8:04 UTC (permalink / raw)
  To: Zsh Users

Is there a way to do an interactive search (or at least a
non-interactive string search) in the command line editor?  I
often have extremely long gcc command lines where I want to delete
an option or do some editing in the middle of the command line,
and moving the cursor to the interesting spot is quite tedious.

Ciao

Dominik ^_^  ^_^

-- 

Dominik Vogt
IBM Germany


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

* Re: Interactive search on the command line?
  2015-11-26  8:04 Interactive search on the command line? Dominik Vogt
@ 2015-11-26  8:20 ` Mikael Magnusson
  2015-11-27  7:03 ` lilydjwg
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 15+ messages in thread
From: Mikael Magnusson @ 2015-11-26  8:20 UTC (permalink / raw)
  To: vogt, Zsh Users

On Thu, Nov 26, 2015 at 9:04 AM, Dominik Vogt <vogt@linux.vnet.ibm.com> wrote:
> Is there a way to do an interactive search (or at least a
> non-interactive string search) in the command line editor?  I
> often have extremely long gcc command lines where I want to delete
> an option or do some editing in the middle of the command line,
> and moving the cursor to the interesting spot is quite tedious.

You can use ctrl-r for this. (eg, history-incremental-*-search-*).

Another useful trick on long command lines is to type alt-300 or some
suitably large number before pressing left/right.

-- 
Mikael Magnusson


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

* Re: Interactive search on the command line?
  2015-11-26  8:04 Interactive search on the command line? Dominik Vogt
  2015-11-26  8:20 ` Mikael Magnusson
@ 2015-11-27  7:03 ` lilydjwg
  2015-11-27  7:34 ` Daniel Shahaf
  2015-11-29 21:46 ` Scott Frazer
  3 siblings, 0 replies; 15+ messages in thread
From: lilydjwg @ 2015-11-27  7:03 UTC (permalink / raw)
  To: zsh-users

On Thu, Nov 26, 2015 at 09:04:00AM +0100, Dominik Vogt wrote:
> Is there a way to do an interactive search (or at least a
> non-interactive string search) in the command line editor?  I
> often have extremely long gcc command lines where I want to delete
> an option or do some editing in the middle of the command line,
> and moving the cursor to the interesting spot is quite tedious.

I will press Ctrl-X Ctrl-E to bring up Vim to edit the command, then
I have the full capacity of my Vim. My setting is:

autoload -Uz edit-command-line
zle -N edit-command-line
bindkey '^X^E' edit-command-line

Also, moving by words (Alt-b/f) is a little faster. I don't use counts
very much because I always get the wrong count.

If you like, you can use the mouse: http://stchaz.free.fr/mouse.zsh

-- 
Best regards,
lilydjwg


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

* Re: Interactive search on the command line?
  2015-11-26  8:04 Interactive search on the command line? Dominik Vogt
  2015-11-26  8:20 ` Mikael Magnusson
  2015-11-27  7:03 ` lilydjwg
@ 2015-11-27  7:34 ` Daniel Shahaf
  2015-11-29 21:46 ` Scott Frazer
  3 siblings, 0 replies; 15+ messages in thread
From: Daniel Shahaf @ 2015-11-27  7:34 UTC (permalink / raw)
  To: Dominik Vogt; +Cc: Zsh Users

Dominik Vogt wrote on Thu, Nov 26, 2015 at 09:04:00 +0100:
> Is there a way to do an interactive search (or at least a
> non-interactive string search) in the command line editor?  I
> often have extremely long gcc command lines where I want to delete
> an option or do some editing in the middle of the command line,
> and moving the cursor to the interesting spot is quite tedious.

There is vi-find-next-char, which is bound by default to 'f' in vi or
'^X^F' in emacs.

Also, you could do:
    % () { gcc -Wall -std=c99 "$@" foo.c } -my-argument-here
and edit the last word.

Also, there is the ^foo^bar history expansion with replacement:

    % echo foo
    foo
    % ^foo^bar
    echo bar
    bar

Daniel


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

* Re: Interactive search on the command line?
  2015-11-26  8:04 Interactive search on the command line? Dominik Vogt
                   ` (2 preceding siblings ...)
  2015-11-27  7:34 ` Daniel Shahaf
@ 2015-11-29 21:46 ` Scott Frazer
  2015-11-30  3:19   ` Daniel Shahaf
  3 siblings, 1 reply; 15+ messages in thread
From: Scott Frazer @ 2015-11-29 21:46 UTC (permalink / raw)
  To: zsh-users

On 11/26/15 3:04 AM, Dominik Vogt wrote:
> Is there a way to do an interactive search (or at least a
> non-interactive string search) in the command line editor?  I
> often have extremely long gcc command lines where I want to delete
> an option or do some editing in the middle of the command line,
> and moving the cursor to the interesting spot is quite tedious.
>

I wrote something recently that might help, it lets you jump to
anywhere in the command line with just a couple keystrokes:

https://github.com/scfrazer/zsh-jump-target

I'm pretty new to zsh, so I wrote it like I would have in a different
language.  If there is a better way to do things, suggestions are
welcome.

Scott



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

* Re: Interactive search on the command line?
  2015-11-29 21:46 ` Scott Frazer
@ 2015-11-30  3:19   ` Daniel Shahaf
  2015-12-02 13:15     ` Scott Frazer
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel Shahaf @ 2015-11-30  3:19 UTC (permalink / raw)
  To: Scott Frazer; +Cc: zsh-users

Scott Frazer wrote on Sun, Nov 29, 2015 at 16:46:35 -0500:
> On 11/26/15 3:04 AM, Dominik Vogt wrote:
> >Is there a way to do an interactive search (or at least a
> >non-interactive string search) in the command line editor?  I
> >often have extremely long gcc command lines where I want to delete
> >an option or do some editing in the middle of the command line,
> >and moving the cursor to the interesting spot is quite tedious.
> >
> 
> I wrote something recently that might help, it lets you jump to
> anywhere in the command line with just a couple keystrokes:
> 
> https://github.com/scfrazer/zsh-jump-target

Thanks, I'll install that.

> I'm pretty new to zsh, so I wrote it like I would have in a different
> language.  If there is a better way to do things, suggestions are
> welcome.

I wouldn't know you were new to zsh if you didn't say so; the code looks
as good as anyone's.  The one thing I would suggest is to use the
${BUFFER[(i)$char]} syntax in the 'for' loop, which should translate to
a strchr() or strstr() call at the C level.  (There's also the
${(ps:$char:)BUFFER} syntax, but I'm not sure it gains you anything.)

Also, three minor points:

- With recent zsh, WARN_CREATE_GLOBAL complains:
    (anon):1: scalar parameter ZSH_JUMP_TARGET_CHOICES created globally in function (anon)
    (anon):2: scalar parameter ZSH_JUMP_TARGET_STYLE created globally in function (anon)
  The fix is to declare these parameters either global ('typeset -g') or 'local'.

- You could use 'region_highlight+=("foo bar baz")' to append to the array.

- You might use an 'always' block to restore $orig_region_highlight.

Thanks again!

Daniel

P.S. Perhaps you could throw a LICENSE file into that repository?


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

* Re: Interactive search on the command line?
  2015-11-30  3:19   ` Daniel Shahaf
@ 2015-12-02 13:15     ` Scott Frazer
  2015-12-03 23:39       ` Daniel Shahaf
  0 siblings, 1 reply; 15+ messages in thread
From: Scott Frazer @ 2015-12-02 13:15 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: zsh-users

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

On Sun, Nov 29, 2015 at 10:19 PM, Daniel Shahaf <d.s@daniel.shahaf.name>
wrote:

> I wouldn't know you were new to zsh if you didn't say so; the code looks
> as good as anyone's.  The one thing I would suggest is to use the
> ${BUFFER[(i)$char]} syntax in the 'for' loop, which should translate to
> a strchr() or strstr() call at the C level.  (There's also the
> ${(ps:$char:)BUFFER} syntax, but I'm not sure it gains you anything.)
>
>
I tried the (i) thing but the function stopped working.  I couldn't find
what it was supposed
to do in the zsh manual, only something about use as a flag for
case-insensitive search
which doesn't seem relevant.  How do people debug these types of things?


> Also, three minor points:
>
> - With recent zsh, WARN_CREATE_GLOBAL complains:
>     (anon):1: scalar parameter ZSH_JUMP_TARGET_CHOICES created globally in
> function (anon)
>     (anon):2: scalar parameter ZSH_JUMP_TARGET_STYLE created globally in
> function (anon)
>   The fix is to declare these parameters either global ('typeset -g') or
> 'local'.
>
> - You could use 'region_highlight+=("foo bar baz")' to append to the array.
>
> - You might use an 'always' block to restore $orig_region_highlight.
>
> P.S. Perhaps you could throw a LICENSE file into that repository?
>
>
Thanks for the feedback, I fixed all these things.

Scott

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

* Re: Interactive search on the command line?
  2015-12-02 13:15     ` Scott Frazer
@ 2015-12-03 23:39       ` Daniel Shahaf
  2015-12-04  0:40         ` Ray Andrews
                           ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Daniel Shahaf @ 2015-12-03 23:39 UTC (permalink / raw)
  To: Scott Frazer; +Cc: zsh-users

Scott Frazer wrote on Wed, Dec 02, 2015 at 08:15:57 -0500:
> On Sun, Nov 29, 2015 at 10:19 PM, Daniel Shahaf <d.s@daniel.shahaf.name>
> wrote:
> 
> > I wouldn't know you were new to zsh if you didn't say so; the code looks
> > as good as anyone's.  The one thing I would suggest is to use the
> > ${BUFFER[(i)$char]} syntax in the 'for' loop, which should translate to
> > a strchr() or strstr() call at the C level.  (There's also the
> > ${(ps:$char:)BUFFER} syntax, but I'm not sure it gains you anything.)
> >
> >
> I tried the (i) thing but the function stopped working.  I couldn't
> find what it was supposed to do in the zsh manual, only something
> about use as a flag for case-insensitive search which doesn't seem
> relevant.

A parenthesized "i" after an opening brace of parameter substitution
means "sort case-insensitively":

    % a=(Foo bar) ; print ${(oi)a}
    bar Foo

A parenthesized "i" after a subscript's bracket is a strstr() flag:

    % s=foobar x=b
    % print ${s[(i)$x]}
    4

The latter is documented in "Subscript Flags" in zshparam(1).  (It took
me a little while to find that; I checked zshexpn(1) first.)

So, you could do something like this (using the (b::) subscript flag as
well):

    % () {
        local haystack="$1"
        local needle="a"
        integer idx
        while (( idx = ${haystack[(ib:idx+1:)${needle}]} ))
              (( idx <= $#haystack ))
        do
          print -r - $haystack[idx,idx+$#needle-1]
        done
      } foobar
    a
    % 

> How do people debug these types of things?

You mean, how people look up flags?  Just type ": ${(" and then press
<TAB>.

> 
> > Also, three minor points:
> >
> > - With recent zsh, WARN_CREATE_GLOBAL complains:
> >     (anon):1: scalar parameter ZSH_JUMP_TARGET_CHOICES created globally in
> > function (anon)
> >     (anon):2: scalar parameter ZSH_JUMP_TARGET_STYLE created globally in
> > function (anon)
> >   The fix is to declare these parameters either global ('typeset -g') or
> > 'local'.
> >
> > - You could use 'region_highlight+=("foo bar baz")' to append to the array.
> >
> > - You might use an 'always' block to restore $orig_region_highlight.
> >
> > P.S. Perhaps you could throw a LICENSE file into that repository?
> >
> >
> Thanks for the feedback, I fixed all these things.

Thanks, LGTM.

Daniel


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

* Re: Interactive search on the command line?
  2015-12-03 23:39       ` Daniel Shahaf
@ 2015-12-04  0:40         ` Ray Andrews
  2015-12-04  0:50         ` Ray Andrews
  2015-12-04  0:59         ` Bart Schaefer
  2 siblings, 0 replies; 15+ messages in thread
From: Ray Andrews @ 2015-12-04  0:40 UTC (permalink / raw)
  To: zsh-users

On 12/03/2015 03:39 PM, Daniel Shahaf wrote:
> You mean, how people look up flags? Just type ": ${(" and then press 
> <TAB>. 

Miraculous.  I had no idea you could do that.  Where can I read up on 
this sort of thing?


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

* Re: Interactive search on the command line?
  2015-12-03 23:39       ` Daniel Shahaf
  2015-12-04  0:40         ` Ray Andrews
@ 2015-12-04  0:50         ` Ray Andrews
  2015-12-04  0:59         ` Bart Schaefer
  2 siblings, 0 replies; 15+ messages in thread
From: Ray Andrews @ 2015-12-04  0:50 UTC (permalink / raw)
  To: zsh-users

On 12/03/2015 03:39 PM, Daniel Shahaf wrote:
> Scott Frazer wrote on Wed, Dec 02, 2015 at 08:15:57 -0500:
>> I tried the (i) thing but the function stopped working. I couldn't 
>> find what it was supposed to do in the zsh manual, only something 
>> about use as a flag for case-insensitive search which doesn't seem 
>> relevant.

Scott, if you're new to this, you hafta be careful about the 
context--the same flag/modifier/qualifier character can mean totally 
different things in different contexts, and it's easy to get confused.  
I wish there was some sort of overview document that primed one as to 
whether this or that was a glob qualifier or an expansion flag or one of 
several other entire families of modifiers, but there isn't one.  Be 
careful.


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

* Re: Interactive search on the command line?
  2015-12-03 23:39       ` Daniel Shahaf
  2015-12-04  0:40         ` Ray Andrews
  2015-12-04  0:50         ` Ray Andrews
@ 2015-12-04  0:59         ` Bart Schaefer
  2015-12-04 20:56           ` Scott Frazer
  2015-12-05 10:00           ` Daniel Shahaf
  2 siblings, 2 replies; 15+ messages in thread
From: Bart Schaefer @ 2015-12-04  0:59 UTC (permalink / raw)
  To: zsh-users

On Dec 3, 11:39pm, Daniel Shahaf wrote:
}
} So, you could do something like this (using the (b::) subscript flag as
} well):
} 
}     % () {
}         local haystack="$1"
}         local needle="a"
}         integer idx
}         while (( idx = ${haystack[(ib:idx+1:)${needle}]} ))
}               (( idx <= $#haystack ))
}         do
}           print -r - $haystack[idx,idx+$#needle-1]
}         done
}       } foobar
}     a
}     % 

This example might be a little clearer if we show the index where the
match was found and use a match pattern longer than one character:

% () {
    local haystack=$1 needle=$2
    integer idx
    while idx=${haystack[(ib:idx+1:)$needle]}
	  (( idx <= $#haystack ))
    do
      print -r - $idx $haystack[idx,idx+$#needle-1]
    done
  } foobar 'o??'
2 oob
3 oba
% 

(I also removed all redundant quotes and braces, since the original
would only work in native zsh emulation anyway.)


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

* Re: Interactive search on the command line?
  2015-12-04  0:59         ` Bart Schaefer
@ 2015-12-04 20:56           ` Scott Frazer
  2015-12-05 10:00           ` Daniel Shahaf
  1 sibling, 0 replies; 15+ messages in thread
From: Scott Frazer @ 2015-12-04 20:56 UTC (permalink / raw)
  To: zsh-users

On 12/3/15 7:59 PM, Bart Schaefer wrote:
> On Dec 3, 11:39pm, Daniel Shahaf wrote:
> }
> } So, you could do something like this (using the (b::) subscript flag as
> } well):
> }
> }     % () {
> }         local haystack="$1"
> }         local needle="a"
> }         integer idx
> }         while (( idx = ${haystack[(ib:idx+1:)${needle}]} ))
> }               (( idx <= $#haystack ))
> }         do
> }           print -r - $haystack[idx,idx+$#needle-1]
> }         done
> }       } foobar
> }     a
> }     %
>
> This example might be a little clearer if we show the index where the
> match was found and use a match pattern longer than one character:
>
> % () {
>      local haystack=$1 needle=$2
>      integer idx
>      while idx=${haystack[(ib:idx+1:)$needle]}
> 	  (( idx <= $#haystack ))
>      do
>        print -r - $idx $haystack[idx,idx+$#needle-1]
>      done
>    } foobar 'o??'
> 2 oob
> 3 oba
> %
>
> (I also removed all redundant quotes and braces, since the original
> would only work in native zsh emulation anyway.)
>

Oh, it's a reverse lookup instead of walking through the entire
BUFFER.  Neat!  Code updated.

Scott



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

* Re: Interactive search on the command line?
  2015-12-04  0:59         ` Bart Schaefer
  2015-12-04 20:56           ` Scott Frazer
@ 2015-12-05 10:00           ` Daniel Shahaf
  2015-12-05 16:38             ` Mikael Magnusson
  1 sibling, 1 reply; 15+ messages in thread
From: Daniel Shahaf @ 2015-12-05 10:00 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer wrote on Thu, Dec 03, 2015 at 16:59:50 -0800:
> On Dec 3, 11:39pm, Daniel Shahaf wrote:
> }     % () {
> }         local haystack="$1"
...
> 
> % () {
>     local haystack=$1 needle=$2
...
> (I also removed all redundant quotes and braces, since the original
> would only work in native zsh emulation anyway.)

The quotes are not redundant; they are required for compatibility with
zsh-5.0.8 and older.  (Those releases predate the upgrade of 'local' to
a reserved word, and, without the quotes, would perform word splitting
before the assignment.)


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

* Re: Interactive search on the command line?
  2015-12-05 10:00           ` Daniel Shahaf
@ 2015-12-05 16:38             ` Mikael Magnusson
  2015-12-05 21:07               ` Bart Schaefer
  0 siblings, 1 reply; 15+ messages in thread
From: Mikael Magnusson @ 2015-12-05 16:38 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

On Sat, Dec 5, 2015 at 11:00 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> Bart Schaefer wrote on Thu, Dec 03, 2015 at 16:59:50 -0800:
>> On Dec 3, 11:39pm, Daniel Shahaf wrote:
>> }     % () {
>> }         local haystack="$1"
> ...
>>
>> % () {
>>     local haystack=$1 needle=$2
> ...
>> (I also removed all redundant quotes and braces, since the original
>> would only work in native zsh emulation anyway.)
>
> The quotes are not redundant; they are required for compatibility with
> zsh-5.0.8 and older.  (Those releases predate the upgrade of 'local' to
> a reserved word, and, without the quotes, would perform word splitting
> before the assignment.)

Zsh doesn't wordsplit unquoted parameters and if it ever did, it was
probably before any of us were born. You may be confusing the above
with the $(foo) syntax which does wordsplit when unquoted.

-- 
Mikael Magnusson


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

* Re: Interactive search on the command line?
  2015-12-05 16:38             ` Mikael Magnusson
@ 2015-12-05 21:07               ` Bart Schaefer
  0 siblings, 0 replies; 15+ messages in thread
From: Bart Schaefer @ 2015-12-05 21:07 UTC (permalink / raw)
  To: Zsh Users

On Dec 5,  5:38pm, Mikael Magnusson wrote:
}
} Zsh doesn't wordsplit unquoted parameters and if it ever did, it was
} probably before any of us were born.

Wow, if a lot of you were born before 1992 I'm going to feel really old.

-- 
Barton E. Schaefer


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

end of thread, other threads:[~2015-12-05 21:07 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-26  8:04 Interactive search on the command line? Dominik Vogt
2015-11-26  8:20 ` Mikael Magnusson
2015-11-27  7:03 ` lilydjwg
2015-11-27  7:34 ` Daniel Shahaf
2015-11-29 21:46 ` Scott Frazer
2015-11-30  3:19   ` Daniel Shahaf
2015-12-02 13:15     ` Scott Frazer
2015-12-03 23:39       ` Daniel Shahaf
2015-12-04  0:40         ` Ray Andrews
2015-12-04  0:50         ` Ray Andrews
2015-12-04  0:59         ` Bart Schaefer
2015-12-04 20:56           ` Scott Frazer
2015-12-05 10:00           ` Daniel Shahaf
2015-12-05 16:38             ` Mikael Magnusson
2015-12-05 21:07               ` Bart Schaefer

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