oh-my-zsh history completion

46

18

I have recently switched to zsh, using robbyrussell's oh-my-zsh. Before that i used bash with a lot of custom stuff and i am only missing one thing because zsh is trying to be 'too smart':

If i type git commit and then zsh goes through all recent git commands. What i really want though, is going through all commands that start with git commit (not just git).

How can i achieve this behavior in (oh-my-)zsh?

Patrick Oscity

Posted 2012-04-27T13:23:48.747

Reputation: 1 499

On Ubuntu see https://superuser.com/a/1490192/131522

– mixel – 2019-10-08T09:25:03.417

Strange, I use OMZSH and it does behave like this.
Do you have 'plugins=(git)' in your zshrc?
– Chris2048 – 2012-04-28T12:29:26.247

yes i do, but it is not only for git, all commands are completed this way. – Patrick Oscity – 2012-04-28T18:21:45.683

Answers

68

I have found the solution to my problem in the ZSH documentation. Oh-my-zsh seems to map the and Keys to something like

bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward

Which yields the exact behavior I described above. The ZSH Documentation describes the behavior of history-search-backward as

Search backward in the history for a line beginning with the first word in the buffer.



What I wanted instead was the following mapping, which I inserted into my ~/.zshrc:

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

The behavior of history-beginning-search-backward is as follows:

Search forward in the history for a line beginning with the current line up to the cursor. This leaves the cursor in its original position.

Also, if \e[A doesn't work for the up or down arrows, press <ctrl-v><KEY (e.g., up arrow)> in another terminal which gives ^[OA. Then you can use this instead of \e[A. The process is described here: http://zshwiki.org/home/zle/bindkeys

Patrick Oscity

Posted 2012-04-27T13:23:48.747

Reputation: 1 499

For non-OMZ user like me: bindkey doesn't do anything real, due to a bug, so OMZ team patched it with this plugin. Simply download it and source it before bindkey.

– Franklin Yu – 2016-11-25T15:22:33.420

@FranklinYu you do realize that's total bs, right? that plugin doesn't do anything with bindkey except USE it to catch some events. – nonchip – 2018-07-25T18:38:08.723

Wow, this is perfect! – Squirrel – 2018-09-07T08:48:23.617

1Thank you thank you thank you!! I was going nuts over this – mhenrixon – 2014-03-19T11:46:53.630

12

I wanted the same behaviour for zsh with oh-my-zsh installed and found plugin history-substring-search.

I achieved the same behaviour described above by adding the plugin to my ~/.zshrc:

plugins=(git brew npm history-substring-search)

I guess this plugin did not exist back when this question was raised. Just an alternate way to achieve the same thing.

dcusan

Posted 2012-04-27T13:23:48.747

Reputation: 121