Question

The keystroke Ctrl + \ causes python and ipython to core dump and quit immediately. How can I prevent this? I don't want to disable the key or remap the keyboard.

I have recently moved to a UK keyboard and the backslash/pipe key, which I'm used to having above the Enter key, is now located in between the left shift and the Z key. I find I'm bumping the keyboard shortcut for SIGQUIT pretty frequently and losing all my variables from the interactive interpreter.

Was it helpful?

Solution

It seems that you are on a *nix platform. Ctrl\ generates SIGQUIT, so this isn't really as much about Python.

If you want to disable the key combination you could use stty before starting Python:

stty quit undef

Alternatively, use the following to run Python:

stty quit undef; python; stty quit ^\\

which would disable the key mapping before executing python and set it back later.

OTHER TIPS

Answers involving signal handlers do not solve the issue for IPython 5 and higher, because the Ctrl-\ binding is now handled explicitly at the application level (this had something to do with migration from readline to prompt toolkit).

To disable Ctrl-\ for newer versions of IPython (tested on IPython 5.3), use the startup files to disable that binding:

from IPython.terminal.shortcuts import force_exit
from IPython import get_ipython
ip = get_ipython()
ip.pt_cli.application.key_bindings_registry.remove_binding(force_exit)

You can use the signal module and set a handler for the SIGQUIT signal:

import signal

def handler(signum, frame):
    print("SIGQUIT")

signal.signal(signal.SIGQUIT, handler)

and add this code to IPython startup files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top