Ipython: Change autocomplete on tab to ctrl-space

Created on 11 Apr 2017  ·  4Comments  ·  Source: ipython/ipython

Hello,

I'd like to change the key binding for autocomplete to ctrl-space. I had read about it here: http://ipython.readthedocs.io/en/latest/config/details.html#keyboard-shortcuts. But I'm not sure how I would do it.

Most helpful comment

Basically, you just need to add this code to any startup file in your profile:

from prompt_toolkit.keys import Keys
from prompt_toolkit.key_binding.bindings.completion import generate_completions

ip = get_ipython()
bind_key = ip.pt_cli.application.key_bindings_registry.add_binding
bind_key(Keys.ControlSpace)(generate_completions)

You should really always check that you're running the terminal with PTK before you run that code though. Something like this should work:

def register_bindings():

    from prompt_toolkit.keys import Keys
    from prompt_toolkit.key_binding.bindings.completion import generate_completions

    bind_key = ip.pt_cli.application.key_bindings_registry.add_binding
    bind_key(Keys.ControlSpace)(generate_completions)

ip = get_ipython()
if getattr(ip, "pt_cli"): register_bindings()
del register_bindings

Note: You could put the registration logic in an if-block, but using a function stops names like bind_key leaking into your session's namespace. Once you have a bunch of bindings and custom filters defined, you end up creating quite a lot of names that you would otherwise have to delete manually. Putting it all in a function, then deleting the function's name is cleaner. For the simple case above, an if-block would probably be better.

All 4 comments

Basically, you just need to add this code to any startup file in your profile:

from prompt_toolkit.keys import Keys
from prompt_toolkit.key_binding.bindings.completion import generate_completions

ip = get_ipython()
bind_key = ip.pt_cli.application.key_bindings_registry.add_binding
bind_key(Keys.ControlSpace)(generate_completions)

You should really always check that you're running the terminal with PTK before you run that code though. Something like this should work:

def register_bindings():

    from prompt_toolkit.keys import Keys
    from prompt_toolkit.key_binding.bindings.completion import generate_completions

    bind_key = ip.pt_cli.application.key_bindings_registry.add_binding
    bind_key(Keys.ControlSpace)(generate_completions)

ip = get_ipython()
if getattr(ip, "pt_cli"): register_bindings()
del register_bindings

Note: You could put the registration logic in an if-block, but using a function stops names like bind_key leaking into your session's namespace. Once you have a bunch of bindings and custom filters defined, you end up creating quite a lot of names that you would otherwise have to delete manually. Putting it all in a function, then deleting the function's name is cleaner. For the simple case above, an if-block would probably be better.

It worked! Thank you for getting back to me so quickly!

When I try to run the above, I get AttributeError: 'TerminalInteractiveShell' object has no attribute 'pt_cli'. Is this a known issue?

write the following to your user preferences of keyboard shortcuts

{
    "shortcuts":[
        {
            "command": "completer:invoke-file",
            "keys": [
                "Ctrl Space"
            ],
            "selector": ".jp-FileEditor .jp-mod-completer-enabled"
        },
        {
            "command": "completer:invoke-file",
            "keys": [
                "Ctrl Space"
            ],
            "selector": ".jp-FileEditor .jp-mod-completer-enabled"
        },
        {
            "command": "completer:invoke-notebook",
            "keys": [
                "Ctrl Space"
            ],
            "selector": ".jp-Notebook.jp-mod-editMode .jp-mod-completer-enabled"
        }

    ]
}
Was this page helpful?
0 / 5 - 0 ratings