Files
fish-shell/share/functions/fish_default_key_bindings.fish
Johannes Altmanninger 50a6e486a5 Allow explicit shift modifier for non-ASCII letters, fix capslock behavior
We canonicalize "ctrl-shift-i" to "ctrl-I".
Both when deciphering this notation (as given to builtin bind),
and when receiving it as a key event ("\e[105;73;6u")

This has problems:

A. Our bind notation canonicalization only works for 26 English letters.
   For example, "ctrl-shift-ä" is not supported -- only "ctrl-Ä" is.
   We could try to fix that but this depends on the keyboard layout.
   For example "bind alt-shift-=" and "bind alt-+" are equivalent on a "us"
   layout but not on a "de" layout.
B. While capslock is on, the key event won't include a shifted key ("73" here).
   This is due a quirk in the kitty keyboard protocol[^1].  This means that
   fish_key_reader's canonicalization doesn't work (unless we call toupper()
   ourselves).

I think we want to support both notations.

It's recommended to match all of these (in this order) when pressing
"ctrl-shift-i".

	1. bind ctrl-shift-i do-something
	2. bind ctrl-shift-I do-something
	3. bind ctrl-I do-something
	4. bind ctrl-i do-something

Support 1 and 3 for now, allowing both bindings to coexist. No priorities
for now. This solves problem A, and -- if we take care to use the explicit
shift notation -- problem B.

For keys that are not affected by capslock, problem B does not apply.  In this
case, recommend the shifted notation ("alt-+" instead of "alt-shift-=")
since that seems more intuitive.
Though if we prioritized "alt-shift-=" over "alt-+" as per the recommendation,
that's an argument against the shifted key.

Example output for some key events:

	$ fish_key_reader -cV
	# decoded from: \e\[61:43\;4u
	bind alt-+ 'do something' # recommended notation
	bind alt-shift-= 'do something'
	# decoded from: \e\[61:43\;68u
	bind alt-+ 'do something' # recommended notation
	bind alt-shift-= 'do something'

	# decoded from: \e\[105:73\;6u
	bind ctrl-I 'do something'
	bind ctrl-shift-i 'do something' # recommended notation
	# decoded from: \e\[105\;70u
	bind ctrl-shift-i 'do something'

Due to the capslock quirk, the last one has only one matching representation
since there is no shifted key.  We could decide to match ctrl-shift-i events
(that don't have a shifted key) to ctrl-I bindings (for ASCII letters), as
before this patch. But that case is very rare, it should only happen when
capslock is on, so it's probably not even a breaking change.

The other way round is supported -- we do match ctrl-I events (typically
with shifted key) to ctrl-shift-i bindings (but only for ASCII letters).
This is mainly for backwards compatibility.

Also note that, bindings without other modifiers currently need to use the
shifted key (like "Ä", not "shift-ä"), since we still get a legacy encoding,
until we request "Report all keys as escape codes".

[^1]: <https://github.com/kovidgoyal/kitty/issues/8493>
2025-04-03 00:51:35 +02:00

80 lines
2.9 KiB
Fish

function fish_default_key_bindings -d "emacs-like key binds"
if contains -- -h $argv
or contains -- --help $argv
echo "Sorry but this function doesn't support -h or --help"
return 1
end
if not set -q argv[1]
bind --erase --all --preset # clear earlier bindings, if any
if test "$fish_key_bindings" != fish_default_key_bindings
__fish_change_key_bindings fish_default_key_bindings || return
set fish_bind_mode default
end
end
# Silence warnings about unavailable keys. See #4431, 4188
if not contains -- -s $argv
set argv -s $argv
end
# These are shell-specific bindings that we share with vi mode.
__fish_shared_key_bindings $argv
or return # protect against invalid $argv
bind --preset $argv right forward-char
bind --preset $argv left backward-char
bind --preset $argv delete delete-char
bind --preset $argv backspace backward-delete-char
bind --preset $argv shift-backspace backward-delete-char
bind --preset $argv home beginning-of-line
bind --preset $argv end end-of-line
bind --preset $argv ctrl-a beginning-of-line
bind --preset $argv ctrl-e end-of-line
bind --preset $argv ctrl-h backward-delete-char
bind --preset $argv ctrl-p up-or-search
bind --preset $argv ctrl-n down-or-search
bind --preset $argv ctrl-f forward-char
bind --preset $argv ctrl-b backward-char
bind --preset $argv ctrl-t transpose-chars
bind --preset $argv ctrl-g cancel
bind --preset $argv ctrl-/ undo
bind --preset $argv ctrl-_ undo # XTerm idiosyncracy, can get rid of this once we go full CSI u
bind --preset $argv ctrl-z undo
bind --preset $argv ctrl-shift-z redo
bind --preset $argv alt-/ redo
bind --preset $argv alt-t transpose-words
bind --preset $argv alt-u upcase-word
bind --preset $argv alt-c capitalize-word
if test (__fish_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
bind --preset $argv alt-\< beginning-of-buffer
bind --preset $argv alt-\> end-of-buffer
bind --preset $argv ctrl-r history-pager
# term-specific special bindings
switch "$TERM"
case xterm-256color
# Microsoft's conemu uses xterm-256color plus
# the following to tell a console to paste:
bind --preset $argv \e\x20ep fish_clipboard_paste
end
end