fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Peter AmmonRemove OS-specific bindings in vi mode308aaa3f0

master
28.3 KiB680 linesraw
1function fish_vi_dec
2    fish_vi_inc_dec dec
3end
4
5function fish_vi_inc
6    fish_vi_inc_dec inc
7end
8
9function fish_vi_arg_digit --description 'Accumulate a digit for the next command'
10    set -g __fish_vi_count "$__fish_vi_count$argv[1]"
11end
12
13function fish_vi_run_count --description 'Run a command $__fish_vi_count times'
14    set -l count (__fish_vi_consume_count __fish_vi_count)
15
16    for i in (seq $count)
17        if functions -q -- $argv[1]
18            $argv
19        else
20            commandline -f $argv
21        end
22    end
23end
24
25function fish_vi_start_operator
26    set -g __fish_vi_operator $argv[1]
27    set -g __fish_vi_start_count (__fish_vi_consume_count __fish_vi_count)
28    set fish_bind_mode operator
29    commandline -f repaint-mode
30end
31
32function fish_vi_operator_cancel
33    set -g __fish_vi_operator
34    set -g __fish_vi_start_count
35    set -g __fish_vi_count
36    set fish_bind_mode default
37    commandline -f repaint-mode
38end
39
40function __fish_vi_consume_count -a varname
41    set -l effective_count $$varname
42    if test -z "$effective_count"
43        set effective_count 1
44    end
45    set -g $varname
46    echo $effective_count
47end
48
49function __fish_vi_delete_char
50    set -l count (__fish_vi_consume_count __fish_vi_count)
51    commandline -f begin-selection
52    if test $count -gt 1
53        for i in (seq 1 (math $count - 1))
54            commandline -f forward-char
55        end
56    end
57    commandline -f kill-selection end-selection
58end
59
60function __fish_vi_backward_delete_char
61    set -l count (__fish_vi_consume_count __fish_vi_count)
62    set -l start_cursor (commandline -C)
63    if test $start_cursor -eq 0
64        return
65    end
66    commandline -f backward-char
67    commandline -f begin-selection
68    if test $count -gt 1
69        for i in (seq 1 (math $count - 1))
70            commandline -f backward-char
71        end
72    end
73    commandline -f kill-selection end-selection
74end
75
76function fish_vi_yank_selection
77    set -g fish_cursor_end_mode exclusive
78    commandline -f kill-selection -f yank
79    set -g fish_cursor_end_mode inclusive
80end
81
82function fish_vi_exec_motion
83    argparse --stop-nonopt linewise -- $argv
84    or return
85
86    set -l motion $argv
87    set -l total (math (__fish_vi_consume_count __fish_vi_start_count) \* (__fish_vi_consume_count __fish_vi_count))
88
89    set fish_bind_mode default
90
91    set -l seq_total (seq $total)
92    if set -ql _flag_linewise
93        switch $__fish_vi_operator
94            case delete
95                for i in $seq_total
96                    commandline -f kill-whole-line
97                end
98            case change
99                for i in $seq_total
100                    commandline -f kill-inner-line
101                end
102                set fish_bind_mode insert
103            case yank
104                for i in $seq_total
105                    commandline -f kill-whole-line yank
106                end
107            case swap-case
108                # Not implemented yet
109                return
110        end
111    else
112        set -l use_selection true
113        set -l swap_case_hack
114        switch $motion[1]
115            case forward-word-vi forward-bigword-vi
116                if test $__fish_vi_operator = swap-case
117                    set swap_case_hack (string replace -r -- '^forward-((?:big)?word)-vi$' '$1' $motion)
118                else
119                    set use_selection false
120                    # kill-word-vi kill-bigword-vi
121                    set motion (string replace -- forward kill $motion)
122                end
123        end
124        set -l motion_cmd
125        switch $motion[1]
126            case commandline
127                set motion_cmd $motion
128            case '*'
129                set motion_cmd commandline -f $motion
130        end
131        switch $motion[1]
132            case forward-char
133                set -e seq_total[1]
134            case backward-char
135                $motion_cmd
136                set -e seq_total[1]
137        end
138        if $use_selection
139            commandline -f begin-selection
140        else
141            commandline -f begin-undo-group
142        end
143        set -l ok true
144        switch $__fish_vi_operator
145            case delete
146                for i in $seq_total
147                    $motion_cmd || { set ok false; break }
148                end
149                if $ok && $use_selection
150                    commandline -f kill-selection
151                end
152            case change
153                switch $motion[1]
154                    case kill-word-vi kill-bigword-vi
155                        set -l end_kill (string replace -- -vi '' $motion[1])
156                        for i in $seq_total
157                            if test $i -eq $total
158                                commandline -f $end_kill
159                            else
160                                $motion_cmd
161                            end
162                        end
163                    case '*'
164                        for i in $seq_total
165                            $motion_cmd || { set ok false; break }
166                        end
167                end
168                if $ok
169                    if $use_selection
170                        commandline -f kill-selection
171                    end
172                    set fish_bind_mode insert
173                end
174            case yank
175                for i in $seq_total
176                    $motion_cmd || { set ok false; break }
177                end
178                if $ok
179                    if $use_selection
180                        fish_vi_yank_selection
181                    else
182                        commandline -f yank
183                    end
184                end
185            case swap-case
186                for i in $seq_total
187                    $motion_cmd || { set ok false; break }
188                end
189                if $ok
190                    if set -q swap_case_hack[1]
191                        set -l word $swap_case_hack
192                        commandline -f \
193                            backward-$word \
194                            forward-$word-end \
195                            togglecase-selection \
196                            backward-$word \
197                            forward-$word-vi
198                    else
199                        commandline -f togglecase-selection
200                    end
201                end
202        end
203        if $use_selection
204            commandline -f end-selection
205        else
206            commandline -f end-undo-group
207        end
208    end
209    commandline -f repaint-mode
210    set -g __fish_vi_operator
211end
212
213# TODO: Currently we do not support hexadecimal and octal values.
214function fish_vi_inc_dec --description 'increment or decrement the number below the cursor'
215    # The cursor is zero based, but all string functions assume 1 to be
216    # the lowest index. Adjust accordingly.
217    set --local cursor (math -- (commandline --cursor) + 1)
218    set --local line (commandline --current-buffer | string collect)
219
220    set --local candidate (string sub --start $cursor -- $line | string collect)
221    if set --local just_found (string match --regex '^-?[0-9]+' -- $candidate)
222        # Search from the current cursor position backwards for as long as we
223        # can identify a valid number.
224        set --function found $just_found
225        set --function found_at $cursor
226        set --local end (math -- $cursor + (string length -- $found) - 1)
227
228        set i (math -- $cursor - 1)
229        while [ $i -ge 1 ]
230            set candidate (string sub --start $i --end $end -- $line)
231            if set just_found (string match --regex '^-?[0-9]+$' -- $candidate)
232                set found $just_found
233                set found_at $i
234                # We found a candidate, but continue to make sure that we captured
235                # the complete number and not just part of it.
236            else
237                # We have already found a number earlier. Work with that.
238                break
239            end
240
241            set i (math -- $i - 1)
242        end
243    else
244        # We didn't find a match below the cursor. Mirror Vim behavior by
245        # checking ahead as well.
246        for i in (seq (math -- $cursor + 1) (math -- (string length -- $line) - 1))
247            set candidate (string sub --start $i -- $line | string collect)
248
249            if set just_found (string match --regex '^-?[0-9]+' -- $candidate)
250                set found $just_found
251                set found_at $i
252                break
253            end
254        end
255
256        if [ -z "$found" ]
257            return
258        end
259    end
260
261    if [ $argv = inc ]
262        set number (math -- $found + 1)
263    else if [ $argv = dec ]
264        set number (math -- $found - 1)
265    end
266
267    set --local number_abs (string trim --left --chars=- -- $number)
268    set --local signed $status
269    set --local found_abs (string trim --left --chars=- -- $found)
270    set number (string pad --char 0 --width (string length -- $found_abs) -- $number_abs)
271    if test $signed -eq 0
272        set number "-$number"
273    end
274
275    # `string sub` may bitch about `--end` being zero if `found_at` is 1.
276    # So ignore errors here...
277    set --local before (string sub --end (math -- $found_at - 1) -- $line 2> /dev/null | string collect)
278    set --local after (string sub --start (math -- $found_at + (string length -- $found)) -- $line | string collect)
279    commandline --replace -- "$before$number$after"
280    # Need to subtract two here because 1) cursor is zero based 2)
281    # `found_at` is the index of the first character of the match, but we
282    # want the one before that.
283    commandline --cursor -- (math -- $found_at + (string length -- $number) - 2)
284    commandline --function -- repaint
285end
286
287function fish_vi_key_bindings --description 'vi-like key bindings for fish'
288    if contains -- -h $argv
289        or contains -- --help $argv
290        echo "Sorry but this function doesn't support -h or --help" >&2
291        return 1
292    end
293
294    # Erase all bindings if not explicitly requested otherwise to
295    # allow for hybrid bindings.
296    # This needs to be checked here because if we are called again
297    # via the variable handler the argument will be gone.
298    set -l rebind true
299    if test "$argv[1]" = --no-erase
300        set rebind false
301        set -e argv[1]
302    else
303        bind --erase --all --preset # clear earlier bindings, if any
304    end
305
306    # Allow just calling this function to correctly set the bindings.
307    # Because it's a rather discoverable name, users will execute it
308    # and without this would then have subtly broken bindings.
309    if test "$fish_key_bindings" != fish_vi_key_bindings
310        and test "$rebind" = true
311        __fish_change_key_bindings fish_vi_key_bindings
312    end
313
314    set -l init_mode insert
315    # These are only the special vi-style keys
316    # not end/home, we share those.
317    set -l eol_keys \$ g,\$
318    set -l bol_keys \^ 0 g\^ _
319
320    if contains -- $argv[1] insert default visual
321        set init_mode $argv[1]
322    else if set -q argv[1]
323        # We should still go on so the bindings still get set.
324        echo "Unknown argument $argv" >&2
325    end
326
327    function __fish_vi_key_bindings_shared
328        eval "$(__fish_shared_key_bindings)"
329    end
330
331    # Inherit shared key bindings.
332    # Do this first so vi-bindings win over default.
333    for mode in insert default visual
334        __fish_vi_key_bindings_shared -M $mode
335    end
336    functions -e __fish_vi_key_bindings_shared
337
338    # Add a way to switch from insert to normal (command) mode.
339    # Note if we are paging, we want to stay in insert mode
340    # See #2871
341    set -l on_escape '
342        if commandline -P
343            commandline -f cancel
344        else
345            set fish_bind_mode default
346            if test (count (commandline --cut-at-cursor | tail -c2)) != 2
347                commandline -f backward-char
348            end
349            commandline -f repaint-mode
350        end
351    '
352    bind --preset -M insert escape $on_escape
353    bind --preset -M insert ctrl-\[ $on_escape
354
355    # Default (command) mode
356    bind --preset :,q exit
357    bind --preset -m insert ctrl-c clear-commandline repaint-mode
358
359    bind --preset -M default escape 'set -g __fish_vi_count'
360    bind --preset -M default ctrl-\[ 'set -g __fish_vi_count'
361
362    for i in (seq 1 9)
363        bind --preset -M default $i "fish_vi_arg_digit $i"
364    end
365    # 0 is special: it is 'beginning-of-line' unless we are already counting (e.g. 10)
366    bind --preset -M default 0 "if test -n \"\$__fish_vi_count\"; fish_vi_arg_digit 0; else; commandline -f beginning-of-line; end"
367
368    # --- Movement with Count Support ---
369    bind --preset -M default h 'fish_vi_run_count backward-char'
370    bind --preset -M default l 'fish_vi_run_count forward-char'
371
372    bind --preset -M default k 'fish_vi_run_count up-or-search'
373    bind --preset -M default j 'fish_vi_run_count down-or-search'
374
375    bind --preset -M default b 'fish_vi_run_count backward-word'
376    bind --preset -M default B 'fish_vi_run_count backward-bigword'
377    bind --preset -M default g,e 'fish_vi_run_count backward-word-end'
378    bind --preset -M default g,E 'fish_vi_run_count backward-bigword-end'
379
380    bind --preset -M default w 'fish_vi_run_count forward-word-vi'
381    bind --preset -M default W 'fish_vi_run_count forward-bigword-vi'
382
383    bind --preset -M default e 'fish_vi_run_count forward-word-end'
384    bind --preset -M default E 'fish_vi_run_count forward-bigword-end'
385
386    bind --preset -M default x __fish_vi_delete_char
387    bind --preset -M default X __fish_vi_backward_delete_char
388
389    bind --preset -m insert enter execute
390    bind --preset -m insert ctrl-j execute
391    bind --preset -m insert ctrl-m execute
392    bind --preset -m insert o 'set fish_cursor_end_mode exclusive' insert-line-under repaint-mode
393    bind --preset -m insert O 'set fish_cursor_end_mode exclusive' insert-line-over repaint-mode
394    bind --preset -m insert i repaint-mode
395    bind --preset -m insert I beginning-of-line repaint-mode
396    bind --preset -m insert a 'set fish_cursor_end_mode exclusive' forward-single-char repaint-mode
397    bind --preset -m insert A 'set fish_cursor_end_mode exclusive' end-of-line repaint-mode
398    bind --preset -m visual v begin-selection repaint-mode
399
400    bind --preset g,g beginning-of-buffer
401    bind --preset G end-of-buffer
402
403    for key in $eol_keys
404        bind --preset $key end-of-line
405    end
406    # Note: 0 is handled in the numeric section above
407    for key in \^ g\^ _
408        bind --preset $key beginning-of-line
409    end
410
411    bind --preset u undo
412    bind --preset ctrl-r redo
413
414    bind --preset [ history-token-search-backward
415    bind --preset ] history-token-search-forward
416    bind --preset -m insert / history-pager repaint-mode
417
418    bind --preset -M insert ctrl-n accept-autosuggestion
419
420    # Vi/Vim doesn't support these keys in insert mode but that seems silly so we do so anyway.
421    bind --preset -M insert home beginning-of-line
422    bind --preset -M default home beginning-of-line
423    bind --preset -M insert end end-of-line
424    bind --preset -M default end end-of-line
425
426    bind --preset -M insert delete delete-char
427    bind --preset -M default delete delete-char
428
429    # Backspace deletes a char in insert mode, but not in normal/default mode.
430    bind --preset -M insert backspace backward-delete-char
431    bind --preset -M insert shift-backspace backward-delete-char
432    bind --preset -M default backspace backward-char
433    bind --preset -M insert ctrl-h backward-delete-char
434    bind --preset -M default ctrl-h backward-char
435
436    # Operators & Operator Mode
437    bind --preset -M default d 'fish_vi_start_operator delete'
438    bind --preset -M default c 'fish_vi_start_operator change'
439    bind --preset -M default y 'fish_vi_start_operator yank'
440    bind --preset -M default g,\~ 'fish_vi_start_operator swap-case'
441
442    bind --preset -M operator escape fish_vi_operator_cancel
443    bind --preset -M operator ctrl-\[ fish_vi_operator_cancel
444
445    for i in (seq 1 9)
446        bind --preset -M operator $i "fish_vi_arg_digit $i"
447    end
448    bind --preset -M operator 0 "if test -n \"\$__fish_vi_count\"; fish_vi_arg_digit 0; else; fish_vi_exec_motion beginning-of-line; end"
449
450    bind --preset -M operator h 'fish_vi_exec_motion backward-char'
451    bind --preset -M operator l 'fish_vi_exec_motion forward-char'
452    bind --preset -M operator k 'fish_vi_exec_motion up-line'
453    bind --preset -M operator j 'fish_vi_exec_motion down-line'
454    bind --preset -M operator b 'fish_vi_exec_motion backward-word'
455    bind --preset -M operator B 'fish_vi_exec_motion backward-bigword'
456    bind --preset -M operator g,e 'fish_vi_exec_motion backward-word-end'
457    bind --preset -M operator g,E 'fish_vi_exec_motion backward-bigword-end'
458    bind --preset -M operator w 'fish_vi_exec_motion forward-word-vi'
459    bind --preset -M operator W 'fish_vi_exec_motion forward-bigword-vi'
460    bind --preset -M operator e 'fish_vi_exec_motion forward-word-end'
461    bind --preset -M operator E 'fish_vi_exec_motion forward-bigword-end'
462
463    bind --preset -M operator 0 'fish_vi_exec_motion beginning-of-line'
464    bind --preset -M operator \^ 'fish_vi_exec_motion beginning-of-line'
465    bind --preset -M operator \$ 'fish_vi_exec_motion end-of-line'
466
467    bind --preset -M operator f --sets-mode f ''
468    bind --preset -M operator F --sets-mode F ''
469    bind --preset -M operator t --sets-mode t ''
470    bind --preset -M operator T --sets-mode T ''
471
472    bind --preset -M f '' get-key 'fish_vi_exec_motion commandline --forward-jump=$fish_key' 'set -eg fish_key'
473    bind --preset -M F '' get-key 'fish_vi_exec_motion commandline --backward-jump=$fish_key' 'set -eg fish_key'
474    bind --preset -M t '' get-key 'fish_vi_exec_motion commandline --forward-jump-till=$fish_key' 'set -eg fish_key'
475    bind --preset -M T '' get-key 'fish_vi_exec_motion commandline --backward-jump-till=$fish_key' 'set -eg fish_key'
476
477    bind --preset -M operator ';' 'fish_vi_exec_motion repeat-jump'
478    bind --preset -M operator , 'fish_vi_exec_motion repeat-jump-reverse'
479
480    bind --preset -M operator d 'fish_vi_exec_motion --linewise'
481    bind --preset -M operator c 'fish_vi_exec_motion --linewise'
482    bind --preset -M operator y 'fish_vi_exec_motion --linewise'
483    bind --preset -M operator \~ 'fish_vi_exec_motion --linewise'
484
485    bind --preset D kill-line
486    bind --preset d,\$ kill-line
487    bind --preset d,\^ backward-kill-line
488    bind --preset d,0 backward-kill-line
489
490    bind --preset d,i,w kill-inner-word
491    bind --preset d,i,W kill-inner-bigword
492    bind --preset d,a,w kill-a-word
493    bind --preset d,a,W kill-a-bigword
494    bind --preset d,i,b jump-till-matching-bracket and jump-till-matching-bracket and begin-selection jump-till-matching-bracket kill-selection end-selection
495    bind --preset d,a,b jump-to-matching-bracket and jump-to-matching-bracket and begin-selection jump-to-matching-bracket kill-selection end-selection
496    bind --preset d,i backward-jump-till and repeat-jump-reverse and begin-selection repeat-jump kill-selection end-selection
497    bind --preset d,a backward-jump and repeat-jump-reverse and begin-selection repeat-jump kill-selection end-selection
498    bind --preset 'd,;' begin-selection repeat-jump kill-selection end-selection
499    bind --preset 'd,comma' begin-selection repeat-jump-reverse kill-selection end-selection
500
501    bind --preset -m insert s delete-char repaint-mode
502    bind --preset -m insert S kill-inner-line repaint-mode
503    bind --preset -m insert C kill-line repaint-mode
504    bind --preset -m insert c,\$ kill-line repaint-mode
505    bind --preset -m insert c,\^ backward-kill-line repaint-mode
506    bind --preset -m insert c,0 backward-kill-line repaint-mode
507
508    bind --preset -m insert c,i,w kill-inner-word repaint-mode
509    bind --preset -m insert c,i,W kill-inner-bigword repaint-mode
510    bind --preset -m insert c,a,w kill-a-word repaint-mode
511    bind --preset -m insert c,a,W kill-a-bigword repaint-mode
512    bind --preset -m insert c,i,b jump-till-matching-bracket and jump-till-matching-bracket and begin-selection jump-till-matching-bracket kill-selection end-selection
513    bind --preset -m insert c,a,b jump-to-matching-bracket and jump-to-matching-bracket and begin-selection jump-to-matching-bracket kill-selection end-selection
514    bind --preset -m insert c,i backward-jump-till and repeat-jump-reverse and begin-selection repeat-jump kill-selection end-selection repaint-mode
515    bind --preset -m insert c,a backward-jump and repeat-jump-reverse and begin-selection repeat-jump kill-selection end-selection repaint-mode
516
517    bind --preset \~ togglecase-char forward-single-char
518    bind --preset g,u downcase-word
519    bind --preset g,U upcase-word
520
521    bind --preset J end-of-line delete-char
522    bind --preset K 'man (commandline -t) 2>/dev/null; or echo -n \a'
523
524    # yy handled by operator mode
525    for seq in '",*,y,y' '",*,Y' '",+,y,y' '",+,Y'
526        bind --preset $seq fish_clipboard_copy
527    end
528    bind --preset Y kill-whole-line yank
529    bind --preset y,\$ kill-line yank
530    bind --preset y,\^ backward-kill-line yank
531    bind --preset y,0 backward-kill-line yank
532    bind --preset y,i,w kill-inner-word yank
533    bind --preset y,i,W kill-inner-bigword yank
534    bind --preset y,a,w kill-a-word yank
535    bind --preset y,a,W kill-a-bigword yank
536    bind --preset y,i,b jump-till-matching-bracket and jump-till-matching-bracket and begin-selection jump-till-matching-bracket fish_vi_yank_selection end-selection
537    bind --preset y,a,b jump-to-matching-bracket and jump-to-matching-bracket and begin-selection jump-to-matching-bracket fish_vi_yank_selection end-selection
538    bind --preset y,i backward-jump-till and repeat-jump-reverse and begin-selection repeat-jump fish_vi_yank_selection end-selection
539    bind --preset y,a backward-jump and repeat-jump-reverse and begin-selection repeat-jump fish_vi_yank_selection end-selection
540
541    bind --preset % jump-to-matching-bracket
542    bind --preset f forward-jump
543    bind --preset F backward-jump
544    bind --preset t forward-jump-till
545    bind --preset T backward-jump-till
546    bind --preset ';' repeat-jump
547    bind --preset , repeat-jump-reverse
548
549    # in emacs yank means paste
550    # in vim p means paste *after* current character, so go forward a char before pasting
551    # also in vim, P means paste *at* current position (like at '|' with cursor = line),
552    # \ so there's no need to go back a char, just paste it without moving
553    bind --preset p 'set -g fish_cursor_end_mode exclusive' forward-char 'set -g fish_cursor_end_mode inclusive' yank
554    bind --preset P yank
555    bind --preset g,p yank-pop
556
557    # same vim 'pasting' note as upper
558    bind --preset '",*,p' 'set -g fish_cursor_end_mode exclusive' forward-char 'set -g fish_cursor_end_mode inclusive' fish_clipboard_paste
559    bind --preset '",*,P' fish_clipboard_paste
560    bind --preset '",+,p' 'set -g fish_cursor_end_mode exclusive' forward-char 'set -g fish_cursor_end_mode inclusive' fish_clipboard_paste
561    bind --preset '",+,P' fish_clipboard_paste
562
563    #
564    # Lowercase r, enters replace_one mode
565    #
566    bind --preset -m replace_one r repaint-mode
567    bind --preset -M replace_one -m default '' 'set -g fish_cursor_end_mode exclusive' delete-char self-insert backward-char repaint-mode 'set -g fish_cursor_end_mode inclusive'
568    bind --preset -M replace_one -m default enter 'set -g fish_cursor_end_mode exclusive' 'commandline -f delete-char; commandline -i \n; commandline -f backward-char' repaint-mode 'set -g fish_cursor_end_mode inclusive'
569    bind --preset -M replace_one -m default ctrl-j 'set -g fish_cursor_end_mode exclusive' 'commandline -f delete-char; commandline -i \n; commandline -f backward-char' repaint-mode 'set -g fish_cursor_end_mode inclusive'
570    bind --preset -M replace_one -m default ctrl-m 'set -g fish_cursor_end_mode exclusive' 'commandline -f delete-char; commandline -i \n; commandline -f backward-char' repaint-mode 'set -g fish_cursor_end_mode inclusive'
571    bind --preset -M replace_one -m default escape cancel repaint-mode
572    bind --preset -M replace_one -m default ctrl-\[ cancel repaint-mode
573
574    #
575    # Uppercase R, enters replace mode
576    #
577    bind --preset -m replace R repaint-mode
578    bind --preset -M replace '' delete-char self-insert
579    bind --preset -M replace -m insert enter execute repaint-mode
580    bind --preset -M replace -m insert ctrl-j execute repaint-mode
581    bind --preset -M replace -m insert ctrl-m execute repaint-mode
582    bind --preset -M replace -m default escape cancel repaint-mode
583    bind --preset -M replace -m default ctrl-\[ cancel repaint-mode
584    # in vim (and maybe in vi), <BS> deletes the changes
585    # but this binding just move cursor backward, not delete the changes
586    bind --preset -M replace backspace backward-char
587    bind --preset -M replace shift-backspace backward-char
588
589    #
590    # Increment or decrement number under the cursor with ctrl+x ctrl+a
591    #
592    bind --preset -M default ctrl-a fish_vi_inc
593    bind --preset -M default ctrl-x fish_vi_dec
594
595    #
596    # visual mode
597    #
598    bind --preset -M visual h backward-char
599    bind --preset -M visual l forward-char
600
601    bind --preset -M visual k up-line
602    bind --preset -M visual j down-line
603
604    bind --preset -M visual b backward-word
605    bind --preset -M visual B backward-bigword
606    bind --preset -M visual g,e backward-word-end
607    bind --preset -M visual g,E backward-bigword-end
608    bind --preset -M visual w forward-word-vi
609    bind --preset -M visual W forward-bigword-vi
610    bind --preset -M visual e forward-word-end
611    bind --preset -M visual E forward-bigword-end
612    bind --preset -M visual o swap-selection-start-stop repaint-mode
613
614    bind --preset -M visual % jump-to-matching-bracket
615    bind --preset -M visual f forward-jump
616    bind --preset -M visual t forward-jump-till
617    bind --preset -M visual F backward-jump
618    bind --preset -M visual T backward-jump-till
619    bind --preset -M visual ';' repeat-jump
620    bind --preset -M visual , repeat-jump-reverse
621
622    for key in $eol_keys
623        bind --preset -M visual $key end-of-line
624    end
625    for key in $bol_keys
626        bind --preset -M visual $key beginning-of-line
627    end
628
629    bind --preset -M visual -m default v end-selection repaint-mode
630    bind --preset -M visual -m insert i end-selection repaint-mode
631    bind --preset -M visual -m insert I end-selection beginning-of-line repaint-mode
632    bind --preset -M visual -m insert c kill-selection end-selection repaint-mode
633    bind --preset -M visual -m insert s kill-selection end-selection repaint-mode
634    bind --preset -M visual -m default d kill-selection end-selection backward-char repaint-mode
635    bind --preset -M visual -m default x kill-selection end-selection repaint-mode
636    bind --preset -M visual -m default X kill-whole-line end-selection repaint-mode
637    bind --preset -M visual -m default y fish_vi_yank_selection end-selection repaint-mode
638    bind --preset -M visual -m default '",*,y' "fish_clipboard_copy; commandline -f end-selection repaint-mode"
639    bind --preset -M visual -m default '",+,y' "fish_clipboard_copy; commandline -f end-selection repaint-mode"
640    bind --preset -M visual -m default \~ togglecase-selection end-selection repaint-mode
641    bind --preset -M visual -m default g,u downcase-selection end-selection repaint-mode
642    bind --preset -M visual -m default g,U upcase-selection end-selection repaint-mode
643
644    bind --preset -M visual -m default ctrl-c end-selection repaint-mode
645    bind --preset -M visual -m default escape end-selection repaint-mode
646    bind --preset -M visual -m default ctrl-\[ end-selection repaint-mode
647
648    # Make it easy to turn an unexecuted command into a comment in the shell history. Also, remove
649    # the commenting chars so the command can be further edited then executed.
650    bind --preset -M default \# __fish_toggle_comment_commandline
651    bind --preset -M visual \# __fish_toggle_comment_commandline
652    bind --preset -M replace \# __fish_toggle_comment_commandline
653
654    # Set the cursor shape
655    # After executing once, this will have defined functions listening for the variable.
656    # Therefore it needs to be before setting fish_bind_mode.
657    fish_vi_cursor
658    set -g fish_cursor_selection_mode inclusive
659    function __fish_vi_key_bindings_on_mode_change --on-variable fish_bind_mode
660        switch $fish_bind_mode
661            case insert replace
662                set -g fish_cursor_end_mode exclusive
663            case '*'
664                set -g fish_cursor_end_mode inclusive
665        end
666    end
667    function __fish_vi_key_bindings_remove_handlers --on-variable __fish_active_key_bindings
668        functions --erase __fish_vi_key_bindings_remove_handlers
669        functions --erase __fish_vi_key_bindings_on_mode_change
670        if type -q __fish_vi_cursor
671            __fish_vi_cursor fish_cursor_default
672        end
673        functions --erase fish_vi_cursor_handle
674        functions --erase fish_vi_cursor_handle_preexec
675        set -e -g fish_cursor_end_mode
676        set -e -g fish_cursor_selection_mode
677    end
678
679    set fish_bind_mode $init_mode
680end