50

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

10 Answers 10

37

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
  • 1
    @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
17

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.

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

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.

6

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.

4

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.

key=(
    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]}"
)
3

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.

2

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.

1

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).

1

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

0

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.)

plugins=(
  git
  history-substring-search
)

source $ZSH/oh-my-zsh.sh

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

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.