feat(session): restore the last session from a keybinding

Besides the --session=last startup option, the saved state can now be brought
back in a running kitty with restore_last_session, mapped to kitty_mod+alt+r.
The option types are regenerated so the shortcut is actually part of the
default configuration.
This commit is contained in:
BlackAngelTV committed 2026-11-11 21:39:45 +01:00
1 parent d7eca9efbb
commit dbcb364526
7 files changed
+62 -1

No files matched your search

+25
View File
@@ -10,6 +10,7 @@
import json
import os
import time
from contextlib import suppress
from typing import TYPE_CHECKING, Any
from .config import atomic_save
@@ -135,3 +136,27 @@ def save_session_of(boss: 'BossType') -> None:
save_last_session(session)
except Exception as e:
log_error(f'Failed to save the last session: {e}')
def restore_last_session_into(boss: 'BossType') -> bool:
"""Add the tabs and windows of the last saved session to a running kitty.
Returns False when there is nothing to restore. The saved state is a JSON document, so it is
handed to the session parser through a short lived file rather than duplicating the parsing.
"""
raw = load_last_session()
if not raw:
return False
import tempfile
from .session import create_session
fd, path = tempfile.mkstemp(prefix='last-session-', suffix='.kitty-session')
try:
with open(fd, 'w', encoding='utf-8') as f:
f.write(raw)
create_session(boss, path)
finally:
with suppress(OSError):
os.unlink(path)
return True