You are not logged in.

#1 2012-10-03 01:38:34

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Zsh: problem with up arrow binding and history function [SOLVED]

The expected action of an 'up arrow' in the middle of a command using zsh is not right.  What do I have misconfigured?

When I type the following:

% cd /mnt<<UP ARROW>>

I expect zsh to search my history of other commands that started with "cd /mnt" and auto complete that subset of my history.  In other words, ONLY history items that begin with "cd /mnt" but that isn't what's happening.  Upon hitting <<UP ARROW>> in the middle of a "cd /mnt" gives me entries in my history that begin with "cd" not with "cd /mnt" and I don't know why.  Where is the error in my configuration?

Relevant config files:
~/.zshrc.
As you can see, it will source the scriptlets in ~/.zsh as well.

Thanks in advance!

Last edited by graysky (2012-10-03 19:20:50)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2012-10-03 02:59:58

w0ng
Member
From: Australia
Registered: 2009-06-04
Posts: 88
Website

Re: Zsh: problem with up arrow binding and history function [SOLVED]

You want this in your zshrc:

autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
...
[[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-beginning-search
[[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-beginning-search

See "History Control" in man zshzle for more info on these:
history-beginning-search-backward
history-search-backward
up-line-or-history
up-line-or-search

See "up-line-or-beginning-search" in man zshcontrib for more info as to why you probably want that one

Last edited by w0ng (2012-10-03 03:09:02)

Offline

#3 2012-10-03 07:28:38

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Zsh: problem with up arrow binding and history function [SOLVED]

@w0ng - Thanks for the tip.  I actually didn't need the autoload lines and made this two line change:

-[[ -n "${key[Up]}"      ]]  && bindkey  "${key[Up]}"      history-search-backward
-[[ -n "${key[Down]}"    ]]  && bindkey  "${key[Down]}"    history-search-forward
+[[ -n "${key[Up]}"      ]] && bindkey  "${key[Up]}"      history-beginning-search-backward
+[[ -n "${key[Down]}"    ]] && bindkey  "${key[Down]}"    history-beginning-search-forward

Last edited by graysky (2012-10-03 07:29:07)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2012-10-03 12:02:46

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Zsh: problem with up arrow binding and history function [SOLVED]

Ack... not solved just yet.

The above fixed the question I asked initially but now I am unsure how to fix another annoyance: when I am at an empty prompt, and hit <<UP ARROW>>, I cycle through my history file as expected BUT the cursor stays at the beginning of the line.  I want it to go the end of the line as it did in bash.

Thoughts are welcomed!

Last edited by graysky (2012-10-03 12:05:58)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2012-10-03 15:13:43

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,845
Website

Re: Zsh: problem with up arrow binding and history function [SOLVED]

How about '{up,down}-line-or-beginning-search'?

autoload up-line-or-beginning-search
autoload down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "^[[A" up-line-or-beginning-search
bindkey "^[[B" down-line-or-beginning-search

*isn't sure how your key definitions work, but they don't seem to work for me*

EDIT: from man zshcontrib:

       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 buf‐
              fer, 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-back‐
              ward and -forward, rather than the first word on the line.

Last edited by WorMzy (2012-10-03 15:15:52)


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#6 2012-10-03 19:19:58

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Zsh: problem with up arrow binding and history function [SOLVED]

This was a tough nut to crack!  Thank you both for the suggestions and links.  Here is what seems to be working (diffs):

+autoload up-line-or-beginning-search
+autoload down-line-or-beginning-search
+zle -N up-line-or-beginning-search
+zle -N down-line-or-beginning-search
+
-[[ -n "${key[Up]}"      ]]  && bindkey   "${key[Up]}"       history-beginning-search-backward
-[[ -n "${key[Down]}"    ]]  && bindkey   "${key[Down]}"    history-beginning-search-forward     
+[[ -n "${key[Up]}"      ]]  && bindkey   "${key[Up]}"       up-line-or-beginning-search
+[[ -n "${key[Down]}"    ]]  && bindkey   "${key[Down]}"    down-line-or-beginning-search

The ~/.zshrc in its entirety is linked in my sig on my github for anyone else wanting this behavior.  Thanks again!  I love the Arch Community and am proud to be a part of it.

Last edited by graysky (2012-10-03 19:20:37)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#7 2012-10-03 23:54:35

mhertz
Member
From: Denmark
Registered: 2010-06-19
Posts: 681

Re: Zsh: problem with up arrow binding and history function [SOLVED]

I have always used:

autoload -U history-search-end

zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end

bindkey "\e[A" history-beginning-search-backward-end
bindkey "\e[B" history-beginning-search-forward-end

That does exactly what you requested, but the one you're using additionally adds another feature; the ability to go up/down on multi-line command-lines, but for me that dosen't work for some reason?

I very rarely use multi-line command-lines, so dosen't really mind, but was just currious about if it works for you with said widget? (I mean if the up/down on multi-lines work, and not the other behaviour which also works fine for me)

Offline

#8 2012-10-04 01:07:58

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Zsh: problem with up arrow binding and history function [SOLVED]

@mhertz - I have two wants:

1) At a clean prompt, up arrow will go back through history leaving the cursor at the end of the command.
2) If I start typing a command, then hit up arrow, search will occur from that point onward leaving the cursor at the position it was when I hit up arrow.

My current ~/.zshrc + the .zsh directory achieves this.  See the github link in my sig.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB