19
$\begingroup$

By default, after evaluating an input cell by hitting Enter or Shift-Return the cursor jumps below the corresponding output cell. Is there a way to make the cursor stay exactly where it was before the evaluation?

$\endgroup$
0

1 Answer 1

10
$\begingroup$

Undocumented tweak:

With[
  { nb := EvaluationNotebook[]
  , c := EvaluationCell[]
  }
, With[
    { cv := CurrentValue[nb, {"TaggingRules", "LastCursorPosition"}]
    , pos := FrontEndExecute@FrontEnd`UndocumentedGetSelectionPacket[nb]
    }
  , With[
      { savePosition := (
          cv = If[MemberQ[pos, "CharacterRange" -> _], Last["CharacterRange" /. pos], False]
        )
      }
    , SetOptions[nb
      , CellEventActions :> {
          {"MenuCommand", "HandleShiftReturn"} :> savePosition
        , {"MenuCommand", "EvaluateCells"} :> savePosition
        , PassEventsDown -> True
        }
      , CellEpilog :>  If[
          IntegerQ[cv]
        , SelectionMove[c, Before, CellContents]
        ; SelectionMove[nb, Next, Character, cv]
        ; cv = False
        ]
      ]
    ]
  ]
]

So it doesn't stay but at least it goes back :) Haven't tested it heavily though.

$\endgroup$
3
  • 1
    $\begingroup$ Instead of using FrontEndExecute@FrontEnd`UndocumentedGetSelectionPacket[nb] one might also use Developer`CellInformation[nb] which also contains the selected character range. See comment under the answer: mathematica.stackexchange.com/questions/13246/… $\endgroup$
    – Qbyte
    Commented Feb 1, 2021 at 20:11
  • $\begingroup$ @Kuba Your code was applied to Wolfram Cloud, but now evaluation using shift+enter does not work. The software should have gone back to normal after refreshing it. It likely is a bug $\endgroup$ Commented Mar 21 at 23:43
  • 1
    $\begingroup$ @TymaGaidash you can CurrentValue[ notebook_, CellEpilog ] = Inherited etc to reset them. $\endgroup$
    – Kuba
    Commented Mar 22 at 5:33

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.