In zsh, I know that I can search history with Ctrl+r. However, oftentimes I start to type a command directly at the prompt, but then realize I should be searching history. When I hit Ctrl+r, it brings up a blank history search prompt like this:

history search prompt not pre-filled

Notice how there is text at my prompt but not at the history search prompt. How do I start the history search with the text already in the prompt, so it looks like this:

history search prompt pre-filled

11 Answers 11


You can use zle's history-search functionality:

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

This binds Up and Down (adjust for your own escape sequences) to a history search, backwards and forwards, based upon what has already been entered at the prompt.

So, if you were to enter "vim" and hit Up, zsh will traverse backwards through your history for only those commands commencing with "vim".

You can additionally have the cursor placed at the end of the line once you have selected your desired command from zsh's history by using the history-search-end function (typically located in /usr/share/zsh/functions/Zle/) and appending -end to the end of each line, like so:

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
  • Great, thank you. I had to remove the "-end" for both bindkey commands to make them work though. I have not edited your answer because maybe there's some situation in which those are necessary? – Sean Mackesey Oct 27 '13 at 23:55
  • No problem: updated the answer with the detail you wanted clarified. – jasonwryan Oct 28 '13 at 0:07
  • This used to work for me until late Nov, Dec. 2015 when I upgraded to latest version of oh my zsh. Any ideas? suggestion? anybody else seeing this? how to debug? – hani elabed Dec 18 '15 at 6:20
  • 2
    @hanielabed Oh-my-Zsh is a clusterfsck; I am not at all surprised it would break something as fundamental as history search... – jasonwryan Dec 18 '15 at 6:24
  • 1
    Is there a way for the cursor to jump to the end from an empty history search, but stay the same position for a partial search? This is what happens in bash currently. – CMCDragonkai Jan 11 '17 at 4:35

Another useful option is history | grep

Assign an alias, e.g.

alias hg='history | grep'

then you can type hg whatever to search for commands you've used, e.g.

$ hg chmod                                                                                                       
 1309  chmod +x rotate_files.sh 
 1385  chmod +x rotate_files_270.sh 
 1512  chmod +x testy.sh 
 1528  chmod +x act_on_2_numbers.sh 
 2142  chmod +x ~/bin/display_tmux_pane_pwd.sh
 4532  chmod +x cat_files.rb 

I put this alias in my dot files.

  • 19
    What does hg remind me of... Oh yeah, Mercurial! – user42117 Mar 27 '15 at 19:57

If you are using oh-my-zsh, add history-substring-search to the plugins=(...) line.

Then add

bindkey "^[[A" history-substring-search-up
bindkey "^[[B" history-substring-search-down

somewhere below the line that read source $ZSH/oh-my-zsh.sh. Save and fire up a new terminal or run source ~/.zshrc in the current terminal.

Note: ^[[A is the escape sequence for up arrow in the terminal I use (kitty) and many others. To check in your terminal of choice, type in showkey -a and then press they key you want to find the escape sequence for.

  • It is unclear what this keymapping does, the answer above explains "This binds Up and Down (adjust for your own escape sequences) to a history search, backwards and forwards, based upon what has already been entered at the prompt." – Colin D May 31 '19 at 4:10
  • @ColinD Added a note to clarify – joelostblom Jun 4 '19 at 22:45
  • 1
    substring search is best, its a superset of command begins with. to implement without being an oh-my-zsh user I did brew install zsh-history-substring-search, checked the repo readme for instructions, I'll just go ahead and create an answer I'm really happy with the result. – Merlin Sep 8 '19 at 5:20

You might want to use the script at https://github.com/zsh-users/zsh-history-substring-search

Where you can type in any part of any previously entered command and press the Up and Down keys to cycle through the matching commands.


I'd highly recommend using "$terminfo[kcuu1]" or "$key[Up]" rather than hard-coded stuff like "^[[A" which may or may not work on any particular system.

Check out /etc/zsh/zshrc for more keys. Here's what it looks like on my system. I think the terminfo keys are more likely to be defined.

    BackSpace  "${terminfo[kbs]}"
    Home       "${terminfo[khome]}"
    End        "${terminfo[kend]}"
    Insert     "${terminfo[kich1]}"
    Delete     "${terminfo[kdch1]}"
    Up         "${terminfo[kcuu1]}"
    Down       "${terminfo[kcud1]}"
    Left       "${terminfo[kcub1]}"
    Right      "${terminfo[kcuf1]}"
    PageUp     "${terminfo[kpp]}"
    PageDown   "${terminfo[knp]}"

What about using zsh-autosuggestions?

The thing is, for each command you type this plugin will show you some suggestions that could be accepted or not. Try it out and give us some feedback.

With no plugins we can press Ctrlr to perform a reverse seach on the history, as soon as you start typing the matched commands will appear as suggestions.


I use the vi-mode plugin. And then in my ~/.zshrc I have the following bindings:

bindkey "^P" history-beginning-search-backward
bindkey "^N" history-beginning-search-forward

That enables me to use the ^P and ^N as normal backward and forward search if I don't enter any text, and as filtered search if I do.


I agree with cheflo that "substring" search is more useful. I cannot comment there so I started a new answer. I use oh-my-zsh, and add "history-substring-search" to the plugins=(...) of ~/.zshrc. Then source ~/.zshrc. Do not need to add anything more. Use "bindkey" to verify:

"^[OA" history-substring-search-up
"^[OB" history-substring-search-down

Later, you type "to" and up arrow, "history" will appear and "to" highlighted (if you used "history" command before).


You could instead use zaw, which completely replaces your search with a much better multi keyword super search, and fixes your problem as well - searching history is only one of the things zaw can search.

Small intro/discussion here


Non-Oh-My-Zsh answer

This worked for me (on macOS) to get substring history easily accessible via up/down arrow. The key binding is up to you.

Substring search history is better than just accessing commands that start with a string. This is more like ctrl-r in most cases, those without globs or regex matching.

Starting from instructions at zsh-users/zsh-history-substring-search

brew install zsh-history-substring-search

# add the following to .zshrc
source /usr/local/share/zsh-history-substring-search/zsh-history-substring-search.zsh
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

Now enjoying this thoroughly.


Because I use oh-my-zsh, and I use the substring solution from joelostblom, James, I modify the .zshrc like this: (which at line 13, find the line "source $ZSH/oh-my-zsh.sh ", then modify code around it.)


source $ZSH/oh-my-zsh.sh

bindkey -v
bindkey "^[[A" history-substring-search-up
bindkey "^[[B" history-substring-search-down
  • bindkey -v will break ctrl-a, ctrl-e. Most likely all ctrl commands. – Michael Ozeryansky Oct 15 '19 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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