3

When using the up and down arrow in my terminal (iTerm 2 on Mac OS Catalina), I can browse through my last commands using the up and down arrows.

I use ZSH (Oh-my-zsh to be precise) and if I type e.g. vim then press up, it will browse my history for any commands issued starting with vim.

I think it's annoying to have to move my hand to the arrow keys, so i wanted to bind ctrl-j and ctrl-k for browsing up and down.

I looked at bindkey and bound ^j to down-history and ^k to up-history. This allows me to browse my history, but not with the "smart" functionality (i'm not sure of the right terminology here) - It simply goes up and down in my history, without regards to my input.

I've looked through the standard widgets on the Zsh Line Editor manual, but I cannot find the right command for this.

Which command should I bind ^j and ^k to to get my desired result?

2
  • Check the output of bindkey | grep -F '^[[A'; that should show you the widget that up-arrow is currently bound to.
    – chepner
    Jun 13, 2020 at 12:03
  • 1
    That was actually a great suggestion. It gave me back up-line-or-history, so naturally I bound ^p to that. Unfortunatly i still only get what seems to be up-history without the "smart" search.
    – Fizk
    Jun 15, 2020 at 19:29

1 Answer 1

5

After some further digging, i ended up on the The Z-Shell Line Editor page at the section called "Prefix searching".

It revealed that the widget I was after, is called history-beginning-search-backward. When using that widget, it works almost as I wanted it to, but not jumping to the end of the line.

A quick search lead me to another StackOverflow Question, that states that if you want to jump to the end of the line (as with the up arrow), it should be done as follows;

I had the same question and managed to find the solution with some experimentation.

I added the following to my .inputrc

"\eOA": history-search-backward # Up

"\eOB": history-search-forward  # Down

"\C-P": "\eOA\C-E"              # Ctrl-P

"\C-N": "\eOB\C-E"              # Ctrl-N

This binds two hotkeys to Ctrl-P and Ctrl-N. One for history search, and the other Ctrl-E for end-of-line.

2
  • Nice. Been searching for an hour on this. Thanks!
    – StevieD
    Aug 22, 2021 at 18:07
  • I get a command not found: \eOA:
    – Murilo
    Sep 24, 2021 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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