From dfbf5103c23040957077a4c8b9ed87ae866bec51 Mon Sep 17 00:00:00 2001 From: Jackie Li Date: Mon, 27 Oct 2025 08:47:35 +0000 Subject: [PATCH 1/2] add focus_tab to restore tab focus by default --- docs/changelog.rst | 3 +++ kitty/tabs.py | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 0cab22ad2..94772dbea 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -196,6 +196,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 d2eafa2f3..bc89f4e96 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -1317,7 +1317,10 @@ class TabManager: # {{{ 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(sorted(self, key=skey)): + 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'] @@ -1326,6 +1329,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 From 23f49e38251a2a0dd21e2d4f4c1f68f8cb267dde Mon Sep 17 00:00:00 2001 From: Jackie Li Date: Mon, 27 Oct 2025 08:56:34 +0000 Subject: [PATCH 2/2] serialised tabs as they appear visually --- kitty/tabs.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/kitty/tabs.py b/kitty/tabs.py index bc89f4e96..a509a1099 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -1312,13 +1312,8 @@ 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) active_tab_index = -1 - for i, tab in enumerate(sorted(self, key=skey)): + 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))