feat(file-manager): sync the working directory with the shell

The panel and the shell each kept their own directory. The new sync_cwd option
keeps them in step both ways, through files in a session directory exported by
kitty rather than by typing a command into the terminal: the shell applies the
directory chosen in the panel from its prompt hook with builtin cd, so nothing
lands in the history and a half typed multi-line command is not submitted.
This commit is contained in:
BlackAngelTV committed 2026-11-11 08:20:11 +01:00
1 parent f16ed6cbfe
commit 1b330bcf81
12 files changed
+425 -6

No files matched your search

+16
View File
@@ -3,6 +3,7 @@
import os
import sys
import tempfile
import termios
from collections import defaultdict
from collections.abc import Callable, Generator, Iterable, Mapping, Sequence
@@ -21,6 +22,20 @@ if TYPE_CHECKING:
from .window import CwdRequest
@run_once
def cwd_sync_dir() -> str:
"""Directory shared by every window of this instance for the file manager's
working directory sync. It is created with private permissions so that only
this user can read the paths the shells publish, and lives in the runtime
directory, which the login session already clears. An empty string means the
sync is unavailable and the participating kittens keep their own behaviour."""
base = os.environ.get('XDG_RUNTIME_DIR')
try:
return tempfile.mkdtemp(prefix='kitty-cwd-sync-', dir=base if base and os.path.isdir(base) else None)
except OSError:
return ''
if is_macos:
from kitty.fast_data_types import abspath_of_process as _abspath_of_process
from kitty.fast_data_types import cmdline_of_process as cmdline_
@@ -464,6 +479,7 @@ class Child:
elif opts.terminfo_type == 'direct':
env['TERMINFO'] = base64_terminfo_data()
env['KITTY_INSTALLATION_DIR'] = kitty_base_dir
env['KITTY_CWD_SYNC_DIR'] = cwd_sync_dir()
self.unmodified_argv = list(self.argv)
if not self.should_run_via_run_shell_kitten and 'disabled' not in opts.shell_integration:
from .shell_integration import modify_shell_environ
+11
View File
@@ -927,6 +927,17 @@ class Tab: # {{{
restored_scrollback: bytes = b'',
) -> Window:
"Create a new window. When dock is specified, the window is docked to dock_owner or to the tab if dock_owner is None."
# A special window opened over another one, or docked next to it, needs
# to know which shell it belongs to: the file manager uses it to find
# the session file where that shell publishes its working directory. A
# dock follows a window group, so its initial parent is the active
# window of the tab it is created in.
parent_window_id = overlay_for
if parent_window_id is None and dock is not None and (aw := self.active_window) is not None:
parent_window_id = aw.id
if parent_window_id is not None:
env = dict(env or {})
env['KITTY_SPECIAL_WINDOW_PARENT'] = str(parent_window_id)
cs = WindowCreationSpec(
use_shell=use_shell,
cmd=cmd,