[IPython-dev] Using Prompt Toolkit to edit terminal inputs in a screen editor.

Carl Smith carl.input at gmail.com
Tue Jul 19 12:28:52 EDT 2016


-- Carl Smith
carl.input at gmail.com

On 19 July 2016 at 12:10, Thomas Kluyver <takowl at gmail.com> wrote:

> Ooh, nice find. If we can agree on a keyboard shortcut that people like
> for this, I'd like to enable it by default in IPython.
>
> On 18 July 2016 at 22:15, Carl Smith <carl.input at gmail.com> wrote:
>
>> Prompt Toolkit allows you to open the current input in your favourite
>> editor, edit the code, and when you exit, you have the edited code in the
>> input buffer. This is different to %edit, and is really useful in practice.
>>
>> If you chuck this code in a startup file, it will create a keybinding for
>> the feature. This code uses Ctrl-N (because ne is currently my favourite
>> tool for these kinds of edits), but you can easily change the keybinding.
>>
>>     from prompt_toolkit.keys import Keys
>>
>>     ip = get_ipython()
>>
>>     def open_input_in_editor(event):
>>
>>         event.cli.current_buffer.tempfile_suffix = ".py"
>>         event.cli.current_buffer.open_in_editor(event.cli)
>>
>>     bind_key = ip.pt_cli.application.key_bindings_registry.add_binding
>>     bind_key(Keys.ControlN)(open_input_in_editor)
>>
>> It'd be nice to have a better way of setting the tempfile suffix, so it
>> isn't redundantly reassigned every time you use the function, but doing it
>> this way is fine for now. Removing that causes the tempfile to have no
>> suffix, so you wouldn't get language specific features like syntax
>> highlighting.
>>
>> Hope someone else finds this useful.
>>
>> Best,
>> -- Carl Smith
>> carl.input at gmail.com
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> https://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> https://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20160719/48f46b61/attachment.html>


More information about the IPython-dev mailing list