From 2bb5cbc95943b0168c8ceb5b639391299e767e72 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Tue, 4 Mar 2025 11:00:27 +0100 Subject: [PATCH] Default bindings for token movements v2 Commit 6af96a81a8 (Default bindings for token movement commands, 2024-10-05) has been reverted but not all docs have been. Key bindings to move by command line argument are quite intuitive, and useful when moving across URLs or other long arguments. We have redundant bindings like {alt,ctrl}-left, so let's use one of them for token movement. We don't want to break the OS-native shortcut for word movement, so use the other one on the current platform. Note that Sublime Text does something similar: it uses the native key binding for word movement, and the vacant one (e.g. `alt-left` on Linux) for sub-word movement in camel case words. While there have been 2.5 votes against making this platform dependent, the majority of feedback was in favor. This uses uname which seems wrong; we should rather use the OS that the terminal is running on. I plan to implement this in future, but there's no consensus yet on whether terminal applications should be allowed to do this. See #10926 See #11107 --- CHANGELOG.rst | 3 ++ doc_src/interactive.rst | 15 ++++++---- .../functions/__fish_shared_key_bindings.fish | 30 +++++++++++++++---- .../functions/fish_default_key_bindings.fish | 14 +++++++-- 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7adf1622a..a9720d11a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -23,6 +23,9 @@ Interactive improvements New or improved bindings ^^^^^^^^^^^^^^^^^^^^^^^^ +- On non-macOS systems, :kbd:`alt-left`, :kbd:`alt-right`, :kbd:`alt-backspace`, :kbd:`alt-delete` no longer operate on punctuation-delimited words but on whole arguments, possibly including special characters like ``/`` and quoted spaces. + On macOS, the same corresponding :kbd:`ctrl-` prefixed keys operate on whole arguments. + Word operations are still available via the other respective modifier, same as in the browser. - :kbd:`ctrl-z` (undo) after executing a command will restore the previous cursor position instead of placing the cursor at the end of the command line. - The OSC 133 prompt marking feature has learned about kitty's ``click_events=1`` flag, which allows moving fish's cursor by clicking. - :kbd:`ctrl-l` now pushes all text located above the prompt to the terminal's scrollback, before clearing and redrawing the screen (via a new special input function ``scrollback-push``). diff --git a/doc_src/interactive.rst b/doc_src/interactive.rst index 28bc55157..73f5be652 100644 --- a/doc_src/interactive.rst +++ b/doc_src/interactive.rst @@ -303,9 +303,11 @@ Some bindings are common across Emacs and vi mode, because they aren't text edit - :kbd:`alt-enter` inserts a newline at the cursor position. This is useful to add a line to a commandline that's already complete. -- :kbd:`alt-left` (``←``) and :kbd:`alt-right` (``→``) move the cursor one word left or right (to the next space or punctuation mark), or moves forward/backward in the directory history if the command line is empty. If the cursor is already at the end of the line, and an autosuggestion is available, :kbd:`alt-right` (``→``) (or :kbd:`alt-f`) accepts the first word in the suggestion. +- :kbd:`alt-left` (``←``) and :kbd:`alt-right` (``→``) move the cursor left or right by one argument (or one word on macOS). + If the command line is empty, they move forward/backward in the directory history. + If the cursor is already at the end of the line, and an autosuggestion is available, :kbd:`alt-right` (``→``) (or :kbd:`alt-f`) accepts the first argument (or word on macOS) in the suggestion. -- :kbd:`ctrl-left` (``←``) and :kbd:`ctrl-right` (``→``) move the cursor one word left or right. These accept one word of the autosuggestion - the part they'd move over. +- :kbd:`ctrl-left` (``←``) and :kbd:`ctrl-right` (``→``) move the cursor left or right by one word. These accept one word of the autosuggestion - the part they'd move over. - :kbd:`shift-left` (``←``) and :kbd:`shift-right` (``→``) move the cursor one word left or right, without stopping on punctuation. These accept one big word of the autosuggestion. @@ -325,11 +327,13 @@ Some bindings are common across Emacs and vi mode, because they aren't text edit - :kbd:`ctrl-x` copies the current buffer to the system's clipboard, :kbd:`ctrl-v` inserts the clipboard contents. (see :doc:`fish_clipboard_copy ` and :doc:`fish_clipboard_paste `) -- :kbd:`alt-d` or :kbd:`ctrl-delete` moves the next word to the :ref:`killring`. +- :kbd:`alt-d` moves the next word to the :ref:`killring`. + +- :kbd:`ctrl-delete` moves the next word (or next argument on macOS) to the :ref:`killring`. - :kbd:`alt-d` lists the directory history if the command line is empty. -- :kbd:`alt-delete` moves the next argument to the :ref:`killring`. +- :kbd:`alt-delete` moves the next argument (or word on macOS) to the :ref:`killring`. - :kbd:`shift-delete` removes the current history item or autosuggestion from the command history. @@ -370,7 +374,8 @@ To enable emacs mode, use :doc:`fish_default_key_bindings ` moves to the end. diff --git a/share/functions/__fish_shared_key_bindings.fish b/share/functions/__fish_shared_key_bindings.fish index 9b8a3410a..547f5a7e5 100644 --- a/share/functions/__fish_shared_key_bindings.fish +++ b/share/functions/__fish_shared_key_bindings.fish @@ -21,8 +21,13 @@ function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mod $legacy_bind --preset $argv -k left backward-char # Ctrl-left/right - these also work in vim. - bind --preset $argv ctrl-right forward-word - bind --preset $argv ctrl-left backward-word + if set -q XPC_FLAGS || set -q __CFBundleIdentifier || test (uname) = Darwin + bind --preset $argv ctrl-right forward-token + bind --preset $argv ctrl-left backward-token + else + bind --preset $argv ctrl-right forward-word + bind --preset $argv ctrl-left backward-word + end bind --preset $argv pageup beginning-of-history bind --preset $argv pagedown end-of-history @@ -54,10 +59,23 @@ function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mod $legacy_bind --preset $argv -k sright forward-bigword $legacy_bind --preset $argv -k sleft backward-bigword - bind --preset $argv alt-right nextd-or-forward-word - bind --preset $argv alt-left prevd-or-backward-word - $legacy_bind --preset $argv \e\[1\;9C nextd-or-forward-word # iTerm2 < 3.5.12 - $legacy_bind --preset $argv \e\[1\;9D prevd-or-backward-word # iTerm2 < 3.5.12 + set -l alt_right_aliases alt-right \e\[1\;9C # iTerm2 < 3.5.12 + set -l alt_left_aliases alt-left \e\[1\;9D # iTerm2 < 3.5.12 + if set -q XPC_FLAGS || set -q __CFBundleIdentifier || test (uname) = Darwin + for alt_right in $alt_left_aliases + bind --preset $argv $alt_right nextd-or-forward-word + end + for alt_left in $alt_left_aliases + bind --preset $argv $alt_left prevd-or-backward-word + end + else + for alt_right in $alt_left_aliases + bind --preset $argv $alt_right nextd-or-forward-token + end + for alt_left in $alt_left_aliases + bind --preset $argv $alt_left prevd-or-backward-token + end + end bind --preset $argv alt-up history-token-search-backward bind --preset $argv alt-down history-token-search-forward diff --git a/share/functions/fish_default_key_bindings.fish b/share/functions/fish_default_key_bindings.fish index 9dadf056f..82f94f5f0 100644 --- a/share/functions/fish_default_key_bindings.fish +++ b/share/functions/fish_default_key_bindings.fish @@ -55,9 +55,17 @@ function fish_default_key_bindings -d "emacs-like key binds" bind --preset $argv alt-u upcase-word bind --preset $argv alt-c capitalize-word - bind --preset $argv alt-backspace backward-kill-word - bind --preset $argv ctrl-backspace backward-kill-word - bind --preset $argv ctrl-delete kill-word + if set -q XPC_FLAGS || set -q __CFBundleIdentifier || test (uname) = Darwin + bind --preset $argv alt-backspace backward-kill-word + bind --preset $argv ctrl-backspace backward-kill-token + bind --preset $argv alt-delete kill-word + bind --preset $argv ctrl-delete kill-token + else + bind --preset $argv alt-backspace backward-kill-token + bind --preset $argv ctrl-backspace backward-kill-word + bind --preset $argv alt-delete kill-token + bind --preset $argv ctrl-delete kill-word + end bind --preset $argv alt-b prevd-or-backward-word bind --preset $argv alt-f nextd-or-forward-word