diff --git a/docs/changelog.rst b/docs/changelog.rst index 3a620f632..2ea8c694f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -202,6 +202,9 @@ Detailed list of changes - Add ``state:focused_os_window`` match query to select all windows in the currently focused OS window (:ref:`search_syntax`) +- Session saving now automatically preserves which tab is active in each OS + window by adding a ``focus_tab`` command to saved session files + 0.43.1 [2025-10-01] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/tabs.py b/kitty/tabs.py index 4f557f42b..158bf9bfa 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -1314,12 +1314,10 @@ class TabManager: # {{{ is_first: bool = False ) -> list[str]: ans = [] - hmap = {tab_id: i for i, tab_id in enumerate(self.active_tab_history)} - if (at := self.active_tab) is not None: - hmap[at.id] = len(self.active_tab_history) + 1 - def skey(tab: Tab) -> int: - return hmap.get(tab.id, -1) - for tab in sorted(self, key=skey): + active_tab_index = -1 + for i, tab in enumerate(self.tabs): + if tab is self.active_tab: + active_tab_index = i ans.extend(tab.serialize_state_as_session(session_path, matched_windows, ser_opts)) if ans: prefix = [] if is_first else ['', '', 'new_os_window'] @@ -1328,6 +1326,10 @@ class TabManager: # {{{ if self.wm_name and self.wm_name != appname: prefix.append(f'os_window_name {self.wm_name}') ans = prefix + ans + # Add focus_tab command to preserve the active tab + if active_tab_index >= 0: + ans.append('') + ans.append(f'focus_tab {active_tab_index}') return ans @property