From a5433b89941d5c6404239fc4a1d017fa31ac21bb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 26 Jan 2026 10:45:17 +0530 Subject: [PATCH] kitten choose-files: Add a new binding of Alt+Enter to modify the name of an existing file when choosing a save file name Fixes #9387 --- docs/changelog.rst | 6 ++++++ docs/kittens/choose-files.rst | 4 +++- kittens/choose_files/main.go | 26 ++++++++++++++++++++++++++ kittens/choose_files/main.py | 7 +++++++ kittens/choose_files/save-file.go | 2 ++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 8ee8c861c..f904d7ba3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -205,6 +205,12 @@ Detailed list of changes - kitten @: Fix relative paths for --password-file being resolved relative to CWD instead of the kitty config directory +- kitten choose-files: Add a new binding of :kbd:`Alt+Enter` to modify the name + of an existing file when choosing a save file name (:iss:`9387`) + +- kitten choose-files: Fix TAB completion in the choose save file name prompt + not working with respect to the current working directory (:iss:`9387`) + 0.45.0 [2025-12-24] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/kittens/choose-files.rst b/docs/kittens/choose-files.rst index e4c45a850..1bde11733 100644 --- a/docs/kittens/choose-files.rst +++ b/docs/kittens/choose-files.rst @@ -99,7 +99,9 @@ This kitten can also be used to select non-existent files, that is a new file for a :guilabel:`Save file` type of dialog using :option:`--mode `:code:`=save-file`. Once you have changed to the directory you want the file to be in (using the :kbd:`Tab` key), -press :kbd:`Ctrl+Enter` and you will be able to type in the file name. +press :kbd:`Ctrl+Enter` and you will be able to type in the file name. If you +wish to modify an existing file name use :kbd:`Alt+Enter` to modify the +filename of the current top match instead. Selecting directories diff --git a/kittens/choose_files/main.go b/kittens/choose_files/main.go index 8e13a8f34..a1240ab1b 100644 --- a/kittens/choose_files/main.go +++ b/kittens/choose_files/main.go @@ -479,6 +479,26 @@ func (h *Handler) switch_to_save_file_name_mode() error { if h.state.SearchText() != "" { name = h.state.SearchText() } + if name == "" { + name = h.current_abspath() + if name != "" { + if r, err := filepath.Rel(h.state.CurrentDir(), name); err == nil { + name = r + } + } + } + h.initialize_save_file_name(name) + return h.draw_screen() +} + +func (h *Handler) switch_to_save_file_name_mode_based_on_existing() error { + name := h.current_abspath() + if name == "" { + return h.switch_to_save_file_name_mode() + } + if r, err := filepath.Rel(h.state.CurrentDir(), name); err == nil { + name = r + } h.initialize_save_file_name(name) return h.draw_screen() } @@ -573,6 +593,12 @@ func (h *Handler) dispatch_action(name, args string) (err error) { } else { return h.switch_to_save_file_name_mode() } + case "modifyname": + if h.state.mode.CanSelectNonExistent() { + return h.switch_to_save_file_name_mode_based_on_existing() + } else { + h.lp.Beep() + } case "toggle": switch args { case "preview": diff --git a/kittens/choose_files/main.py b/kittens/choose_files/main.py index f11295fbb..f01aeb044 100644 --- a/kittens/choose_files/main.py +++ b/kittens/choose_files/main.py @@ -133,6 +133,13 @@ When choosing existing directories, will accept the directory whoose contents are being currently displayed as the choice. Does not work when selecting files to open rather than to save. ''') +map('Modify file name', 'modifyname alt+enter modifyname', long_text=''' +Modify the name of an existing file and select it for saving. +Useful when specifying a file or directory name for saving that does not yet exist, +but is based on an existing file name. +Does not work when selecting files to open rather than to save. +''') + map('Next result', 'next_result down next 1') map('Previous result', 'prev_result up next -1') diff --git a/kittens/choose_files/save-file.go b/kittens/choose_files/save-file.go index 42c6480c7..e94319494 100644 --- a/kittens/choose_files/save-file.go +++ b/kittens/choose_files/save-file.go @@ -24,6 +24,7 @@ func FilePromptCompleter(getcwd func() string) func(string, string) *cli.Complet return ans } } + debugprintln(1111111, getcwd()) return func(before_cursor, after_cursor string) (ans *cli.Completions) { defer func() { if r := recover(); r != nil { @@ -39,6 +40,7 @@ func FilePromptCompleter(getcwd func() string) func(string, string) *cli.Complet prefix = d + utils.IfElse(strings.HasSuffix(d, string(os.PathSeparator)), "", string(os.PathSeparator)) } dir := "" + debugprintln(2222222, getcwd()) if path == "" { path = getcwd() dir = path