Sublime Forum

[SOLUTION] Undo and Redo to not move the cursor or the viewport


It is well known, and not loved, that Undo and Redo returns to the current cursor position. While this “feature” may be working as designed, some users–arguably many–consider it a bug, a bug to which there is apparently still no workaround, as Soft Undo and Soft Redo also zoom to the cursor.

So that one may see undone and redone changes independent of the current cursor position, how about a setting or settings to not zoom to the cursor on undo and redo?


Undo and redo

Can you describe the problem, because a workaround may be there



Too see the full range of behavior:

  1. Have over 3 times as many lines as are visible in the viewport.
  2. Make an edit in around the middle line.
  3. Move the cursor so the edited line is not in the viewport.
  4. Undo. The cursor and viewport move to the positions they were in at step 2.
  5. Move the cursor so the edited line is not in the viewport.
  6. Redo. The cursor and viewport move to the positions they were in at step 2.

When undoing and redoing, the cursor and the viewport should not move.



This should workaround the situation, you save it as (main menu bar - preferences - browse pacakges - user folder) something like Packages/User/

Basically, get_regions has a bug that you cannot get the regions after doing undo, so what I did was to fold the thing, and unfold it after undo. That way I can restore the cursors in the correct position. But because of this bug, you lose any fold you have.



This seems to partially work. It also 1. shouldn’t recenter the view on the previous cursor position and 2. redo isn’t incorporated.

Bug: Undoing when the cursor is on the last line makes the cursor disappear such that nothing can be typed unless you click somewhere.



I forgot about redo, there I updated with redo support and out of bounds ugly hack check


shouldn’t recenter the view on the previous cursor position and

idk what you mean, here screen does not move.



The update fixes the previous bounds bug but it still doesn’t work quite right.


  • Redo moves the cursor and it may scroll the view.
  • Undo doesn’t move the cursor, unless you Undo and Redo and then Undo again on the same line, and it may scroll the view.

I’ve even disabled many packages just in case of any conflicts. Windows 3103.



There I fixed the viewport moving when the cursor is in another page than the position you are undoing/redoing, about other issues, please give a step by step way to repro.



Undo doesn’t move the display but it disappears the cursor; when you then arrow the cursor, it appears where it should be.

Redo moves the cursor and, if applicable, the viewport.



i dont understand



What do you not understand?

The objectives are 1. to make Undo and Redo never move the cursor to a different line (because, out of the box, they do), and 2. to make Undo and Redo never change which lines are visible, or in view (the viewport).

The current state of your fix–which I’m grateful you are creating–only partially accomplishes the first objective.



Yes, we agree, It works for me. cursor does not move and screen stay in the same position even if undoing something in a non visible area of the screen. So, if something does not work, please check with a portable version of ST, and be sure you have the latest ST and “plugin” installed. After that, if you have any problem. Describe it completely, please. Seomthing like, Have text “aaa” move cursor to line 4 place cursor in column 9 then … etc, etc Because it seems to me we are not seeing the same thing. So maybe describing a very specific case can help debug the issue if any.

To clarify your test case in item2 works ok to me. It does what you desire in this thread.



ignore. sdf sd fdsf



OK for some reason, in my setup works as you expect. But on a vanilla ST it does not work as we expect. I found another bug in the selection ST API, the ST API has too many bugs. I found like three bugs in a row just now.



ok, there I updated with more workarounds and hopefully will work as we expect. Please let me know.



Bugs in the ST API, 'eh? That ought to get the attention of JPS. :smiley:

The latest update doesn’t seem to change the behavior at all from the default, on Windows 3103 x64 and on vanilla x64 portable.

If it still may be of use, here is my test case:

  1. Vertically size ST to 30 lines
  2. Have 90 populated lines (I used a 5-unique-line repeated pattern with line 5 blank)
  3. Center the viewport and place the cursor on line 44
  4. Do a find and replace of the unique part of line 44 (regex or not)
  5. Scroll until line 1 is visible
  6. Place the cursor on one of the visible lines
  7. Undo
  8. Scroll until line 90 is visible
  9. Place the cursor on one of the visible lines
  10. Redo

Currently, steps 7 and 9 recenter the viewport on and move the cursor back to line 44, which the objectives are to stop.

And how did you spice up your ST? :slightly_smiling:



Quite challenging,

  1. Opened portable vanilla ST
  2. I did make sure I have latest the plugin and nothing more (no other packages, no preferences changed)
  3. Restarted
  4. Did you test case
  5. It works, no recenter of viewport and no move of the cursor.

Here have screen recorder with what I did

I think is safe to assume ST is not mature enough.

  1. get_regions does not work after undo/redo
  2. sel() does not update the cursor position on the screen
  3. sel() lies with the cursor positions, as is reporting other position than the actually position
  4. set_viewport_position, does not set the viewport.


Unfortunate conclusion about the ST API but excellent workarounds! Thank you! However, Undo and Redo on the last line moves the cursor up one line.

Although one difference in our test case executions is that I use Replace All, this doesn’t seem to matter.

Now, I finally noticed that my Redo wasn’t working because I had remapped it from the default redo_or_repeat to redo, and the plugin wasn’t listening for redo. So, I added all redo and undo variants and it seems to work in every case in my installed instance but not at all on vanilla:

if command_name == ‘undo’ or command_name == ‘soft_undo’ or command_name == ‘redo’ or command_name == ‘soft_redo’ or command_name == ‘redo_or_repeat’:



Did you try to put your code in a TextCommand ?
If I remember well there’s some issue with refreshing the view when you do this kind of thinks out of a begin_edit/end_edit block.



idk… maybe works… yes I trust you, but this is supposed to work out of the box…why is there if does not works? Also I have kinda similar problems in other packages that does not use the on_after_something commands… bufferscroll is for example a package that discovered many bugs, and the bugs are there in same shape after many years… whats the point of creating an API if remains broken… something that is supposed to be done with a few lines, needs from many silly and ugly(I like this word, because I’m ugly too) [have you read my code?] workarounds, is very frustrating. …

Thanks also @ParticleMan Im gonna add these similar commands too, glad you got it working. :confused: