From 03792a171787e1dc37bcac17465f21a57a210800 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 19 Aug 2025 16:06:49 +0530 Subject: [PATCH] A new --add-to-session flag for launch useful to optionally have new windows/tab join the active session when launched. --- docs/sessions.rst | 15 +++++++++++++++ kitty/boss.py | 8 ++++++++ kitty/launch.py | 19 +++++++++++++++++-- kitty/rc/launch.py | 1 + kitty/tabs.py | 2 +- 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/docs/sessions.rst b/docs/sessions.rst index 41b7ae315..f10006145 100644 --- a/docs/sessions.rst +++ b/docs/sessions.rst @@ -177,6 +177,21 @@ all the major keywords you can use in kitty session files: launch --cwd=$THIS_IS_EXPANDED some-program $THIS_IS_NOT_EXPANDED +Making newly created windows join an existing session +--------------------------------------------------------- + +Normally, after activating a session, if you create new windows/tabs +they don't belong to the session. If you would prefer to have them belong +to the currently active session, you can use the :opt:`launch --add-to-session` +option, like this: + + map kitty_mod+enter launch --add-to-session=. + +This will cause newly created windows, to belong to the currently active +session, if any. Note that adding a window to a session in this way is +temporary, it does not edit the session file, it is meant only to allow +adding of windows to sessions in the current kitty instance. + Keyword reference --------------------- diff --git a/kitty/boss.py b/kitty/boss.py index a6aa3db82..f25c1e0e0 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -1528,6 +1528,14 @@ class Boss: t = self.active_tab return None if t is None else t.active_window + @property + def active_session(self) -> str: + if t := self.active_tab: + if w := t.active_window: + return w.created_in_session_name or t.created_in_session_name + return t.created_in_session_name + return '' + def refresh_active_tab_bar(self) -> bool: tm = self.active_tab_manager if tm: diff --git a/kitty/launch.py b/kitty/launch.py index 86daeb9b9..ac75bdf65 100644 --- a/kitty/launch.py +++ b/kitty/launch.py @@ -146,6 +146,16 @@ window to connect to a remote host, you can use the :option:`--hold-after-ssh` flag to prevent the new window from closing when the connection is terminated. +--add-to-session +Add the newly created window/tab to the specified session. Use :code:`.` to add +to the currently active session, if any. See :ref:`sessions` +for what a session is and how to use one. Adding a newly created window to a session +is purely temporary, it does not change the actual session file, for that you have +to resave the session. Note that using this flag in a launch +command within a session file has no effect as the window is always added to the +session belonging to that file. + + --env {env_docs} @@ -438,7 +448,7 @@ def layer_shell_config_from_panel_opts(panel_opts: Iterable[str]) -> LayerShellC return layer_shell_config(opts) -def tab_for_window(boss: Boss, opts: LaunchCLIOptions, target_tab: Tab | None, next_to: Window | None) -> Tab: +def tab_for_window(boss: Boss, opts: LaunchCLIOptions, target_tab: Tab | None, next_to: Window | None, add_to_session: str) -> Tab: def create_tab(tm: TabManager | None = None) -> Tab: if tm is None: @@ -454,6 +464,7 @@ def tab_for_window(boss: Boss, opts: LaunchCLIOptions, target_tab: Tab | None, n tab = tm.new_tab(empty_tab=True, location=opts.location) if opts.tab_title: tab.set_title(opts.tab_title) + tab.created_in_session_name = add_to_session return tab if opts.type == 'tab': @@ -769,16 +780,20 @@ def _launch( if child_death_callback is not None: child_death_callback(0, None) else: + add_to_session = opts.add_to_session or '' + if add_to_session == '.': + add_to_session = boss.active_session kw['hold'] = opts.hold if force_target_tab and target_tab is not None: tab = target_tab else: - tab = tab_for_window(boss, opts, target_tab, next_to) + tab = tab_for_window(boss, opts, target_tab, next_to, add_to_session) watchers = load_watch_modules(opts.watcher) with Window.set_ignore_focus_changes_for_new_windows(opts.keep_focus): new_window: Window = tab.new_window( env=env or None, watchers=watchers or None, is_clone_launch=is_clone_launch, next_to=next_to, startup_command_via_shell_integration=startup_command_via_shell_integration, **kw) + new_window.created_in_session_name = add_to_session if child_death_callback is not None: boss.monitor_pid(new_window.child.pid or 0, child_death_callback) if new_window.creation_spec: diff --git a/kitty/rc/launch.py b/kitty/rc/launch.py index cf1c802d4..57a009a77 100644 --- a/kitty/rc/launch.py +++ b/kitty/rc/launch.py @@ -26,6 +26,7 @@ class Launch(RemoteCommand): source_window/str: The window to use as source for data or empty string to use active window window_title/str: Title for the new window cwd/str: Working directory for the new window + add_to_session/str: Session name to add created window/tab to env/list.str: List of environment variables of the form NAME=VALUE var/list.str: List of user variables of the form NAME=VALUE os_panel/list.str: List of panel settings diff --git a/kitty/tabs.py b/kitty/tabs.py index 0e8e6c062..584d3c000 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -258,11 +258,11 @@ class Tab: # {{{ self.new_special_window(spec) else: from .launch import launch + spec.opts.add_to_session = self.created_in_session_name launched_window = launch( boss, spec.opts, spec.args, target_tab=target_tab, force_target_tab=True, startup_command_via_shell_integration=window.run_command_at_shell_startup) if launched_window is not None: - launched_window.created_in_session_name = self.created_in_session_name launched_window.serialized_id = window.serialized_id if window.resize_spec is not None: self.resize_window(*window.resize_spec)