2

My ~/.zshrc:

setopt histignoredups
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 "^[[A" history-beginning-search-backward-end
bindkey "^[[B" history-beginning-search-forward-end

I want up arrow to fill the last command line that matches the command line typed so far,

dotnet build
dotnet bin/Debug/netcoreapp2.2/MyProg.dll
dotnet build
dotnet bin<UP ARROW>

should fill in dotnet bin/Debug/netcoreapp2.2/MyProg.dll.

However, with the ~/.zshrc I currently have, it fills in dotnet build.

Looks like it only works up to the first space.

How can I make the up arrow select the last command line that matches the command line typed so far?

1 Answer 1

1

TL,DR:

bindkey "^[OA" history-beginning-search-backward-end
bindkey "^[OB" history-beginning-search-forward-end

Does your terminal actually send ^[[A for Up? Mine sends ^[OA. With your .zshrc:

darkstar% bindkey | grep '\^\[.A'
"^[OA" up-line-or-history
"^[[A" history-beginning-search-backward-end

Pressing Up sends ^[OA which has its default binding and recalls the previous line. It's just a coincindence that the previous line starts with the same word. Pressing Esc [ A recalls the previous line starting with dotnet bin as desired.

To check what your terminal sends for a function or cursor key, press Ctrl+V then the key or key chord. Note that full-screen applications often use a different terminal mode from command-line applications where some keys send different escape sequences (Application Cursor Keys). ^[[A is the usual command-line escape sequence for Up, ^[OA is the usual full-screen escape sequence.

1
  • It seems to be inconsistent. Some of my terminal's tabs are ^[[A and some are ^[OA. Do you know why this might be? Terminal.app on macOS Nov 12, 2020 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .