autocomplete-while-you-type in IPython shell
Would it be possible to expose an option to enable an autocomplete-while-you-type experience in IPython shell? In other words, try to show the user the menu of available autocompletions when they hit any key, rather than making them hit Tab to see it, similar to how autocompletion works in IDEs like VS Code, PyCharm, etc.
I searched the tracker and the docs for this and couldn't find it. Sorry if I missed something.
Thanks for your consideration and for the great work on IPython!
It requires changing how the completer from prompt_toolkit is used. Absolutely doable, although IIRC it requires changing the default keybindings: left/right arrow keys could no longer be used to move cursor, as they are bound to the autocomplete menu by default.
Reference: search for complete_while_typing in Prompt Toolkit reference.
Thanks for the quick reply, and great to know this is doable! I had a look at the Prompt Toolkit docs you linked to, and so far I've gotten as far as:
In [1]: import IPython
In [2]: ipython = IPython.get_ipython()
In [3]: ipython.pt_app.app.current_buffer.complete_while_typing.func()
Out[3]: False
but still haven't figured out how to set complete_while_typing to True.
@jab I think it has to be done by using a Filter, you need to pass it to complete_while_typing at the moment the instance of Buffer is created.
Thanks. Sounds like this is something that requires changes to IPython itself then?
@jab if you want to do it the supported way, then yes. It's probably possible to hack it in just by changing parameters in some of the instances of prompt toolkit classes, but I haven't looked that deep into it.
I might try to PR it though. But I'd first wait for @Carreau to comment on whether it's something he'd like to see: it would have to be some kind of a config option. It's very user-specific behavior and I don't think that everyone would be onboard to have it as the new default.
Any updates?
Out of curiosity, was this added in the past? We got a report of this over on the Terminal repo: https://github.com/microsoft/terminal/issues/14510 and it's certainly not us providing that completion text
You are probably refering to #12570 which implemented autosuggestion from history (in terminal only) and not auto-trigger of full completer as this issue is asking for (although that can be achieved with jupyterlab-lsp).
Changelog entry for autosuggestions: https://ipython.readthedocs.io/en/stable/whatsnew/version8.html#autosuggestions
Thanks for the clarification!
According to @MrMino
@jab I think it has to be done by using a
Filter, you need to pass it tocomplete_while_typingat the moment the instance ofBufferis created.
A compromise solution is to modify the IPython/terminal/interactiveshell.py. Add complete_while_typing=True to the PromptSession constructor in the init_prompt_toolkit_cli method of the TerminalInteractiveShell class like:
self.pt_app = PromptSession(
complete_while_typing=True,
...
)
You also need to disable history search (change the enable_history_search to False in the config file) in order for complete_while_typing to work properly.
Thanks @RIvance, tried that but still didn't get the autosuggest-while-typing behavior that you get out-of-the-box with e.g. bpython. (For example, type "foo". and immediately see a menu of all possible methods you can call on "foo" (and the menu appears as soon as you type ., no need to hit Tab first). As you continue typing, the menu gets filtered down to match what you've typed.) Do you get that in IPython when you try the above?
Would also be awesome if IPython had autosuggest for function parameters that automatically appeared as soon as you type ( (bpython also has this out-of-the-box), but not sure if that deserves its own separate issue.