Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change autocomplete on tab to ctrl-space #10440

Closed
hexhexD opened this issue Apr 11, 2017 · 5 comments
Closed

Change autocomplete on tab to ctrl-space #10440

hexhexD opened this issue Apr 11, 2017 · 5 comments
Milestone

Comments

@hexhexD
Copy link

hexhexD commented Apr 11, 2017

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.

@carlsmith
Copy link
Contributor

carlsmith commented Apr 11, 2017

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.

@hexhexD
Copy link
Author

hexhexD commented Apr 11, 2017

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

@hexhexD hexhexD closed this as completed Apr 11, 2017
@Carreau Carreau modified the milestone: no action May 30, 2017
@aggregate1166877
Copy link

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

@pravinkmr26
Copy link

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"
        }

    ]
}

@slavistan
Copy link

@Pravinkumar2629 The first and second object of the shortcuts array in your example are identical. Is this intended?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants