Discussion:
[Grml] zsh history completion via arrow keys
John
2013-01-19 17:34:09 UTC
Permalink
Just discovered?grmlzshrc and am loving it. ?One thing I cannot seem to figure out though is how to enable zsh history completion on up arrow that reads what I have typed and only completes matches? ?For example, I type:

% vim ~/bin ? ? ? ? ? ? ? <<UPARROW>>

The expected behavior is to autocomplete entries that begin with 'vim ~/bin' but that isn't what happens. ?When I hit UPARROW the auto complete lists shows anything that started with 'vim ' which ignores the ~/bin bit.

I am using Arch Linux and the Distro provided?grml-zsh-config package. ?The only configuration I have done is to copy /etc/skel/.zshrc to ~


Thank you!
Frank Terbeck
2013-01-19 21:00:42 UTC
Permalink
Post by John
Just discovered?grmlzshrc and am loving it. ?One thing I cannot seem to figure
out though is how to enable zsh history completion on up arrow that reads what
% vim ~/bin ? ? ? ? ? ? ? <<UPARROW>>
The expected behavior is to autocomplete entries that begin with 'vim ~/bin'
but that isn't what happens. ?When I hit UPARROW the auto complete lists shows
anything that started with 'vim ' which ignores the ~/bin bit.
We actually do have

bindkey '\e[A' up-line-or-search

in there. However, we might need to do this too:

bindkey '\eOA' up-line-or-search

...keyboard handling is something that we want to improve for a long
time. But it's not trivial to get right and especially to convert our
existing bindings, without breaking backwards compatibility (too much).


Report if the additional binding enables that feature for you.

Regards, Frank
John
2013-01-20 10:48:26 UTC
Permalink
Post by Frank Terbeck
Report if the additional binding enables that feature for you.
Hi Frank. ?I added the line to my /etc/zsh/zshrc right under the original but upon opening a new terminal, the?desired?effect is not present.

Starting on line 671:
bindkey '\e[A' ?up-line-or-search ? ? ? # cursor up
bindkey '\eOA' ?up-line-or-search
bindkey '\e[B' ?down-line-or-search ? ? # <ESC>-

If you have other thoughts, I am very interested.
Frank Terbeck
2013-01-20 10:53:48 UTC
Permalink
Post by John
I added the line to my /etc/zsh/zshrc right under the original but
upon opening a new terminal, the?desired?effect is not present.
[...]
Post by John
If you have other thoughts, I am very interested.
In your terminal, at the zsh prompt, hit Control-v followed by the
up-cursor key. What does that insert into the command line?

Regards, Frank
--
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
-- RFC 1925
John
2013-01-20 11:18:52 UTC
Permalink
Post by Frank Terbeck
In your terminal, at the zsh prompt, hit Control-v followed by the
up-cursor key.? What does that insert into the command line?
I recently discovered fishshell.com which has a very nice?implementation?of this feature. ?See:?https://github.com/graysky2/configs/blob/master/shell/.zsh/lib/substring-search.zsh
Frank Terbeck
2013-01-20 11:34:18 UTC
Permalink
John wrote:
[...]
Post by John
I recently discovered fishshell.com which has a very nice?implementation?of
this feature.
Maybe so, but too big to include for my taste. But it looks like that
should work on top of our zshrc just fine. So use it if you like it.

However it only binds to these sequences:

bindkey '\e[A' history-substring-search-up
bindkey '\e[B' history-substring-search-down

So that won't help your problem at hand. (Then again, I just told you in
the other mail how to find out about the actual escape sequence our
terminal emits when you hit the up-cursor key; so you can fix that on
you own now).

Regards, Frank
John
2013-01-22 19:36:45 UTC
Permalink
Post by Frank Terbeck
In your terminal, at the zsh prompt, hit Control-v followed by the
up-cursor key.? What does that insert into the command line?
Dunno how I missed this post. ?The output you requested:

^[[A ? ? ? ? (up arrow)
^[[B ? ? ? ? (down arrow)
Frank Terbeck
2013-01-23 08:43:17 UTC
Permalink
Post by John
Post by Frank Terbeck
In your terminal, at the zsh prompt, hit Control-v followed by the
up-cursor key.? What does that insert into the command line?
^[[A ? ? ? ? (up arrow)
Well, in that case, this binding should be invoked and things should
work:

bindkey '\e[A' ?up-line-or-search


To make sure, what does this yield?

% bindkey '<CTRL-v><Up-Arrow>'

(Press the actual keys, don't just paste that line.)

Regards, Frank
John
2013-01-23 19:59:33 UTC
Permalink
Post by Frank Terbeck
Well, in that case, this binding should be invoked and things should
? bindkey '\e[A' ?up-line-or-search
To make sure, what does this yield?
? % bindkey '<CTRL-v><Up-Arrow>'
(Press the actual keys, don't just paste that line.)
Hello Frank - Here I have removed the fishshell mod that I posted and am back to the modifed grml config. ?Recall that I modified it per your suggestion to add the following to /etc/zsh/zshrc

bindkey '\e[A' ?up-line-or-search ? ? ? # cursor up

bindkey '\eOA' ?up-line-or-search
bindkey '\e[B' ?down-line-or-search ? ? # <ESC>-

Here is the output of the command you asked about:

% bindkey '"^[[A" up-line-or-search

Thank you!
Frank Terbeck
2013-01-23 20:09:51 UTC
Permalink
John wrote:
[...]
Post by John
% bindkey '"^[[A" up-line-or-search
So, `up-line-or-search' is indeed active already. Isn't this what you
asked for? From the manual:

up-line-or-search
Move up a line in the buffer, or if already at the top
line, search backward in the history for a line
beginning with the first word in the buffer.

If called from a function by the zle command with
arguments, the first argument is taken as the string
for which to search, rather than the first word in the
buffer.


Regards, Frank
John
2013-01-23 20:16:01 UTC
Permalink
Post by Frank Terbeck
So, `up-line-or-search' is indeed active already. Isn't this what you
? ? up-line-or-search
? ? ? ? ? Move up a line in the? buffer, or if already at the top
? ? ? ? ? line,? search? backward? in? the? history? for? a? line
? ? ? ? ? beginning with the first word in the buffer.
? ? ? ? ? If? called from? a? function by? the? zle command? with
? ? ? ? ? arguments, the? first argument? is taken as? the string
? ? ? ? ? for which to search, rather? than the first word in the
? ? ? ? ? buffer.
It is active, but its behavior isn't what I'd like... For example:

% vim ~/<<UPARROW>>

Here are the first 4 responses?to the above line:

% vim PKGBUILD
% vim build_time.txt
% ~/.ssh/known_hosts
% ~/bin/wake


I want zsh to only show me responses in my history that begin with 'vim ~/' so in my example above, the first two would not show up at all. ?Is there such a widget (without using that huge fishshell thing).

Thanks!
Frank Terbeck
2013-01-23 20:28:09 UTC
Permalink
John wrote:
[...]
Post by John
% vim ~/<<UPARROW>>
% vim PKGBUILD
% vim build_time.txt
% ~/.ssh/known_hosts
% ~/bin/wake
I want zsh to only show me responses in my history that begin with 'vim ~/' so
in my example above, the first two would not show up at all. ?Is there such a
widget (without using that huge fishshell thing).
Heh. Sorry for not reading your first posting thoroughly enough.
up-line-or-beginning-search, down-line-or-beginning-search
These widgets are similar to the builtin functions
up-line-or-search and down-line-or-search: if in a
multiline buffer they move up or down within the
buffer, otherwise they search for a history line
matching the start of the current line. In this case,
however, they search for a line which matches the
current line up to the current cursor position, in the
manner of history-beginning-search-backward and
-forward, rather than the first word on the line.

So, you'd probably need something along the lines of this:

autoload -Uz up-line-or-beginning-search
autoload -Uz down-line-or-beginning-search
bindkey '\eOA' up-line-or-beginning-search
bindkey '\e[A' up-line-or-beginning-search
bindkey '\eOB' down-line-or-beginning-search
bindkey '\e[B' down-line-or-beginning-search


Regards, Frank
John
2013-01-23 20:37:54 UTC
Permalink
Heh.? Sorry for not reading your first posting thoroughly enough.
? ? up-line-or-beginning-search, down-line-or-beginning-search
? ? ? ? ? These? widgets are? similar? to? the builtin? functions
? ? ? ? ? up-line-or-search? and? down-line-or-search:? if? in? a
? ? ? ? ? multiline? buffer? they? move? up or? down? within? the
? ? ? ? ? buffer,? otherwise? they? search? for? a? history? line
? ? ? ? ? matching the start of the? current line.? In this case,
? ? ? ? ? however,? they? search for? a? line? which matches? the
? ? ? ? ? current line up to the? current cursor position, in the
? ? ? ? ? manner? ? of? history-beginning-search-backward? ? and
? ? ? ? ? -forward, rather than the first word on the line.
? autoload -Uz up-line-or-beginning-search
? autoload -Uz down-line-or-beginning-search
? bindkey '\eOA' up-line-or-beginning-search
? bindkey '\e[A' up-line-or-beginning-search
? bindkey '\eOB' down-line-or-beginning-search
? bindkey '\e[B' down-line-or-beginning-search
OK! ?I placed those 6 lines in my ~/.zshrc.local and resourced the config file. ?The key mapping seems intact:

% bindkey '"^[[A" up-line-or-beginning-search

But if I attempt to use the uparrow now, I get:

% vim ~/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? :(?
No such widget `up-line-or-beginning-search'

You thoughts are welcomed and thank you.
Frank Terbeck
2013-01-23 20:49:13 UTC
Permalink
John wrote:
[...]
Post by John
% vim ~/
No such widget `up-line-or-beginning-search'
Meh. This is what I get for not trying things out...

autoload -Uz up-line-or-beginning-search
zle -N up-line-or-beginning-search
autoload -Uz down-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey '\eOA' up-line-or-beginning-search
bindkey '\e[A' up-line-or-beginning-search
bindkey '\eOB' down-line-or-beginning-search
bindkey '\e[B' down-line-or-beginning-search

There seems to be a cosmetic bug that triggers the first time you use
either of those (if you got warn_create_global set, which I do):

up-line-or-beginning-search:15: scalar parameter __savecursor created globally in function
up-line-or-beginning-search:16: scalar parameter __searching created globally in function

you can do the following until you get a fixed up zsh version:

typeset -g __savecursor __searching


Regards, Frank
John
2013-01-23 21:28:07 UTC
Permalink
Post by Frank Terbeck
Meh. This is what I get for not trying things out...
? autoload -Uz up-line-or-beginning-search
? zle -N up-line-or-beginning-search
? autoload -Uz down-line-or-beginning-search
? zle -N down-line-or-beginning-search
? bindkey '\eOA' up-line-or-beginning-search
? bindkey '\e[A' up-line-or-beginning-search
? bindkey '\eOB' down-line-or-beginning-search
? bindkey '\e[B' down-line-or-beginning-search
There seems to be a cosmetic bug that triggers the first time you use
up-line-or-beginning-search:15: scalar parameter __savecursor created globally in function
up-line-or-beginning-search:16: scalar parameter __searching created globally in function
? typeset -g __savecursor __searching
The 8 lines you posted works without the typeset bit. ?Thank you. I think others would like this functionality. ?Perhaps add it as a the grml default?
Frank Terbeck
2013-01-23 21:33:19 UTC
Permalink
John wrote:
[...]
Post by John
The 8 lines you posted works without the typeset bit.
Then you don't have the warn_create_global option set. Which is ok, I
guess. I'm going to fix it upstream anyway.
Post by John
Thank you.
You're welcome.
Post by John
I think others would like this functionality. ?Perhaps add it as a the
grml default?
I don't think we should wiggle around with the default settings too
much. Things like this are pretty much up to anyone's subjective
preferences, which is exactly why we do have facilities like zshrc.local
and zshrc.pre in place.

Regards, Frank
Frank Terbeck
2013-01-23 22:05:23 UTC
Permalink
Frank Terbeck wrote:
[...]
Post by Frank Terbeck
I don't think we should wiggle around with the default settings too
much. Things like this are pretty much up to anyone's subjective
preferences, which is exactly why we do have facilities like zshrc.local
and zshrc.pre in place.
Also, it turns out, that we already do have a binding that does
basically the same on <PageUp> and <PageDown>. So.... :)

(Missing this serves me right for not using our zsh setup myself...)

Regards, Frank
John
2013-01-24 09:03:12 UTC
Permalink
Post by Frank Terbeck
Meh. This is what I get for not trying things out...
? autoload -Uz up-line-or-beginning-search
? zle -N up-line-or-beginning-search
? autoload -Uz down-line-or-beginning-search
? zle -N down-line-or-beginning-search
? bindkey '\eOA' up-line-or-beginning-search
? bindkey '\e[A' up-line-or-beginning-search
? bindkey '\eOB' down-line-or-beginning-search
? bindkey '\e[B' down-line-or-beginning-search
One last question for you, Frank. ?In the interest of maintaining a minimal configuration, are all 8 lines required for this functionality? ?My setup is the out-of-the-box grml configuration:
*Unmodified /etc/zsh/zshrc
*Unmodified /etc/skel/.zshrc (in my $HOME)
*8 line $HOME/.zshrc.local

Thanks again.
Frank Terbeck
2013-01-24 09:11:51 UTC
Permalink
Post by John
? autoload -Uz up-line-or-beginning-search
? zle -N up-line-or-beginning-search
? autoload -Uz down-line-or-beginning-search
? zle -N down-line-or-beginning-search
? bindkey '\eOA' up-line-or-beginning-search
? bindkey '\e[A' up-line-or-beginning-search
? bindkey '\eOB' down-line-or-beginning-search
? bindkey '\e[B' down-line-or-beginning-search
One last question for you, Frank. ?In the interest of maintaining a minimal
configuration, are all 8 lines required for this functionality? ?My setup is
Well, you _could_ leave out the two lines that contain bindings for
'\eOA' and '\eOB'. But those make things work in more terminals. So you
should probably keep them anyway.
Post by John
*Unmodified /etc/zsh/zshrc
That one you need. And you shouldn't modify it indeed.
Post by John
*Unmodified /etc/skel/.zshrc (in my $HOME)
This one, you could leave out, because it's just a set of examples, that
are all commented out...
Post by John
*8 line $HOME/.zshrc.local
...and since you should do all modifications via this file, you should
also put those examples from the skel file into .zshrc.local.

Regards, Frank
--
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
-- RFC 1925
John
2013-01-24 20:55:05 UTC
Permalink
Cool thanks for the time and effort. ?Everything is as I want it.
Loading...