2016-05-25 11:09:33 +00:00
|
|
|
function __fish_shared_key_bindings -d "Bindings shared between emacs and vi mode"
|
|
|
|
# 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.
|
|
|
|
|
2016-11-13 22:07:03 +00:00
|
|
|
if contains -- -h $argv
|
|
|
|
or contains -- --help $argv
|
2022-04-04 03:57:55 +00:00
|
|
|
echo "Sorry but this function doesn't support -h or --help" >&2
|
2016-11-13 22:07:03 +00:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \cy yank
|
2016-11-28 05:27:22 +00:00
|
|
|
or return # protect against invalid $argv
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \ey yank-pop
|
2016-05-25 11:09:33 +00:00
|
|
|
|
|
|
|
# Left/Right arrow
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv -k right forward-char
|
|
|
|
bind --preset $argv -k left backward-char
|
|
|
|
bind --preset $argv \e\[C forward-char
|
|
|
|
bind --preset $argv \e\[D backward-char
|
2016-05-25 11:09:33 +00:00
|
|
|
# Some terminals output these when they're in in keypad mode.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \eOC forward-char
|
|
|
|
bind --preset $argv \eOD backward-char
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2020-03-15 11:58:51 +00:00
|
|
|
# Ctrl-left/right - these also work in vim.
|
|
|
|
bind --preset $argv \e\[1\;5C forward-word
|
|
|
|
bind --preset $argv \e\[1\;5D backward-word
|
|
|
|
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv -k ppage beginning-of-history
|
|
|
|
bind --preset $argv -k npage end-of-history
|
2017-01-15 19:11:38 +00:00
|
|
|
|
2016-05-25 11:09:33 +00:00
|
|
|
# Interaction with the system clipboard.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \cx fish_clipboard_copy
|
|
|
|
bind --preset $argv \cv fish_clipboard_paste
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \e cancel
|
|
|
|
bind --preset $argv \t complete
|
|
|
|
bind --preset $argv \cs pager-toggle-search
|
2016-05-25 11:09:33 +00:00
|
|
|
# shift-tab does a tab complete followed by a search.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv --key btab complete-and-search
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2020-03-19 17:18:11 +00:00
|
|
|
bind --preset $argv \e\n "commandline -f expand-abbr; commandline -i \n"
|
|
|
|
bind --preset $argv \e\r "commandline -f expand-abbr; commandline -i \n"
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv -k down down-or-search
|
|
|
|
bind --preset $argv -k up up-or-search
|
|
|
|
bind --preset $argv \e\[A up-or-search
|
|
|
|
bind --preset $argv \e\[B down-or-search
|
|
|
|
bind --preset $argv \eOA up-or-search
|
|
|
|
bind --preset $argv \eOB down-or-search
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2019-02-07 20:37:05 +00:00
|
|
|
bind --preset $argv -k sright forward-bigword
|
|
|
|
bind --preset $argv -k sleft backward-bigword
|
|
|
|
|
2016-05-25 11:09:33 +00:00
|
|
|
# Alt-left/Alt-right
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \e\eOC nextd-or-forward-word
|
|
|
|
bind --preset $argv \e\eOD prevd-or-backward-word
|
|
|
|
bind --preset $argv \e\e\[C nextd-or-forward-word
|
|
|
|
bind --preset $argv \e\e\[D prevd-or-backward-word
|
|
|
|
bind --preset $argv \eO3C nextd-or-forward-word
|
|
|
|
bind --preset $argv \eO3D prevd-or-backward-word
|
|
|
|
bind --preset $argv \e\[3C nextd-or-forward-word
|
|
|
|
bind --preset $argv \e\[3D prevd-or-backward-word
|
|
|
|
bind --preset $argv \e\[1\;3C nextd-or-forward-word
|
|
|
|
bind --preset $argv \e\[1\;3D prevd-or-backward-word
|
|
|
|
bind --preset $argv \e\[1\;9C nextd-or-forward-word #iTerm2
|
|
|
|
bind --preset $argv \e\[1\;9D prevd-or-backward-word #iTerm2
|
2016-05-25 11:09:33 +00:00
|
|
|
|
|
|
|
# Alt-up/Alt-down
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \e\eOA history-token-search-backward
|
|
|
|
bind --preset $argv \e\eOB history-token-search-forward
|
|
|
|
bind --preset $argv \e\e\[A history-token-search-backward
|
|
|
|
bind --preset $argv \e\e\[B history-token-search-forward
|
|
|
|
bind --preset $argv \eO3A history-token-search-backward
|
|
|
|
bind --preset $argv \eO3B history-token-search-forward
|
|
|
|
bind --preset $argv \e\[3A history-token-search-backward
|
|
|
|
bind --preset $argv \e\[3B history-token-search-forward
|
|
|
|
bind --preset $argv \e\[1\;3A history-token-search-backward
|
|
|
|
bind --preset $argv \e\[1\;3B history-token-search-forward
|
|
|
|
bind --preset $argv \e\[1\;9A history-token-search-backward # iTerm2
|
|
|
|
bind --preset $argv \e\[1\;9B history-token-search-forward # iTerm2
|
2016-05-25 11:09:33 +00:00
|
|
|
# Bash compatibility
|
|
|
|
# https://github.com/fish-shell/fish-shell/issues/89
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \e. history-token-search-backward
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \el __fish_list_current_token
|
2020-04-07 15:42:49 +00:00
|
|
|
bind --preset $argv \eo __fish_preview_current_file
|
2019-10-06 13:14:10 +00:00
|
|
|
bind --preset $argv \ew __fish_whatis_current_token
|
2017-02-12 10:58:06 +00:00
|
|
|
# ncurses > 6.0 sends a "delete scrollback" sequence along with clear.
|
|
|
|
# This string replace removes it.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \cl 'echo -n (clear | string replace \e\[3J ""); commandline -f repaint'
|
2020-07-01 18:54:13 +00:00
|
|
|
bind --preset $argv \cc cancel-commandline
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \cu backward-kill-line
|
|
|
|
bind --preset $argv \cw backward-kill-path-component
|
|
|
|
bind --preset $argv \e\[F end-of-line
|
|
|
|
bind --preset $argv \e\[H beginning-of-line
|
2016-09-03 17:27:42 +00:00
|
|
|
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \ed 'set -l cmd (commandline); if test -z "$cmd"; echo; dirh; commandline -f repaint; else; commandline -f kill-word; end'
|
|
|
|
bind --preset $argv \cd delete-or-exit
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2022-05-08 00:10:29 +00:00
|
|
|
bind --preset $argv \es "if command -q sudo; fish_commandline_prepend sudo; else if command -q doas; fish_commandline_prepend doas; end"
|
2019-09-25 16:34:54 +00:00
|
|
|
|
2016-05-25 11:09:33 +00:00
|
|
|
# Allow reading manpages by pressing F1 (many GUI applications) or Alt+h (like in zsh).
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv -k f1 __fish_man_page
|
|
|
|
bind --preset $argv \eh __fish_man_page
|
2016-05-25 11:09:33 +00:00
|
|
|
|
2016-11-13 22:07:03 +00:00
|
|
|
# This will make sure the output of the current command is paged using the default pager when
|
|
|
|
# you press Meta-p.
|
2016-05-25 11:09:33 +00:00
|
|
|
# If none is set, less will be used.
|
2020-03-09 18:36:12 +00:00
|
|
|
bind --preset $argv \ep __fish_paginate
|
2016-11-13 22:07:03 +00:00
|
|
|
|
2016-05-25 11:09:33 +00:00
|
|
|
# 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.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \e\# __fish_toggle_comment_commandline
|
2016-12-07 03:24:08 +00:00
|
|
|
|
|
|
|
# The [meta-e] and [meta-v] keystrokes invoke an external editor on the command buffer.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset $argv \ee edit_command_buffer
|
|
|
|
bind --preset $argv \ev edit_command_buffer
|
2017-02-25 13:42:52 +00:00
|
|
|
|
2018-03-07 10:06:23 +00:00
|
|
|
# Tmux' focus events.
|
|
|
|
# Exclude paste mode because that should get _everything_ literally.
|
|
|
|
for mode in (bind --list-modes | string match -v paste)
|
|
|
|
# We only need the in-focus event currently (to redraw the vi-cursor).
|
2019-04-21 17:48:46 +00:00
|
|
|
bind --preset -M $mode \e\[I 'emit fish_focus_in'
|
|
|
|
bind --preset -M $mode \e\[O false
|
|
|
|
bind --preset -M $mode \e\[\?1004h false
|
2018-03-07 10:06:23 +00:00
|
|
|
end
|
|
|
|
|
2017-02-25 13:42:52 +00:00
|
|
|
# Support for "bracketed paste"
|
|
|
|
# The way it works is that we acknowledge our support by printing
|
|
|
|
# \e\[?2004h
|
|
|
|
# then the terminal will "bracket" every paste in
|
|
|
|
# \e\[200~ and \e\[201~
|
|
|
|
# Every character in between those two will be part of the paste and should not cause a binding to execute (like \n executing commands).
|
|
|
|
#
|
|
|
|
# We enable it after every command and disable it before (in __fish_config_interactive.fish)
|
|
|
|
#
|
2019-03-13 11:27:33 +00:00
|
|
|
# Support for this seems to be ubiquitous - emacs enables it unconditionally (!) since 25.1
|
|
|
|
# (though it only supports it since then, it seems to be the last term to gain support).
|
2017-02-25 13:42:52 +00:00
|
|
|
#
|
|
|
|
# NOTE: This is more of a "security" measure than a proper feature.
|
|
|
|
# The better way to paste remains the `fish_clipboard_paste` function (bound to \cv by default).
|
|
|
|
# We don't disable highlighting here, so it will be redone after every character (which can be slow),
|
2019-03-13 11:27:33 +00:00
|
|
|
# and it doesn't handle "paste-stop" sequences in the paste (which the terminal needs to strip).
|
2017-02-25 13:42:52 +00:00
|
|
|
#
|
2019-03-13 11:27:33 +00:00
|
|
|
# See http://thejh.net/misc/website-terminal-copy-paste.
|
2017-03-16 17:35:44 +00:00
|
|
|
|
2019-03-13 11:27:33 +00:00
|
|
|
# Bind the starting sequence in every bind mode, even user-defined ones.
|
2017-03-16 17:35:44 +00:00
|
|
|
# Exclude paste mode or there'll be an additional binding after switching between emacs and vi
|
|
|
|
for mode in (bind --list-modes | string match -v paste)
|
2020-03-09 18:36:12 +00:00
|
|
|
bind --preset -M $mode -m paste \e\[200~ __fish_start_bracketed_paste
|
2017-02-25 13:42:52 +00:00
|
|
|
end
|
|
|
|
# This sequence ends paste-mode and returns to the previous mode we have saved before.
|
2020-03-09 18:36:12 +00:00
|
|
|
bind --preset -M paste \e\[201~ __fish_stop_bracketed_paste
|
2017-02-25 13:42:52 +00:00
|
|
|
# In paste-mode, everything self-inserts except for the sequence to get out of it
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset -M paste "" self-insert
|
2017-02-25 13:42:52 +00:00
|
|
|
# Without this, a \r will overwrite the other text, rendering it invisible - which makes the exercise kinda pointless.
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset -M paste \r "commandline -i \n"
|
2019-03-13 11:27:33 +00:00
|
|
|
|
|
|
|
# We usually just pass the text through as-is to facilitate pasting code,
|
|
|
|
# but when the current token contains an unbalanced single-quote (`'`),
|
|
|
|
# we escape all single-quotes and backslashes, effectively turning the paste
|
|
|
|
# into one literal token, to facilitate pasting non-code (e.g. markdown or git commitishes)
|
2018-09-18 09:52:25 +00:00
|
|
|
bind --preset -M paste "'" "__fish_commandline_insert_escaped \' \$__fish_paste_quoted"
|
|
|
|
bind --preset -M paste \\ "__fish_commandline_insert_escaped \\\ \$__fish_paste_quoted"
|
2019-03-13 11:27:33 +00:00
|
|
|
# Only insert spaces if we're either quoted or not at the beginning of the commandline
|
|
|
|
# - this strips leading spaces if they would trigger histignore.
|
2020-03-04 22:28:22 +00:00
|
|
|
bind --preset -M paste " " self-insert-notfirst
|
2022-04-24 19:15:36 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
# Closing a command substitution expands abbreviations
|
|
|
|
bind --preset $argv ")" self-insert expand-abbr
|
|
|
|
# Ctrl-space inserts space without expanding abbrs
|
2022-05-11 19:57:37 +00:00
|
|
|
bind --preset $argv -k nul 'test -n "$(commandline)" && commandline -i " "'
|
2022-04-24 19:23:18 +00:00
|
|
|
# Shift-space (CSI u escape sequence) behaves like space because it's easy to mistype.
|
2022-07-04 11:11:39 +00:00
|
|
|
bind --preset $argv \e\[32\;2u 'commandline -i " "; commandline -f expand-abbr'
|
2022-04-24 19:15:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
bind --preset $argv \n execute
|
|
|
|
bind --preset $argv \r execute
|
2022-04-24 19:23:18 +00:00
|
|
|
# Control+Return behave like Return because it's easy to mistype after accepting an autosuggestion.
|
|
|
|
bind --preset $argv \e\[27\;5\;13~ execute # Sent with XTerm.vt100.formatOtherKeys: 0
|
|
|
|
bind --preset $argv \e\[13\;5u execute # CSI u sequence, sent with XTerm.vt100.formatOtherKeys: 1
|
2022-04-24 19:15:36 +00:00
|
|
|
end
|
2017-03-09 15:52:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function __fish_commandline_insert_escaped --description 'Insert the first arg escaped if a second arg is given'
|
|
|
|
if set -q argv[2]
|
|
|
|
commandline -i \\$argv[1]
|
|
|
|
else
|
|
|
|
commandline -i $argv[1]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fish_start_bracketed_paste
|
|
|
|
# Save the last bind mode so we can restore it.
|
|
|
|
set -g __fish_last_bind_mode $fish_bind_mode
|
|
|
|
# If the token is currently single-quoted,
|
|
|
|
# we escape single-quotes (and backslashes).
|
2021-04-20 13:29:28 +00:00
|
|
|
string match -q 'single*' (__fish_tokenizer_state -- (commandline -ct | string collect))
|
2017-03-09 15:52:38 +00:00
|
|
|
and set -g __fish_paste_quoted 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function __fish_stop_bracketed_paste
|
|
|
|
# Restore the last bind mode.
|
|
|
|
set fish_bind_mode $__fish_last_bind_mode
|
|
|
|
set -e __fish_paste_quoted
|
2016-05-25 11:09:33 +00:00
|
|
|
end
|