I was wondering if it is possible to run the clear command automatically when I initiate a reverse search with CTRLR.

I am familiar with the precmd function in zsh, but do not know how to limit it to a subset of commands without slowing down the sell. One possible solution would be to add an if condition in precmd for detecting reverse search, although I am not sure what the condition should be for history-incremental-search-backward.

1 Answer 1

function my-history-incremental-search-backward {
    zle clear-screen
    zle history-incremental-search-backward
zle -N my-history-incremental-search-backward
bindkey "^r" my-history-incremental-search-backward

We add a custom incremental search function that first clears the screen (you could also run clear but that looked ugly and then does not redraw the prompt if you control+c out of the search) and then runs the real history-incremental-search-backward.

Alternatively, you could redefine the history-incremental-search-backward widget with:

function history-incremental-search-backward {
  zle clear-screen
  zle .$WIDGET
zle -N history-incremental-search-backward

So you don't even need to rebind to all the keys the widget was already bound to.

  • Thank you for providing this solution. I added these lines to my ~/.zshrc but did not get the desired effect. I will continue to work on it and share updates later.
    – Matt
    Aug 11, 2022 at 3:37
  • autoload does not resolve the issue. You are right. I made sure the snippet was near the end of the file.
    – Matt
    Aug 11, 2022 at 4:02
  • Unfortunately, none of the solutions mentioned in this post work for me.
    – Matt
    Aug 11, 2022 at 18:27

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.