Skip to content

Commit 7fbee93

Browse files
authored
Do not submit command when user presses enter to select a completion. (#1613)
1 parent 53a5c0f commit 7fbee93

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

cmd2/cmd2.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,17 @@ def _create_main_session(self, auto_suggest: bool, completekey: str) -> PromptSe
684684
Otherwise, uses dummy drivers to support non-interactive streams like
685685
pipes or files.
686686
"""
687-
key_bindings = None
687+
# Configure custom key bindings
688+
key_bindings = KeyBindings()
689+
690+
# Add a binding for 'enter' that triggers only when a completion is selected.
691+
# This allows accepting a completion without submitting the command.
692+
@key_bindings.add('enter', filter=filters.completion_is_selected)
693+
def _(event: Any) -> None: # pragma: no cover
694+
event.current_buffer.complete_state = None
695+
688696
if completekey != self.DEFAULT_COMPLETEKEY:
689697
# Configure prompt_toolkit `KeyBindings` with the custom key for completion
690-
key_bindings = KeyBindings()
691-
692698
@key_bindings.add(completekey)
693699
def _(event: Any) -> None: # pragma: no cover
694700
"""Trigger completion."""

0 commit comments

Comments
 (0)