[IPython-dev] Shortcut to convert cell between code and markdown while remaining in edit mode?

Maximilian Albert maximilian.albert at gmail.com
Mon Apr 21 06:06:11 EDT 2014


Briliant! This works prefectly, and together with Zoltán's suggestion of
clearing the cache (which I must admit I could have thought of...)
completely solves my problem. Thanks a lot to everybody for the quick and
very helpful replies! :)

Best wishes,
Max


2014-04-21 9:08 GMT+01:00 Juergen Hasch <python at elbonia.de>:

>
> The reason you see this behavior is because
> a) custom.js gets executed before there is an IPython.keyboard_manager
> b) switching between code and markdown cells actually creates a new cell
>
> This should do:
>
> $([IPython.events]).on('app_initialized.NotebookApp', function(){
>
>     IPython.keyboard_manager.edit_shortcuts.add_shortcut('ctrl-y', {
>         help : 'toggle markdown/code',
>         handler : function (event) {
>             var cell = IPython.notebook.get_selected_cell();
>             if (cell.cell_type == 'code') {
>                 var cur = cell.code_mirror.getCursor();
>                 IPython.notebook.command_mode();
>                 IPython.notebook.to_markdown();
>                 IPython.notebook.edit_mode();
>                 cell = IPython.notebook.get_selected_cell();
>                 cell.code_mirror.setCursor(cur);
>             } else if (cell.cell_type == 'markdown') {
>                 var cur = cell.code_mirror.getCursor();
>                 IPython.notebook.command_mode();
>                 IPython.notebook.to_code();
>                 IPython.notebook.edit_mode();
>                 cell = IPython.notebook.get_selected_cell();
>                 cell.code_mirror.setCursor(cur);
>             }
>             return false;
>         }
>     });
>
> });
>
>
> Am 21.04.2014 01:01, schrieb Maximilian Albert:
> > Hi Min,
> >
> > many thanks for the quick reply. I was aware that keyboard shortcut
> customization was on the agenda, but hadn't realised
> > that it had hit master (or even a released version) yet. That's great
> news! :)
> >
> > Unfortunately, adding the code you posted to the file
> .ipython/profile_nbserver/static/custom/custom.js didn't make any
> > difference - it seems like the code is simply ignored (I am starting the
> notebook using --profile=nbserver, so it should
> > be the correct file, right?).
> >
> > However, I can paste the snippet into a running notebook using the
> %%javascript magic. If I do this then the keyboard
> > shortcut is indeed defined and I can use it. But the behaviour is
> slightly strange in that once I press Ctrl-y I seem to
> > be "stuck" in the cell I'm editing and can't leave it using Shift+Enter
> alone (I have to click with the mouse in a
> > different cell to get out). However, the following works as expected
> (not the additional lines
> > "IPython.notebook.command_mode();"):
> >
> > ==>
> > IPython.keyboard_manager.edit_shortcuts.add_shortcut('ctrl-y', {
> >     help : 'toggle markdown/code',
> >     handler : function (event) {
> >         var cell = IPython.notebook.get_selected_cell();
> >         if (cell.cell_type == 'code') {
> >             IPython.notebook.command_mode();
> >             IPython.notebook.to_markdown();
> >             IPython.notebook.edit_mode();
> >         } else if (cell.cell_type == 'markdown') {
> >             IPython.notebook.command_mode();
> >             IPython.notebook.to_code();
> >             IPython.notebook.edit_mode();
> >         }
> >         return false;
> >     }
> > });
> > <==
> >
> > The only minor drawback is that this also resets the cursor back to the
> beginning of the cell (as James noticed in his
> > other email). I guess whatever the fix is would work in both cases?
> >
> > Many thanks again!
> > Max
> >
> >
> > _______________________________________________
> > IPython-dev mailing list
> > IPython-dev at scipy.org
> > http://mail.scipy.org/mailman/listinfo/ipython-dev
> >
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140421/05d54a61/attachment.html>


More information about the IPython-dev mailing list