Using Prompt Toolkit to edit terminal inputs in a screen editor.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Using Prompt Toolkit to edit terminal inputs in a screen editor.

Carl Smith
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

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using Prompt Toolkit to edit terminal inputs in a screen editor.

Thomas Kluyver-2
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 <[hidden email]> 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

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using Prompt Toolkit to edit terminal inputs in a screen editor.

Carl Smith

-- Carl Smith

On 19 July 2016 at 12:10, Thomas Kluyver <[hidden email]> 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 <[hidden email]> 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

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev



_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using Prompt Toolkit to edit terminal inputs in a screen editor.

Carl Smith
​Sorry for sending a blank reply to the list earlier. GMail needs to move [Send] to the right-hand side.

On 19 July 2016 at 12:10, Thomas Kluyver <[hidden email]> 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.

​Sweet. That would cool. Cheers Thomas. I found it a couple of days ago, and use it all the time. Prompt Toolkit is awesome.

Once you get a reference to their PT cli instance (as `ip.pt_cli`, or the first arg to any prompt token method, or the `cli` attribute of any event) you can access the input buffer, set up keybindings and any stuff like that - basically, anywhere you're just playing with state and event handlers, you're generally ok. But. There are still a lot of cool options that are in the PT docs, that we can't access yet from IPython (as far as I tell).

The documented APIs for the inaccessible PT features involve passing stuff to their `parse` method, using named args, but there's no obvious way to attach the same stuff to a running instance. Any ideas would be appreciated. As a concrete example, how would an IPython user add a bottom toolbar:


Thanks,
-- Carl Smith


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using Prompt Toolkit to edit terminal inputs in a screen editor.

Thomas Kluyver-2
On 19 July 2016 at 17:58, Carl Smith <[hidden email]> wrote:
The documented APIs for the inaccessible PT features involve passing stuff to their `parse` method, using named args, but there's no obvious way to attach the same stuff to a running instance. Any ideas would be appreciated. As a concrete example, how would an IPython user add a bottom toolbar:

I think that's something we'd need to expose. I'm planning to investigate adding a bottom toolbar for IPython 6.

_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using Prompt Toolkit to edit terminal inputs in a screen editor.

Carl Smith

On 19 July 2016 at 18:01, Thomas Kluyver <[hidden email]> wrote:
On 19 July 2016 at 17:58, Carl Smith <[hidden email]> wrote:
The documented APIs for the inaccessible PT features involve passing stuff to their `parse` method, using named args, but there's no obvious way to attach the same stuff to a running instance. Any ideas would be appreciated. As a concrete example, how would an IPython user add a bottom toolbar:

I think that's something we'd need to expose. I'm planning to investigate adding a bottom toolbar for IPython 6.

​Cool. It'd be nice if there was a way to define some of this stuff on a running instance, instead of you guys having to push each feature by hand, but I'm just being impatient really. Each new feature will need properly integrating into IPython anyway.

​On keys for the editor feature, users can do `from prompt_toolkit.keys import Keys`​, then use stuff like `Keys.ControlV` easily. The PT API would make it pretty simple to take a user's Key subclass and set up their shortcut. I think only do it once per session though, like any event handler.

​Best, and thanks again Thomas. You do an awesome job with the Python community.​


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using Prompt Toolkit to edit terminal inputs in a screen editor.

Carl Smith

On 19 July 2016 at 18:12, Carl Smith <[hidden email]> wrote:
​On keys for the editor feature, users can do `from prompt_toolkit.keys import Keys`​, then use stuff like `Keys.ControlV` easily. The PT API would make it pretty simple to take a user's Key subclass and set up their shortcut. I think only do it once per session though, like any event handler.

​This^ would go in the IPython config file.

​When you're creating a fullscreen app with PT, do you still call `prompt`? The point really: Do you call a method that accepts the same config args as `prompt`? Could you add another option to the config file that requires a dict, where the keys are `prompt` keyword args, and then just `**kargs` it into the `prompt` call inside IPython?

You'd still maybe want IPython APIs for PT stuff people want to do more often, but at least users would always have raw access the much more of the library.

​Best,​
-- Carl Smith


_______________________________________________
IPython-dev mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/ipython-dev