Is there a shortcut to undo a viewport move?
I encountered many bugs so this is more of a proof of concept than ready solution
I adapted the code from here.
Run the below code, make some changes to viewport and press CTRL + SHIFT + F12 to undo viewport changes (keeping track of a few more properties has to be added e.g. orthogonal/perspective)
bl_info = {
    &quot;name&quot;: &quot;Work Macro&quot;,
    &quot;category&quot;: &quot;Object&quot;,
}

import bpy, time
from collections import defaultdict

timeout = 1  # number of seconds between changes justifying a separate state
undo_buffer_size = 100  # in this crude implementation, each buffer will grow
                        # to double its limit before being truncated to the limit

states = defaultdict(list) # key: space object, value: list of tuples(last_change, view_matrix)
view_handler = None


def callback():
    # perhaps you can only use context.area.spaces.active instead of iterating here
    areas = bpy.data.window_managers[0].windows[0].screen.areas
    for area in (a for a in areas if a.type == 'VIEW_3D'):
        for space in (s for s in area.spaces if s.type == 'VIEW_3D'):
            space_states = states[space]
            if not space_states:  # empty list
                space_states.append((time.time(), space.region_3d.view_matrix.copy()))
            else:
                last_time, view_matrix = states[space][-1]
                matrix_change = view_matrix != space.region_3d.view_matrix
                if matrix_change and time.time() - last_time &gt; timeout:
                    states[space].append((time.time(), space.region_3d.view_matrix.copy()))
                    if len(states[space]) &gt; undo_buffer_size * 2:
                        states[space] = states[space][-undo_buffer_size:]    


class WorkMacro(bpy.types.Operator):
    &quot;&quot;&quot;Work Macro&quot;&quot;&quot;
    bl_idname = &quot;object.work_macro&quot;
    bl_label = &quot;Work Macro&quot;
    bl_options = set()

    def execute(self, context):
        space = context.area.spaces.active
        if len(states[space]) &gt; 1:
            states[space].pop()  # remove current state
        revert_to = states[space][-1][1].copy()
        space.region_3d.view_matrix = revert_to 
        #sometimes the above will slightly modify the matrix so let's update the undo state with it:
        states[space][-1] = (time.time(), space.region_3d.view_matrix.copy())
        return {'FINISHED'}


# store keymaps here to access after registration
addon_keymaps = []


def register():
    bpy.utils.register_class(WorkMacro)

    # handle the keymap
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='3D View Generic', space_type='VIEW_3D')
    kmi = km.keymap_items.new(WorkMacro.bl_idname, 'F12', 'PRESS', ctrl=True, shift=True)
    addon_keymaps.append(km)
    global view_handler
    view_handler = bpy.types.SpaceView3D.draw_handler_add(callback, (), 'WINDOW', 'POST_VIEW') 
    

def unregister():
    print('unregister')
    bpy.utils.unregister_class(WorkMacro)

    # handle the keymap
    wm = bpy.context.window_manager
    for km in addon_keymaps:
        wm.keyconfigs.addon.keymaps.remove(km)
    # clear the list
    del addon_keymaps[:]
    bpy.types.SpaceView3D.draw_handler_remove(view_handler, 'WINDOW')  # remove handler added on end of register() function


if __name__ == &quot;__main__&quot;:
    register()