6
$\begingroup$

It is very useful during refactoring to move lines around. All popular IDEs or text editors have this feature (PyCharm, Vim, Sublime, etc..) for example in Xcode you press: ⌥⌘[ or ⌥⌘] to move lines up and down.

Question: Is there a shortcut for reordering cells like this, i.e. moving the cell your cursor is in up one cell or down one cell? Effectively, this would be the same as cutting the current cell and pasting before the previous cell or after the next one.

$\endgroup$
  • $\begingroup$ I guess you would need to get the list of Cells and bind the reordering to some key strokes $\endgroup$ – Mike Honeychurch Feb 4 '15 at 0:11
  • 1
    $\begingroup$ But say you have a huge notebook, don't we just want to look at the nearby cells? @MikeHoneychurch $\endgroup$ – M.R. Feb 4 '15 at 0:35
  • $\begingroup$ related: 66626 $\endgroup$ – Kuba Feb 4 '15 at 9:20
  • $\begingroup$ Thanks for the Accept. Let me know if you have any problems with my code or find the need to extend it in a way that you do not know how. $\endgroup$ – Mr.Wizard Feb 4 '15 at 13:27
6
$\begingroup$

Here is a quick pass at a Palette that does this. The same commands could be used for keyboard commands in KeyEventTranslations.tr. This is not rigorously written or tested so be careful with it.

mover[label_, c_List] :=
 Button[label,
  With[{nb = SelectedNotebook[]},
   (NotebookWrite[nb, #2];
      SelectionMove[nb, Previous, Cell]) &[
    SelectionMove[nb, All, Cell],
    NotebookRead[SelectedCells[nb]],
    NotebookDelete[nb],
    SelectionMove[nb, #, Cell] & /@ c
    ]
   ]
  ]

{mover["cell up", {Previous, Before}],
 mover["cell down", {Next, After}]} // CreatePalette

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ @Kuba In reverse order: (2) yes, just a way to avoid a Symbol to hold the Cell. (1) On my system I can already repeat click to move up and down more than one place, unless I (again?!) pasted the wrong code here. Which OS do you run again? $\endgroup$ – Mr.Wizard Feb 4 '15 at 10:03
  • $\begingroup$ @Kuba I tested it again, and in my 10.0.2 under Windows 7-x64 it works. After NotebookWrite the cursor is below the newly inserted cell, and after SelectionMove[nb, Previous, Cell] the original cell (in its new location) is selected. Really weird if you're seeing something else. I have the Suggestion Bar turned off; do you have it on by chance? $\endgroup$ – Mr.Wizard Feb 4 '15 at 10:10
  • $\begingroup$ @Kuba Yes, I checked a third time. However I'm not sure why I put [[1]] there and now I think it would be better without it. If you leave that out does this work better for you? $\endgroup$ – Mr.Wizard Feb 4 '15 at 10:24
  • $\begingroup$ @Kuba For me the output of NotebookRead[SelectedCells[nb]] is a list, e.g. {Cell[BoxData["foo"], "Input"]} -- to get the first Cell I used [[1]]. I'll remove that from my answer however as it is not needed on my system and apparently breaks something on yours. $\endgroup$ – Mr.Wizard Feb 4 '15 at 10:42
  • 1
    $\begingroup$ Ok, now I've patiently tested this and it seems there is a difference between V9 and V10. On V9 NotebookRead[SelectedCells[nb]] gives Cell[_] if SelectedCells has length 1. I've probably tested this in hurry earlier and haven't realised that it was on 9. Nevertheless, it's better to have something more general :) $\endgroup$ – Kuba Feb 4 '15 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.