From e89057b70cda630b061ddacdd633fdb81b865b23 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sat, 3 Sep 2016 23:00:41 +0200 Subject: [PATCH] Make hybrid bindings easier to achieve The vi-bindings function would unconditionally erase all bindings, making it impossible to call it last. This would disable the mode-indicator (and in future also the cursor). Make it so any argument to fish_vi_key_bindings stops it from erasing bindings. It would also be possible to demand an argument to erase (or to erase as a separate step). but the usual case seems to be _switching_ to a set of bindings. --- doc_src/index.hdr.in | 13 +++++++++++++ share/functions/fish_default_key_bindings.fish | 6 +++--- share/functions/fish_vi_key_bindings.fish | 6 ++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/doc_src/index.hdr.in b/doc_src/index.hdr.in index 9dca7f770..4a3e43027 100644 --- a/doc_src/index.hdr.in +++ b/doc_src/index.hdr.in @@ -1025,6 +1025,19 @@ You can change these key bindings using the bindinsert mode is active. @key{Escape} enters command mode. The commands available in command, insert and visual mode are described below. Vi mode shares some bindings with Emacs mode. +It is also possible to add all emacs-mode bindings to vi-mode by using something like + +\fish +function fish_user_key_bindings + # Execute this once per mode that emacs bindings should be used in + fish_default_key_bindings -M insert + # Without an argument, fish_vi_key_bindings will default to + # resetting all bindings. + # The argument specifies the initial mode (insert, "default" or visual). + fish_vi_key_bindings insert +end +\endfish + When in vi-mode, the `fish_mode_prompt` function will display a mode indicator to the left of the prompt. The `fish_vi_cursor` function is available to change the cursor's shape depending on the mode in supported terminals. \subsubsection vi-mode-command Command mode diff --git a/share/functions/fish_default_key_bindings.fish b/share/functions/fish_default_key_bindings.fish index db7bb971e..f26db4146 100644 --- a/share/functions/fish_default_key_bindings.fish +++ b/share/functions/fish_default_key_bindings.fish @@ -1,18 +1,18 @@ function fish_default_key_bindings -d "Default (Emacs-like) key bindings for fish" if not set -q argv[1] + # Clear earlier bindings, if any + bind --erase --all if test "$fish_key_bindings" != "fish_default_key_bindings" # Allow the user to set the variable universally set -q fish_key_bindings; or set -g fish_key_bindings set fish_key_bindings fish_default_key_bindings # This triggers the handler, which calls us again and ensures the user_key_bindings are executed return end - # Clear earlier bindings, if any - bind --erase --all end # These are shell-specific bindings that we share with vi mode. - __fish_shared_key_bindings + __fish_shared_key_bindings $argv # This is the default binding, i.e. the one used if no other binding matches bind $argv "" self-insert diff --git a/share/functions/fish_vi_key_bindings.fish b/share/functions/fish_vi_key_bindings.fish index cc8e675b3..e920c630c 100644 --- a/share/functions/fish_vi_key_bindings.fish +++ b/share/functions/fish_vi_key_bindings.fish @@ -1,6 +1,8 @@ function fish_vi_key_bindings --description 'vi-like key bindings for fish' # Allow any argument to skip setting the variable. if not set -q argv[1] + # Only erase the bindings if called without argument to allow hybrid bindings. + bind --erase --all # Allow just calling this function to correctly set the bindings. # Because it's a rather discoverable name, users will execute it # and without this would then have subtly broken bindings. @@ -22,13 +24,13 @@ function fish_vi_key_bindings --description 'vi-like key bindings for fish' # not end/home, we share those. set -l eol_keys \$ g\$ set -l bol_keys \^ 0 g\^ - if set -q argv[1] + # Ignore any argument that is not a valid mode name. + if set -q argv[1]; and contains -- $argv[1] insert default visual set init_mode $argv[1] end # Inherit shared key bindings. # Do this first so vi-bindings win over default. - bind --erase --all for mode in insert default visual __fish_shared_key_bindings -M $mode end