2
$\begingroup$

Often I'll have a block that runs something like a random test, and I'll want to run it maybe a couple dozen times by hand, tweaking as necessary. I'd like to be able to hit some key or shortcut to re-run the last statement. What I've always done so far is click back in the code block and Shift+Enter again, which gets old after you've done it a thousand times.

What I'm not looking for:

  • Ctrl-L
  • Hitting Up to move the cursor back to the block (often the output is in the way and it's less practical than the mouse)
  • Using Manipulate or what have you to make things easier

Basically, if such a shortcut exists or is trivial to fabricate, please share... otherwise, I've got to admit that this is such a minor complaint that a proper solution is not worth the investment.

$\endgroup$
2
  • $\begingroup$ you can use the % sign? $\endgroup$
    – blacke23
    Jun 9, 2021 at 9:00
  • $\begingroup$ Hit Shift-Up, Up, Shift-Enter. This skips over the output with two keystrokes regardless of how long the output is. $\endgroup$
    – Szabolcs
    Jun 9, 2021 at 9:07

2 Answers 2

1
$\begingroup$

The cell below can be rerun just using Shift-Enter multiple times.

a = If[ValueQ[a], a + 1, 1]
SelectionMove[EvaluationNotebook[], Previous, Cell, 2]
$\endgroup$
2
  • $\begingroup$ Perfect, thanks, this is just the level of trouble I'm happy to go to. I'm sticking reload := SelectionMove[EvaluationNotebook[], Previous, Cell, 2] into my standard init.m, and now I can just toss in a reload at the end of a block and I'm good to go. $\endgroup$
    – Trev
    Jun 12, 2021 at 3:01
  • $\begingroup$ You might find this useful too, for selecting additional cells : FrontEndExecute[FrontEndToken[EvaluationNotebook[], "SelectNextLine"]] e.g. as used here. It can also accept the "SelectPreviousLine" token. $\endgroup$ Jun 12, 2021 at 8:23
1
$\begingroup$

What I'm not looking for:

  • Hitting Up to move the cursor back to the block (often the output is in the way and it's less practical than the mouse)

As I understand, the reason you don't want to use the Up key is that if the output is long, it takes many keypresses to skip over it.

I usually use Shift-Up, Up, Shift-Enter to re-evaluate the last input. Shift-Up selects the entire output, then Up moves the selection to the previous cell (i.e. the last input). The advantage is that two keystrokes are always sufficient to go back to the last input, regardless of how large the output cell is.

This won't be able to skip over several Printed or Echoed cells in a single step though.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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