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.
This commit is contained in:
Fabian Homborg 2016-09-03 23:00:41 +02:00
parent 1bc887cd9f
commit e89057b70c
3 changed files with 20 additions and 5 deletions

View file

@ -1025,6 +1025,19 @@ You can change these key bindings using the <a href="commands.html#bind">bind</a
Vi mode allows for the use of Vi-like commands at the prompt. Initially, <a href="#vi-mode-insert">insert mode</a> is active. @key{Escape} enters <a href="#vi-mode-command">command mode</a>. The commands available in command, insert and visual mode are described below. Vi mode shares <a href="#shared-binds">some bindings</a> with <a href="#emacs-mode">Emacs mode</a>.
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 <a href="fish_mode_prompt.html">`fish_mode_prompt`</a> 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

View file

@ -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

View file

@ -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