14
$\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$
8
$\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$

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.