fish-shell/share/functions/__fish_shared_key_bindings.fish
Johannes Altmanninger 8bf8b10f68 Extended & human-friendly keys
See the changelog additions for user-visible changes.

Since we enable/disable terminal protocols whenever we pass terminal ownership,
tests can no longer run in parallel on the same terminal.

For the same reason, readline shortcuts in the gdb REPL will not work anymore.
As a remedy, use gdbserver, or lobby for CSI u support in libreadline.

Add sleep to some tests, otherwise they fall (both in CI and locally).

There are two weird failures on FreeBSD remaining, disable them for now
https://github.com/fish-shell/fish-shell/pull/10359/checks?check_run_id=23330096362

Design and implementation borrows heavily from Kakoune.

In future, we should try to implement more of the kitty progressive
enhancements.

Closes #10359
2024-04-02 14:35:16 +02:00

175 lines
8.8 KiB
Fish

function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mode"
set -l legacy_bind bind
# These are some bindings that are supposed to be shared between vi mode and default mode.
# They are supposed to be unrelated to text-editing (or movement).
# This takes $argv so the vi-bindings can pass the mode they are valid in.
if contains -- -h $argv
or contains -- --help $argv
echo "Sorry but this function doesn't support -h or --help" >&2
return 1
end
bind --preset $argv ctrl-y yank
or return # protect against invalid $argv
bind --preset $argv alt-y yank-pop
# Left/Right arrow
bind --preset $argv right forward-char
bind --preset $argv left backward-char
$legacy_bind --preset $argv -k right forward-char
$legacy_bind --preset $argv -k left backward-char
$legacy_bind --preset $argv \e\[C forward-char
$legacy_bind --preset $argv \e\[D backward-char
# Some terminals output these when they're in in keypad mode.
$legacy_bind --preset $argv \eOC forward-char
$legacy_bind --preset $argv \eOD backward-char
# Ctrl-left/right - these also work in vim.
bind --preset $argv ctrl-right forward-word
bind --preset $argv ctrl-left backward-word
$legacy_bind --preset $argv \e\[1\;5C forward-word
$legacy_bind --preset $argv \e\[1\;5D backward-word
bind --preset $argv pageup beginning-of-history
bind --preset $argv pagedown end-of-history
$legacy_bind --preset $argv -k ppage beginning-of-history
$legacy_bind --preset $argv -k npage end-of-history
# Interaction with the system clipboard.
bind --preset $argv ctrl-x fish_clipboard_copy
bind --preset $argv ctrl-v fish_clipboard_paste
bind --preset $argv escape cancel
bind --preset $argv ctrl-\[ cancel
bind --preset $argv tab complete
bind --preset $argv ctrl-i complete
bind --preset $argv ctrl-s pager-toggle-search
# shift-tab does a tab complete followed by a search.
bind --preset $argv shift-tab complete-and-search
$legacy_bind --preset $argv -k btab complete-and-search
bind --preset $argv shift-delete history-pager-delete or backward-delete-char
$legacy_bind --preset $argv -k sdc history-pager-delete or backward-delete-char
bind --preset $argv down down-or-search
$legacy_bind --preset $argv -k down down-or-search
bind --preset $argv up up-or-search
$legacy_bind --preset $argv -k up up-or-search
$legacy_bind --preset $argv \e\[A up-or-search
$legacy_bind --preset $argv \e\[B down-or-search
$legacy_bind --preset $argv \eOA up-or-search
$legacy_bind --preset $argv \eOB down-or-search
bind --preset $argv shift-right forward-bigword
bind --preset $argv shift-left backward-bigword
$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\eOC nextd-or-forward-word
$legacy_bind --preset $argv \e\eOD prevd-or-backward-word
$legacy_bind --preset $argv \e\e\[C nextd-or-forward-word
$legacy_bind --preset $argv \e\e\[D prevd-or-backward-word
$legacy_bind --preset $argv \eO3C nextd-or-forward-word
$legacy_bind --preset $argv \eO3D prevd-or-backward-word
$legacy_bind --preset $argv \e\[3C nextd-or-forward-word
$legacy_bind --preset $argv \e\[3D prevd-or-backward-word
$legacy_bind --preset $argv \e\[1\;3C nextd-or-forward-word
$legacy_bind --preset $argv \e\[1\;3D prevd-or-backward-word
$legacy_bind --preset $argv \e\[1\;9C nextd-or-forward-word #iTerm2
$legacy_bind --preset $argv \e\[1\;9D prevd-or-backward-word #iTerm2
bind --preset $argv alt-up history-token-search-backward
bind --preset $argv alt-down history-token-search-forward
$legacy_bind --preset $argv \e\eOA history-token-search-backward
$legacy_bind --preset $argv \e\eOB history-token-search-forward
$legacy_bind --preset $argv \e\e\[A history-token-search-backward
$legacy_bind --preset $argv \e\e\[B history-token-search-forward
$legacy_bind --preset $argv \eO3A history-token-search-backward
$legacy_bind --preset $argv \eO3B history-token-search-forward
$legacy_bind --preset $argv \e\[3A history-token-search-backward
$legacy_bind --preset $argv \e\[3B history-token-search-forward
$legacy_bind --preset $argv \e\[1\;3A history-token-search-backward
$legacy_bind --preset $argv \e\[1\;3B history-token-search-forward
$legacy_bind --preset $argv \e\[1\;9A history-token-search-backward # iTerm2
$legacy_bind --preset $argv \e\[1\;9B history-token-search-forward # iTerm2
# Bash compatibility
# https://github.com/fish-shell/fish-shell/issues/89
bind --preset $argv alt-. history-token-search-backward
bind --preset $argv alt-l __fish_list_current_token
bind --preset $argv alt-o __fish_preview_current_file
bind --preset $argv alt-w __fish_whatis_current_token
bind --preset $argv ctrl-l clear-screen
bind --preset $argv ctrl-c cancel-commandline
bind --preset $argv ctrl-u backward-kill-line
bind --preset $argv ctrl-w backward-kill-path-component
bind --preset $argv end end-of-line
$legacy_bind --preset $argv \e\[F end-of-line
bind --preset $argv home beginning-of-line
$legacy_bind --preset $argv \e\[H beginning-of-line
bind --preset $argv alt-d 'set -l cmd (commandline); if test -z "$cmd"; echo; dirh; commandline -f repaint; else; commandline -f kill-word; end'
bind --preset $argv ctrl-d delete-or-exit
bind --preset $argv alt-s 'for cmd in sudo doas please; if command -q $cmd; fish_commandline_prepend $cmd; break; end; end'
# Allow reading manpages by pressing F1 (many GUI applications) or Alt+h (like in zsh).
bind --preset $argv F1 __fish_man_page
$legacy_bind --preset $argv -k f1 __fish_man_page
bind --preset $argv alt-h __fish_man_page
# This will make sure the output of the current command is paged using the default pager when
# you press Meta-p.
# If none is set, less will be used.
bind --preset $argv alt-p __fish_paginate
# Make it easy to turn an unexecuted command into a comment in the shell history. Also,
# remove the commenting chars so the command can be further edited then executed.
bind --preset $argv alt-# __fish_toggle_comment_commandline
# These keystrokes invoke an external editor on the command buffer.
bind --preset $argv alt-e edit_command_buffer
bind --preset $argv alt-v edit_command_buffer
# Bindings that are shared in text-insertion modes.
if not set -l index (contains --index -- -M $argv)
or test $argv[(math $index + 1)] = insert
# This is the default binding, i.e. the one used if no other binding matches
bind --preset $argv "" self-insert
or exit # protect against invalid $argv
# Space and other command terminators expands abbrs _and_ inserts itself.
bind --preset $argv " " self-insert expand-abbr
bind --preset $argv ";" self-insert expand-abbr
bind --preset $argv "|" self-insert expand-abbr
bind --preset $argv "&" self-insert expand-abbr
bind --preset $argv ">" self-insert expand-abbr
bind --preset $argv "<" self-insert expand-abbr
bind --preset $argv shift-enter expand-abbr "commandline -i \n"
# Shift+Return as sent with XTerm.vt100.formatOtherKeys: 0
$legacy_bind --preset $argv \e\[27\;2\;13~ expand-abbr "commandline -i \n"
# Shift+Return CSI u sequence, sent with XTerm.vt100.formatOtherKeys: 1
$legacy_bind --preset $argv \e\[13\;2u expand-abbr "commandline -i \n"
bind --preset $argv alt-enter expand-abbr "commandline -i \n"
# Closing a command substitution expands abbreviations
bind --preset $argv ")" self-insert expand-abbr
# Ctrl-space inserts space without expanding abbrs
bind --preset $argv ctrl-space 'test -n "$(commandline)" && commandline -i " "'
bind --preset $argv -k nul 'test -n "$(commandline)" && commandline -i " "'
# Shift-space behaves like space because it's easy to mistype.
bind --preset $argv shift-space 'commandline -i " "; commandline -f expand-abbr'
$legacy_bind --preset $argv \e\[32\;2u 'commandline -i " "; commandline -f expand-abbr' # CSI u escape sequence
bind --preset $argv enter execute
bind --preset $argv ctrl-j execute
bind --preset $argv ctrl-m execute
# Make Control+Return behave like Return because it's easy to mistype after accepting an autosuggestion.
bind --preset $argv ctrl-enter execute
$legacy_bind --preset $argv \e\[27\;5\;13~ execute # Sent with XTerm.vt100.formatOtherKeys: 0
$legacy_bind --preset $argv \e\[13\;5u execute # CSI u sequence, sent with XTerm.vt100.formatOtherKeys: 1
end
end