Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently, I have the following in my .zshrc:

bindkey '^[[A' up-line-or-search
bindkey '^[[B' down-line-or-search

However, this only seems to match the content of my current input before a space character occurs. For example, sudo ls / will match every line in my history that begins with sudo, while I would like it to only match lines that match my entire input. (i.e. sudo ls /etc would match, but not sudo cat /var/log/messages)

What do I need to change in order to gain the desired behavior?

Here is my entire .zshrc in case it is relevant: https://gist.github.com/919566

share|improve this question
up vote 8 down vote accepted

This is the documented behavior:

Move down a line in the buffer, or if already at the bottom line, search forward in the history for a line beginning with the first word in the buffer.

There doesn't seem to be an existing widget that does exactly what you want, so you'll have to make your own. Here's how to define a widget that behaves like up-line-or-search, but using the beginning of the line (up to the cursor) rather than the first word as search string. Not really tested, especially not on multi-line input.

up-line-or-search-prefix () {
  local CURSOR_before_search=$CURSOR
  zle up-line-or-search "$LBUFFER"
zle -N up-line-or-search-prefix

An alternate approach is to use history-beginning-search-backward, but only call it if the cursor is on the first line. Untested.

up-line-or-history-beginning-search () {
  if [[ -n $PREBUFFER ]]; then
    zle up-line-or-history
    zle history-beginning-search-backward
zle -N up-line-or-history-beginning-search
share|improve this answer
Thanks, Gilles, your first solution works pretty well, but there's one issue and I'm hoping you can help me resolve it. When I call up-line-or-search-prefix on an empty input, it gives me the last run command -- the desired behavior. However, when I run up-line-or-search-prefix again, it does an up-line-or-search on the first character of my input. Is it possible to somehow change things so that pressing the up arrow with an empty input will scroll through my history? – rps Jul 14 '11 at 16:33
@rps Ah, gotcha, it was picking up the first word because the argument was missing (instead of being empty as it should have). Use my edited version. – Gilles Jul 14 '11 at 23:02

zsh provide this functionality by using



bindkey "^[[A" history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward

Find exact Key code by


share|improve this answer
An alternative for keycodes that might work across different terminals can be found in zprezto [1]. <kbd>Up</kbd> is $terminfo[kcuu1], <kbd>Down</kbd> is $terminfo[kcud1]. – Justin C Nov 22 '15 at 21:02

Have also a look at the zsh-history-substring-search project.

It offers what you are looking for, and it can even be used as a oh-my-zsh plugin.

share|improve this answer

This blog post from 2013 recommends a couple of keybinds that match all the words before the cursor.

# Cycle through history based on characters already typed on the line
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search
bindkey "$terminfo[kcuu1]" up-line-or-beginning-search
bindkey "$terminfo[kcud1]" down-line-or-beginning-search

I used $terminfo as recommended in one of the comments, so this should set up the keys correctly, regardless of your terminal and environment.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.