Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers 3

up vote 8 down vote accepted

This is the documented behavior:

down-line-or-search
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"
  CURSOR=$CURSOR_before_search
}
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
  else
    zle history-beginning-search-backward
  fi
}
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
add comment

zsh provide this functionality by using

history-beginning-search-backward
history-beginning-search-forward

Ex.

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

Find exact Key code by
ctrl+vKEY

Ex.
ctrl+vUP
ctrl+vDOWN
ctrl+vPageUp
ctrl+vPageDown
etc.

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.