From 185ed8768435d6846cfbe36fc99a38d4227e0aee Mon Sep 17 00:00:00 2001 From: leo7martins <219400372+leo7martins@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:52:58 +0000 Subject: [PATCH] Add yank-last-arg to Vi insert mode via Meta-'.' and Meta-'_' Bind the existing yank-last-arg named command ('escape'+'.' and 'escape'+'_') in Vi insert mode, matching the Emacs bindings and readline's default vi-insert behavior (bash 'set -o vi' users get M-./M-_ for yank-last-arg as well). Fixes #1905. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- src/prompt_toolkit/key_binding/bindings/vi.py | 5 +++++ tests/test_cli.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/prompt_toolkit/key_binding/bindings/vi.py b/src/prompt_toolkit/key_binding/bindings/vi.py index e3fca1b98..cca59558e 100644 --- a/src/prompt_toolkit/key_binding/bindings/vi.py +++ b/src/prompt_toolkit/key_binding/bindings/vi.py @@ -448,6 +448,11 @@ def load_vi_bindings() -> KeyBindingsBase: # Insert a character literally (quoted insert). handle("c-v", filter=vi_insert_mode)(get_by_name("quoted-insert")) + # Yank the last argument of the previous entry, like the Emacs + # bindings and readline's default Vi-insert bindings. (Issue #1905.) + handle("escape", ".", filter=vi_insert_mode)(get_by_name("yank-last-arg")) + handle("escape", "_", filter=vi_insert_mode)(get_by_name("yank-last-arg")) + @handle("escape") def _back_to_navigation(event: E) -> None: """ diff --git a/tests/test_cli.py b/tests/test_cli.py index a876f2993..8aafb8508 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -940,3 +940,23 @@ def test_accept_default(): # prompt reset between calls happened at the wrong time, breaking this.) result = session.prompt(default="world", accept_default=True) assert result == "world" + + +def test_vi_yank_last_arg(): + """ + In Vi insert mode, Meta-'.' and Meta-'_' yank the last argument of the + previous entry, like the Emacs bindings and readline's default + Vi-insert bindings. (Issue #1905.) + """ + history = InMemoryHistory() + history.append_string("first one two") + + feed = partial(_feed_cli_with_input, editing_mode=EditingMode.VI, history=history) + + # Meta-'.' inserts the last word of the previous entry. + result, cli = feed("test \x1b.\r") + assert result.text == "test two" + + # Alias: Meta-'_'. + result, cli = feed("test \x1b_\r") + assert result.text == "test two"