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

Carl Smith carl.input at gmail.com
Mon Jul 18 17:15:21 EDT 2016

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"

    bind_key = ip.pt_cli.application.key_bindings_registry.add_binding

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

Hope someone else finds this useful.

-- Carl Smith
carl.input at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20160718/ac5653c2/attachment.html>

More information about the IPython-dev mailing list