feat(file-manager): open files in the editor from the panel

Files used to open in a new tab, which loses the shell the user was
working in. Hand the editor command to that shell instead, optionally
through sudo for files owned by root.

A docked panel cannot do that: kitty never routes the result of a
launch --type=window-dock back to the shell, so Enter used to close the
panel without opening anything. Add --open-here, which suspends the
panel and runs the editor in its own window. The editor is resolved
from the new `editor` option, because kitty's own editor option is not
visible to a process started with launch.
This commit is contained in:
BlackAngelTV committed 2026-11-05 15:48:01 +01:00
1 parent fb87a7306f
commit 86982b79be
3 files changed
+212 -15

No files matched your search

+53 -13
View File
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# License: GPL v3 Copyright: 2026, FWS
import os
import shlex
import sys
from functools import partial
@@ -48,6 +47,33 @@ sorted by last modification time, most recent first.
egr()
agr('opening', 'Opening files')
opt(
'editor',
'',
long_text="""
Command used to open a file when the panel opens it itself, that is when
:code:`--open-here` is given. When empty, :envvar:`VISUAL` then
:envvar:`EDITOR` are used, falling back to :code:`vi`. kitty's own :opt:`editor`
option is not visible to a panel started with :code:`launch`, so set this option
when the panel runs in a dock and the two differ.
""",
)
opt(
'open_with_sudo',
'no',
option_type='to_bool',
long_text="""
Prefix the editor command with :code:`sudo` when opening a file, so that files
the user cannot write to, such as system configuration files, can still be
edited. The password prompt, if any, appears in the shell that hosts the panel.
""",
)
egr()
agr('shortcuts', 'Keyboard shortcuts')
map('Quit without changing directory', 'quit q quit')
@@ -69,7 +95,11 @@ map(
'activate enter activate',
long_text="""
When the selection is a directory it becomes the working directory of the shell
that launched the panel. When it is a file, a new tab is opened on its directory.
that launched the panel. When it is a file, the shell receives the command that
opens it in the editor configured with the :opt:`editor` option,
:envvar:`VISUAL` or :envvar:`EDITOR`. With :code:`--open-here` the panel runs the
editor in its own window instead, which is what makes this action work in a
docked panel.
""",
)
map(
@@ -122,6 +152,13 @@ type=bool-set
Show hidden files when the panel opens.
--open-here
type=bool-set
Open files in the panel's own window instead of sending the editor command to
the shell. Required in a window dock, where kitty never hands the result back to
the shell.
--config -c
type=list
completion=type:file ext:conf group:"Config files" kwds:none,NONE
@@ -153,17 +190,20 @@ def change_directory(w: Any, path: str) -> None:
w.paste_text(cmd)
def open_in_new_tab(boss: BossType, w: Any, path: str) -> None:
from kitty.tabs import SpecialWindow
def open_file(w: Any, path: str, use_sudo: bool = False) -> None:
# Run the editor in the shell that hosted the panel, like the cd action, so
# the file opens where the user already is instead of in a new tab.
from kitty.utils import get_editor
tab = w.tabref()
tm = tab.tab_manager_ref() if tab is not None else None
if tm is None:
boss.ring_bell_if_allowed()
return
cwd = os.path.dirname(path) or None
shell = os.environ.get('SHELL') or '/bin/sh'
tm.new_tab(SpecialWindow(cmd=[shell], cwd=cwd))
cmd = get_editor(path_to_edit=path)
if use_sudo:
# -- stops sudo from eating editor arguments that look like its own options.
cmd = ['sudo', '--'] + cmd
text = shlex.join(cmd)
if w.at_prompt:
w.write_to_child(text + '\r')
else:
w.paste_text(text)
@result_handler(has_ready_notification=True)
@@ -180,7 +220,7 @@ def handle_result(args: list[str], data: dict[str, Any], target_window_id: int,
if action == 'cd':
change_directory(w, path)
elif action == 'open':
open_in_new_tab(boss, w, path)
open_file(w, path, data.get('sudo') == '1')
help_text = 'Browse directories in a side panel and change the working directory of the shell without leaving it'