1

When I open the Terminal and press the "page up" button it simply scrolls my Terminal screen one page up, so that I can see what I typed and what the executed commands outputs were earlier.

I want to bind this key to a command called "history-search-backward" but I can't find a correct argument to provide for a bindkey ??? history-search-backward command.

I successfully set ctrl+N to do history-search-backward: bindkey "^N" history-search-backward but it is much trickier for page up/page down keys. What do I mean by "trickier":

When I execute read command and then press ctrl+N it shows ^N in the output but if I do the same for page UP or page DOWN keys... it simply scrolls my screen up/down and outputs nothing. Same goes for od -c and showkey -a - no output, simply scroll screen up/down. Even echo $terminfo[kpp] returns empty line, which concerns me because echo $terminfo[khome] returns H.

I downloaded "Key Codes" app (link to app store) and it provides me with these key codes for page UP/page DOWN keys but I can't figure out how to use "116 / 0x74" or "121 / 0x79" as an argument to bindkey command or is it even possible.

enter image description here

I use macOS Sonoma 14.5 and a Terminal app which I believe is a built-in app, it uses zsh and echo $TERM command outputs xterm-256color.

How do I bindkey page UP/page DOWN keys to history search commands?

1 Answer 1

3

By default, the Page Up and Page Down keystrokes are intercepted and interpreted by Terminal.app, so they never make it to the shell running in the terminal window. Therefore there's no way to assign a key sequence via bindkey.

However, Terminal can be customized to pass those keys to zsh. The menu selection Terminal -> Settings... -> Profiles -> Basic (or another profile) -> Keyboard will bring up the dialog for adding key sequences: Settings... -> Profiles -> Basic -> Keyboard dialog

Click on +, set Key to Page Up, Modifier to None, Action to Send Text:, and enter esc[5~ in the text box:

keystroke entry dialog

Similarly, Page Down can be set to esc[6~. The key sequences are from a list here, although almost any unique sequence will work.

Now we can pair the keys and commands via bindkey:

bindkey $'\e[5~' history-search-backward
bindkey $'\e[6~' history-search-forward
2
  • Binding them to standard key escape codes help your binding to work in more programs easily without much configuration. They may not even offer easy ways to change default keybinding.
    – hym3242
    Commented Jul 3 at 22:53
  • Thank you, this does work like I intended to
    – a_girl
    Commented Jul 4 at 10:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .