2014-04-09 16:59:58 +00:00
|
|
|
function fish_vi_key_bindings --description 'vi-like key bindings for fish'
|
2016-11-13 22:07:03 +00: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-04 22:47:37 +00: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-15 23:47:19 +00:00
|
|
|
set -l rebind true
|
|
|
|
if test "$argv[1]" = "--no-erase"
|
|
|
|
set rebind false
|
2016-09-04 22:47:37 +00:00
|
|
|
set -e argv[1]
|
2016-11-15 23:47:19 +00:00
|
|
|
else
|
2016-11-28 05:27:22 +00:00
|
|
|
bind --erase --all # clear earlier bindings, if any
|
2016-05-25 11:09:33 +00:00
|
|
|
end
|
2016-09-04 22:47:37 +00: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-15 23:47:19 +00:00
|
|
|
and test "$rebind" = "true"
|
|
|
|
# Allow the user to set the variable universally.
|
2016-09-04 22:47:37 +00:00
|
|
|
set -q fish_key_bindings
|
|
|
|
or set -g fish_key_bindings
|
2016-11-13 22:07:03 +00: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-04 22:47:37 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-04-29 03:53:09 +00: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-15 23:47:19 +00:00
|
|
|
set -q fish_escape_delay_ms
|
|
|
|
or set -g fish_escape_delay_ms 100
|
2016-03-29 22:33:37 +00:00
|
|
|
|
|
|
|
set -l init_mode insert
|
2016-09-03 17:27:42 +00: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-04 22:47:37 +00:00
|
|
|
|
|
|
|
if contains -- $argv[1] insert default visual
|
2016-03-29 23:42:58 +00:00
|
|
|
set init_mode $argv[1]
|
2016-09-04 22:47:37 +00: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-29 22:33:37 +00:00
|
|
|
end
|
|
|
|
|
2016-05-25 11:09:33 +00:00
|
|
|
# Inherit shared key bindings.
|
2016-03-29 22:33:37 +00:00
|
|
|
# Do this first so vi-bindings win over default.
|
2016-05-25 11:09:33 +00:00
|
|
|
for mode in insert default visual
|
|
|
|
__fish_shared_key_bindings -M $mode
|
|
|
|
end
|
|
|
|
|
|
|
|
bind -M insert \r execute
|
|
|
|
bind -M insert \n execute
|
2016-11-28 05:27:22 +00:00
|
|
|
|
2016-05-25 11:09:33 +00:00
|
|
|
bind -M insert "" self-insert
|
2016-03-29 22:33:37 +00:00
|
|
|
|
|
|
|
# Add way to kill current command line while in insert mode.
|
2016-04-14 02:46:18 +00:00
|
|
|
bind -M insert \cc __fish_cancel_commandline
|
2016-03-29 22:33:37 +00:00
|
|
|
# Add a way to switch from insert to normal (command) mode.
|
2017-01-31 18:58:06 +00:00
|
|
|
# Note if we are paging, we want to stay in insert mode
|
|
|
|
# See #2871
|
|
|
|
bind -M insert \e "if commandline -P; commandline -f cancel; else; set fish_bind_mode default; commandline -f backward-char force-repaint; end"
|
2016-03-29 22:33:37 +00:00
|
|
|
|
2016-04-14 02:46:18 +00:00
|
|
|
# Default (command) mode
|
2016-03-29 22:33:37 +00:00
|
|
|
bind :q exit
|
2016-04-14 02:46:18 +00:00
|
|
|
bind -m insert \cc __fish_cancel_commandline
|
2016-05-25 11:09:33 +00:00
|
|
|
bind -M default h backward-char
|
|
|
|
bind -M default l forward-char
|
2016-03-29 22:33:37 +00:00
|
|
|
bind -m insert \n execute
|
|
|
|
bind -m insert \r execute
|
|
|
|
bind -m insert i force-repaint
|
|
|
|
bind -m insert I beginning-of-line force-repaint
|
|
|
|
bind -m insert a forward-char force-repaint
|
|
|
|
bind -m insert A end-of-line force-repaint
|
|
|
|
bind -m visual v begin-selection force-repaint
|
|
|
|
|
|
|
|
#bind -m insert o "commandline -a \n" down-line force-repaint
|
|
|
|
#bind -m insert O beginning-of-line "commandline -i \n" up-line force-repaint # doesn't work
|
|
|
|
|
|
|
|
bind gg beginning-of-buffer
|
|
|
|
bind G end-of-buffer
|
|
|
|
|
|
|
|
for key in $eol_keys
|
2016-03-29 23:42:58 +00:00
|
|
|
bind $key end-of-line
|
2016-03-29 22:33:37 +00:00
|
|
|
end
|
|
|
|
for key in $bol_keys
|
2016-03-29 23:42:58 +00:00
|
|
|
bind $key beginning-of-line
|
2016-03-29 22:33:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
bind u history-search-backward
|
|
|
|
bind \cr history-search-forward
|
|
|
|
|
|
|
|
bind [ history-token-search-backward
|
|
|
|
bind ] history-token-search-forward
|
|
|
|
|
|
|
|
bind k up-or-search
|
|
|
|
bind j down-or-search
|
|
|
|
bind b backward-word
|
|
|
|
bind B backward-bigword
|
|
|
|
bind ge backward-word
|
|
|
|
bind gE backward-bigword
|
|
|
|
bind w forward-word forward-char
|
|
|
|
bind W forward-bigword forward-char
|
|
|
|
bind e forward-char forward-word backward-char
|
|
|
|
bind E forward-bigword backward-char
|
|
|
|
|
2017-01-15 19:11:38 +00: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.
|
|
|
|
bind -M insert -k home beginning-of-line 2>/dev/null
|
|
|
|
bind -M default -k home beginning-of-line 2>/dev/null
|
|
|
|
bind -M insert -k end end-of-line 2>/dev/null
|
|
|
|
bind -M default -k end end-of-line 2>/dev/null
|
|
|
|
|
|
|
|
bind -M default x delete-char
|
|
|
|
bind -M default X backward-delete-char
|
|
|
|
bind -M insert -k dc delete-char
|
|
|
|
bind -M default -k dc delete-char
|
2016-03-29 22:33:37 +00:00
|
|
|
|
2016-08-04 18:40:21 +00:00
|
|
|
# Backspace deletes a char in insert mode, but not in normal/default mode.
|
|
|
|
bind -M insert -k backspace backward-delete-char
|
|
|
|
bind -M default -k backspace backward-char
|
2016-12-17 05:52:58 +00:00
|
|
|
bind -M insert \ch backward-delete-char
|
|
|
|
bind -M default \ch backward-char
|
2016-12-18 20:25:56 +00:00
|
|
|
bind -M insert \x7f backward-delete-char
|
|
|
|
bind -M default \x7f backward-char
|
2017-01-15 19:11:38 +00:00
|
|
|
bind -M insert \e\[3\;2~ backward-delete-char # Mavericks Terminal.app shift-ctrl-delete
|
|
|
|
bind -M default \e\[3\;2~ backward-delete-char # Mavericks Terminal.app shift-ctrl-delete
|
2016-03-29 22:33:37 +00:00
|
|
|
|
|
|
|
bind dd kill-whole-line
|
|
|
|
bind D kill-line
|
|
|
|
bind d\$ kill-line
|
|
|
|
bind d\^ backward-kill-line
|
|
|
|
bind dw kill-word
|
|
|
|
bind dW kill-bigword
|
|
|
|
bind diw forward-char forward-char backward-word kill-word
|
|
|
|
bind diW forward-char forward-char backward-bigword kill-bigword
|
|
|
|
bind daw forward-char forward-char backward-word kill-word
|
|
|
|
bind daW forward-char forward-char backward-bigword kill-bigword
|
|
|
|
bind de kill-word
|
|
|
|
bind dE kill-bigword
|
|
|
|
bind db backward-kill-word
|
|
|
|
bind dB backward-kill-bigword
|
|
|
|
bind dge backward-kill-word
|
|
|
|
bind dgE backward-kill-bigword
|
|
|
|
|
|
|
|
bind -m insert s delete-char force-repaint
|
|
|
|
bind -m insert S kill-whole-line force-repaint
|
|
|
|
bind -m insert cc kill-whole-line force-repaint
|
|
|
|
bind -m insert C kill-line force-repaint
|
|
|
|
bind -m insert c\$ kill-line force-repaint
|
|
|
|
bind -m insert c\^ backward-kill-line force-repaint
|
|
|
|
bind -m insert cw kill-word force-repaint
|
|
|
|
bind -m insert cW kill-bigword force-repaint
|
|
|
|
bind -m insert ciw forward-char forward-char backward-word kill-word force-repaint
|
|
|
|
bind -m insert ciW forward-char forward-char backward-bigword kill-bigword force-repaint
|
|
|
|
bind -m insert caw forward-char forward-char backward-word kill-word force-repaint
|
|
|
|
bind -m insert caW forward-char forward-char backward-bigword kill-bigword force-repaint
|
|
|
|
bind -m insert ce kill-word force-repaint
|
|
|
|
bind -m insert cE kill-bigword force-repaint
|
|
|
|
bind -m insert cb backward-kill-word force-repaint
|
|
|
|
bind -m insert cB backward-kill-bigword force-repaint
|
|
|
|
bind -m insert cge backward-kill-word force-repaint
|
|
|
|
bind -m insert cgE backward-kill-bigword force-repaint
|
|
|
|
|
|
|
|
bind '~' capitalize-word
|
|
|
|
bind gu downcase-word
|
|
|
|
bind gU upcase-word
|
|
|
|
|
|
|
|
bind J end-of-line delete-char
|
|
|
|
bind K 'man (commandline -t) ^/dev/null; or echo -n \a'
|
|
|
|
|
|
|
|
bind yy kill-whole-line yank
|
2016-03-29 23:42:58 +00:00
|
|
|
bind Y kill-whole-line yank
|
2016-03-29 22:33:37 +00:00
|
|
|
bind y\$ kill-line yank
|
|
|
|
bind y\^ backward-kill-line yank
|
|
|
|
bind yw kill-word yank
|
|
|
|
bind yW kill-bigword yank
|
|
|
|
bind yiw forward-char forward-char backward-word kill-word yank
|
|
|
|
bind yiW forward-char forward-char backward-bigword kill-bigword yank
|
|
|
|
bind yaw forward-char forward-char backward-word kill-word yank
|
|
|
|
bind yaW forward-char forward-char backward-bigword kill-bigword yank
|
|
|
|
bind ye kill-word yank
|
|
|
|
bind yE kill-bigword yank
|
|
|
|
bind yb backward-kill-word yank
|
|
|
|
bind yB backward-kill-bigword yank
|
|
|
|
bind yge backward-kill-word yank
|
|
|
|
bind ygE backward-kill-bigword yank
|
|
|
|
|
|
|
|
bind f forward-jump
|
|
|
|
bind F backward-jump
|
|
|
|
bind t forward-jump and backward-char
|
|
|
|
bind T backward-jump and forward-char
|
|
|
|
|
|
|
|
# in emacs yank means paste
|
|
|
|
bind p yank
|
|
|
|
bind P backward-char yank
|
|
|
|
bind gp yank-pop
|
|
|
|
|
|
|
|
bind '"*p' "commandline -i ( xsel -p; echo )[1]"
|
|
|
|
bind '"*P' backward-char "commandline -i ( xsel -p; echo )[1]"
|
|
|
|
|
|
|
|
#
|
|
|
|
# Lowercase r, enters replace-one mode
|
|
|
|
#
|
|
|
|
bind -m replace-one r force-repaint
|
|
|
|
bind -M replace-one -m default '' delete-char self-insert backward-char force-repaint
|
|
|
|
bind -M replace-one -m default \e cancel force-repaint
|
|
|
|
|
|
|
|
#
|
|
|
|
# visual mode
|
|
|
|
#
|
|
|
|
bind -M visual h backward-char
|
|
|
|
bind -M visual l forward-char
|
|
|
|
|
|
|
|
bind -M visual k up-line
|
|
|
|
bind -M visual j down-line
|
|
|
|
|
|
|
|
bind -M visual b backward-word
|
|
|
|
bind -M visual B backward-bigword
|
|
|
|
bind -M visual ge backward-word
|
|
|
|
bind -M visual gE backward-bigword
|
|
|
|
bind -M visual w forward-word
|
|
|
|
bind -M visual W forward-bigword
|
|
|
|
bind -M visual e forward-word
|
|
|
|
bind -M visual E forward-bigword
|
|
|
|
bind -M visual o swap-selection-start-stop force-repaint
|
|
|
|
|
|
|
|
for key in $eol_keys
|
2016-03-29 23:42:58 +00:00
|
|
|
bind -M visual $key end-of-line
|
2016-03-29 22:33:37 +00:00
|
|
|
end
|
|
|
|
for key in $bol_keys
|
2016-03-29 23:42:58 +00:00
|
|
|
bind -M visual $key beginning-of-line
|
2016-03-29 22:33:37 +00:00
|
|
|
end
|
|
|
|
|
2016-03-29 23:42:58 +00:00
|
|
|
bind -M visual -m insert c kill-selection end-selection force-repaint
|
2016-03-29 22:33:37 +00:00
|
|
|
bind -M visual -m default d kill-selection end-selection force-repaint
|
|
|
|
bind -M visual -m default x kill-selection end-selection force-repaint
|
|
|
|
bind -M visual -m default X kill-whole-line end-selection force-repaint
|
|
|
|
bind -M visual -m default y kill-selection yank end-selection force-repaint
|
|
|
|
bind -M visual -m default '"*y' "commandline -s | xsel -p" end-selection force-repaint
|
|
|
|
|
|
|
|
bind -M visual -m default \cc end-selection force-repaint
|
2016-03-29 23:42:58 +00:00
|
|
|
bind -M visual -m default \e end-selection force-repaint
|
2016-03-29 22:33:37 +00:00
|
|
|
|
2016-04-17 03:03:14 +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.
|
|
|
|
bind -M default \# __fish_toggle_comment_commandline
|
|
|
|
bind -M visual \# __fish_toggle_comment_commandline
|
2016-09-04 23:22:07 +00: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 00:52:41 +00:00
|
|
|
end
|