ipython: change PageDown/PageUp to move back/forward through command history

In my shell (zsh) or in python, I can go backward through command history by pressing PageDown, and I can go forward by pressing PageUp.

But in ipython, these shortcuts are reversed.

Where are these shortcuts defined for ipython, and how can I reverse them back, so that

PageDown goes back in history, and PageUp goes forward in history?

I am using ipython3 version 5.8.0 on Debian 10.

Answer

Create script in ~/.ipython/profile_default/startup directory with any name ending with extension .py or .ipy

For example i created history_keybindings.py and put it inside ~/.ipython/profile_default/startup directory

from IPython import get_ipython
from IPython.terminal.shortcuts import previous_history_or_previous_completion, next_history_or_next_completion
from prompt_toolkit.keys import Keys
from prompt_toolkit.filters import HasSelection

ip = get_ipython()

registry = None

if (getattr(ip, 'pt_app', None)):
   # for IPython versions 7.x
   registry = ip.pt_app.key_bindings
elif (getattr(ip, 'pt_cli', None)):
   # for IPython versions 5.x
   registry = ip.pt_cli.application.key_bindings_registry

if registry:
   registry.add_binding(Keys.PageUp, filter=(~HasSelection()))(previous_history_or_previous_completion)
   registry.add_binding(Keys.PageDown, filter=(~HasSelection()))(next_history_or_next_completion)

Note: For more info check here