2014-04-10 00:59:58 +08:00
|
|
|
function fish_vi_key_bindings --description 'vi-like key bindings for fish'
|
2016-11-14 06:07:03 +08:00
|
|
|
if contains -- -h $argv
|
|
|
|
or contains -- --help $argv
|
|
|
|
echo "Sorry but this function doesn't support -h or --help"
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2016-09-05 06:47:37 +08:00
|
|
|
# Erase all bindings if not explicitly requested otherwise to
|
|
|
|
# allow for hybrid bindings.
|
|
|
|
# This needs to be checked here because if we are called again
|
|
|
|
# via the variable handler the argument will be gone.
|
2016-11-16 07:47:19 +08:00
|
|
|
set -l rebind true
|
|
|
|
if test "$argv[1]" = "--no-erase"
|
|
|
|
set rebind false
|
2016-09-05 06:47:37 +08:00
|
|
|
set -e argv[1]
|
2016-11-16 07:47:19 +08:00
|
|
|
else
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --erase --all --preset # clear earlier bindings, if any
|
2016-05-25 19:09:33 +08:00
|
|
|
end
|
2016-09-05 06:47:37 +08:00
|
|
|
|
2017-12-22 07:44:18 +08:00
|
|
|
# Silence warnings about unavailable keys. See #4431, 4188
|
|
|
|
if not contains -- -s $argv
|
2018-03-09 23:29:25 +08:00
|
|
|
set argv "-s" "-M" $argv
|
2017-12-22 07:44:18 +08:00
|
|
|
end
|
|
|
|
|
2016-09-05 06:47:37 +08:00
|
|
|
# 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.
|
|
|
|
if test "$fish_key_bindings" != "fish_vi_key_bindings"
|
2016-11-16 07:47:19 +08:00
|
|
|
and test "$rebind" = "true"
|
|
|
|
# Allow the user to set the variable universally.
|
2016-09-05 06:47:37 +08:00
|
|
|
set -q fish_key_bindings
|
|
|
|
or set -g fish_key_bindings
|
2016-11-14 06:07:03 +08:00
|
|
|
# This triggers the handler, which calls us again and ensures the user_key_bindings
|
|
|
|
# are executed.
|
|
|
|
set fish_key_bindings fish_vi_key_bindings
|
2016-09-05 06:47:37 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-04-29 11:53:09 +08:00
|
|
|
# The default escape timeout is 300ms. But for users of Vi bindings that can be slightly
|
|
|
|
# annoying when trying to switch to Vi "normal" mode. So set a shorter timeout in this case
|
|
|
|
# unless the user has explicitly set the delay.
|
2016-11-16 07:47:19 +08:00
|
|
|
set -q fish_escape_delay_ms
|
|
|
|
or set -g fish_escape_delay_ms 100
|
2016-03-30 06:33:37 +08:00
|
|
|
|
|
|
|
set -l init_mode insert
|
2016-09-04 01:27:42 +08:00
|
|
|
# These are only the special vi-style keys
|
|
|
|
# not end/home, we share those.
|
|
|
|
set -l eol_keys \$ g\$
|
|
|
|
set -l bol_keys \^ 0 g\^
|
2016-09-05 06:47:37 +08:00
|
|
|
|
|
|
|
if contains -- $argv[1] insert default visual
|
2016-03-30 07:42:58 +08:00
|
|
|
set init_mode $argv[1]
|
2016-09-05 06:47:37 +08:00
|
|
|
else if set -q argv[1]
|
|
|
|
# We should still go on so the bindings still get set.
|
|
|
|
echo "Unknown argument $argv" >&2
|
2016-03-30 06:33:37 +08:00
|
|
|
end
|
|
|
|
|
2016-05-25 19:09:33 +08:00
|
|
|
# Inherit shared key bindings.
|
2016-03-30 06:33:37 +08:00
|
|
|
# Do this first so vi-bindings win over default.
|
2016-05-25 19:09:33 +08:00
|
|
|
for mode in insert default visual
|
|
|
|
__fish_shared_key_bindings -M $mode
|
|
|
|
end
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv insert \r execute
|
|
|
|
bind --preset $argv insert \n execute
|
2016-11-28 13:27:22 +08:00
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv insert "" self-insert
|
2016-03-30 06:33:37 +08:00
|
|
|
|
|
|
|
# Add way to kill current command line while in insert mode.
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv insert \cc __fish_cancel_commandline
|
2016-03-30 06:33:37 +08:00
|
|
|
# Add a way to switch from insert to normal (command) mode.
|
2017-02-01 02:58:06 +08:00
|
|
|
# Note if we are paging, we want to stay in insert mode
|
|
|
|
# See #2871
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv insert \e "if commandline -P; commandline -f cancel; else; set fish_bind_mode default; commandline -f backward-char force-repaint; end"
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2016-04-14 10:46:18 +08:00
|
|
|
# Default (command) mode
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset :q exit
|
|
|
|
bind --preset -m insert \cc __fish_cancel_commandline
|
|
|
|
bind --preset $argv default h backward-char
|
|
|
|
bind --preset $argv default l forward-char
|
|
|
|
bind --preset -m insert \n execute
|
|
|
|
bind --preset -m insert \r execute
|
|
|
|
bind --preset -m insert i force-repaint
|
|
|
|
bind --preset -m insert I beginning-of-line force-repaint
|
|
|
|
bind --preset -m insert a forward-char force-repaint
|
|
|
|
bind --preset -m insert A end-of-line force-repaint
|
|
|
|
bind --preset -m visual v begin-selection force-repaint
|
|
|
|
|
|
|
|
#bind --preset -m insert o "commandline -a \n" down-line force-repaint
|
|
|
|
#bind --preset -m insert O beginning-of-line "commandline -i \n" up-line force-repaint # doesn't work
|
|
|
|
|
|
|
|
bind --preset gg beginning-of-buffer
|
|
|
|
bind --preset G end-of-buffer
|
2016-03-30 06:33:37 +08:00
|
|
|
|
|
|
|
for key in $eol_keys
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $key end-of-line
|
2016-03-30 06:33:37 +08:00
|
|
|
end
|
|
|
|
for key in $bol_keys
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $key beginning-of-line
|
2016-03-30 06:33:37 +08:00
|
|
|
end
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset u history-search-backward
|
|
|
|
bind --preset \cr history-search-forward
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset [ history-token-search-backward
|
|
|
|
bind --preset ] history-token-search-forward
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset k up-or-search
|
|
|
|
bind --preset j down-or-search
|
|
|
|
bind --preset b backward-word
|
|
|
|
bind --preset B backward-bigword
|
|
|
|
bind --preset ge backward-word
|
|
|
|
bind --preset gE backward-bigword
|
|
|
|
bind --preset w forward-word forward-char
|
|
|
|
bind --preset W forward-bigword forward-char
|
|
|
|
bind --preset e forward-char forward-word backward-char
|
|
|
|
bind --preset E forward-bigword backward-char
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2017-01-16 03:11:38 +08:00
|
|
|
# OS X SnowLeopard doesn't have these keys. Don't show an annoying error message.
|
|
|
|
# Vi/Vim doesn't support these keys in insert mode but that seems silly so we do so anyway.
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv insert -k home beginning-of-line 2>/dev/null
|
|
|
|
bind --preset $argv default -k home beginning-of-line 2>/dev/null
|
|
|
|
bind --preset $argv insert -k end end-of-line 2>/dev/null
|
|
|
|
bind --preset $argv default -k end end-of-line 2>/dev/null
|
2017-01-16 03:11:38 +08:00
|
|
|
|
2017-04-20 04:59:53 +08:00
|
|
|
# Vi moves the cursor back if, after deleting, it is at EOL.
|
|
|
|
# To emulate that, move forward, then backward, which will be a NOP
|
|
|
|
# if there is something to move forward to.
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv default x delete-char forward-char backward-char
|
|
|
|
bind --preset $argv default X backward-delete-char
|
|
|
|
bind --preset $argv insert -k dc delete-char forward-char backward-char
|
|
|
|
bind --preset $argv default -k dc delete-char forward-char backward-char
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2016-08-05 02:40:21 +08:00
|
|
|
# Backspace deletes a char in insert mode, but not in normal/default mode.
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv insert -k backspace backward-delete-char
|
|
|
|
bind --preset $argv default -k backspace backward-char
|
|
|
|
bind --preset $argv insert \ch backward-delete-char
|
|
|
|
bind --preset $argv default \ch backward-char
|
|
|
|
bind --preset $argv insert \x7f backward-delete-char
|
|
|
|
bind --preset $argv default \x7f backward-char
|
|
|
|
bind --preset $argv insert \e\[3\;2~ backward-delete-char # Mavericks Terminal.app shift-ctrl-delete
|
|
|
|
bind --preset $argv default \e\[3\;2~ backward-delete-char # Mavericks Terminal.app shift-ctrl-delete
|
|
|
|
|
|
|
|
bind --preset dd kill-whole-line
|
|
|
|
bind --preset D kill-line
|
|
|
|
bind --preset d\$ kill-line
|
|
|
|
bind --preset d\^ backward-kill-line
|
|
|
|
bind --preset dw kill-word
|
|
|
|
bind --preset dW kill-bigword
|
|
|
|
bind --preset diw forward-char forward-char backward-word kill-word
|
|
|
|
bind --preset diW forward-char forward-char backward-bigword kill-bigword
|
|
|
|
bind --preset daw forward-char forward-char backward-word kill-word
|
|
|
|
bind --preset daW forward-char forward-char backward-bigword kill-bigword
|
|
|
|
bind --preset de kill-word
|
|
|
|
bind --preset dE kill-bigword
|
|
|
|
bind --preset db backward-kill-word
|
|
|
|
bind --preset dB backward-kill-bigword
|
|
|
|
bind --preset dge backward-kill-word
|
|
|
|
bind --preset dgE backward-kill-bigword
|
|
|
|
bind --preset df begin-selection forward-jump kill-selection end-selection
|
|
|
|
bind --preset dt begin-selection forward-jump backward-char kill-selection end-selection
|
|
|
|
bind --preset dF begin-selection backward-jump kill-selection end-selection
|
|
|
|
bind --preset dT begin-selection backward-jump forward-char kill-selection end-selection
|
|
|
|
|
|
|
|
bind --preset -m insert s delete-char force-repaint
|
|
|
|
bind --preset -m insert S kill-whole-line force-repaint
|
|
|
|
bind --preset -m insert cc kill-whole-line force-repaint
|
|
|
|
bind --preset -m insert C kill-line force-repaint
|
|
|
|
bind --preset -m insert c\$ kill-line force-repaint
|
|
|
|
bind --preset -m insert c\^ backward-kill-line force-repaint
|
|
|
|
bind --preset -m insert cw kill-word force-repaint
|
|
|
|
bind --preset -m insert cW kill-bigword force-repaint
|
|
|
|
bind --preset -m insert ciw forward-char forward-char backward-word kill-word force-repaint
|
|
|
|
bind --preset -m insert ciW forward-char forward-char backward-bigword kill-bigword force-repaint
|
|
|
|
bind --preset -m insert caw forward-char forward-char backward-word kill-word force-repaint
|
|
|
|
bind --preset -m insert caW forward-char forward-char backward-bigword kill-bigword force-repaint
|
|
|
|
bind --preset -m insert ce kill-word force-repaint
|
|
|
|
bind --preset -m insert cE kill-bigword force-repaint
|
|
|
|
bind --preset -m insert cb backward-kill-word force-repaint
|
|
|
|
bind --preset -m insert cB backward-kill-bigword force-repaint
|
|
|
|
bind --preset -m insert cge backward-kill-word force-repaint
|
|
|
|
bind --preset -m insert cgE backward-kill-bigword force-repaint
|
|
|
|
|
|
|
|
bind --preset '~' capitalize-word
|
|
|
|
bind --preset gu downcase-word
|
|
|
|
bind --preset gU upcase-word
|
|
|
|
|
|
|
|
bind --preset J end-of-line delete-char
|
|
|
|
bind --preset K 'man (commandline -t) 2>/dev/null; or echo -n \a'
|
|
|
|
|
|
|
|
bind --preset yy kill-whole-line yank
|
|
|
|
bind --preset Y kill-whole-line yank
|
|
|
|
bind --preset y\$ kill-line yank
|
|
|
|
bind --preset y\^ backward-kill-line yank
|
|
|
|
bind --preset yw kill-word yank
|
|
|
|
bind --preset yW kill-bigword yank
|
|
|
|
bind --preset yiw forward-char forward-char backward-word kill-word yank
|
|
|
|
bind --preset yiW forward-char forward-char backward-bigword kill-bigword yank
|
|
|
|
bind --preset yaw forward-char forward-char backward-word kill-word yank
|
|
|
|
bind --preset yaW forward-char forward-char backward-bigword kill-bigword yank
|
|
|
|
bind --preset ye kill-word yank
|
|
|
|
bind --preset yE kill-bigword yank
|
|
|
|
bind --preset yb backward-kill-word yank
|
|
|
|
bind --preset yB backward-kill-bigword yank
|
|
|
|
bind --preset yge backward-kill-word yank
|
|
|
|
bind --preset ygE backward-kill-bigword yank
|
|
|
|
|
|
|
|
bind --preset f forward-jump
|
|
|
|
bind --preset F backward-jump
|
|
|
|
bind --preset t forward-jump-till
|
|
|
|
bind --preset T backward-jump-till
|
|
|
|
bind --preset ';' repeat-jump
|
|
|
|
bind --preset , repeat-jump-reverse
|
2016-03-30 06:33:37 +08:00
|
|
|
|
|
|
|
# in emacs yank means paste
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset p yank
|
|
|
|
bind --preset P backward-char yank
|
|
|
|
bind --preset gp yank-pop
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset '"*p' "commandline -i ( xsel -p; echo )[1]"
|
|
|
|
bind --preset '"*P' backward-char "commandline -i ( xsel -p; echo )[1]"
|
2016-03-30 06:33:37 +08:00
|
|
|
|
|
|
|
#
|
2017-04-17 19:47:40 +08:00
|
|
|
# Lowercase r, enters replace_one mode
|
2016-03-30 06:33:37 +08:00
|
|
|
#
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset -m replace_one r force-repaint
|
|
|
|
bind --preset $argv replace_one -m default '' delete-char self-insert backward-char force-repaint
|
|
|
|
bind --preset $argv replace_one -m default \e cancel force-repaint
|
2016-03-30 06:33:37 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# visual mode
|
|
|
|
#
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv visual h backward-char
|
|
|
|
bind --preset $argv visual l forward-char
|
|
|
|
|
|
|
|
bind --preset $argv visual k up-line
|
|
|
|
bind --preset $argv visual j down-line
|
|
|
|
|
|
|
|
bind --preset $argv visual b backward-word
|
|
|
|
bind --preset $argv visual B backward-bigword
|
|
|
|
bind --preset $argv visual ge backward-word
|
|
|
|
bind --preset $argv visual gE backward-bigword
|
|
|
|
bind --preset $argv visual w forward-word
|
|
|
|
bind --preset $argv visual W forward-bigword
|
|
|
|
bind --preset $argv visual e forward-word
|
|
|
|
bind --preset $argv visual E forward-bigword
|
|
|
|
bind --preset $argv visual o swap-selection-start-stop force-repaint
|
|
|
|
|
|
|
|
bind --preset $argv visual f forward-jump
|
|
|
|
bind --preset $argv visual t forward-jump-till
|
|
|
|
bind --preset $argv visual F backward-jump
|
|
|
|
bind --preset $argv visual T backward-jump-till
|
2018-01-27 00:36:18 +08:00
|
|
|
|
2016-03-30 06:33:37 +08:00
|
|
|
for key in $eol_keys
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv visual $key end-of-line
|
2016-03-30 06:33:37 +08:00
|
|
|
end
|
|
|
|
for key in $bol_keys
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv visual $key beginning-of-line
|
2016-03-30 06:33:37 +08:00
|
|
|
end
|
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv visual -m insert c kill-selection end-selection force-repaint
|
|
|
|
bind --preset $argv visual -m default d kill-selection end-selection force-repaint
|
|
|
|
bind --preset $argv visual -m default x kill-selection end-selection force-repaint
|
|
|
|
bind --preset $argv visual -m default X kill-whole-line end-selection force-repaint
|
|
|
|
bind --preset $argv visual -m default y kill-selection yank end-selection force-repaint
|
|
|
|
bind --preset $argv visual -m default '"*y' "commandline -s | xsel -p; commandline -f end-selection force-repaint"
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2018-09-18 17:52:25 +08:00
|
|
|
bind --preset $argv visual -m default \cc end-selection force-repaint
|
|
|
|
bind --preset $argv visual -m default \e end-selection force-repaint
|
2016-03-30 06:33:37 +08:00
|
|
|
|
2016-04-17 11:03:14 +08: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 17:52:25 +08:00
|
|
|
bind --preset $argv default \# __fish_toggle_comment_commandline
|
|
|
|
bind --preset $argv visual \# __fish_toggle_comment_commandline
|
2016-09-05 07:22:07 +08:00
|
|
|
|
|
|
|
# Set the cursor shape
|
|
|
|
# After executing once, this will have defined functions listening for the variable.
|
|
|
|
# Therefore it needs to be before setting fish_bind_mode.
|
|
|
|
fish_vi_cursor
|
|
|
|
|
|
|
|
set fish_bind_mode $init_mode
|
|
|
|
|
2013-12-31 08:52:41 +08:00
|
|
|
end
|