grace/dots

dotfiles/nix configs

git clone https://git.gae.moe/grace/dots

Grace Yoderi have got to be better about these things9c3d491

main
12.3 KiB295 linesraw
1# IMPORTANT!!!
2#
3# When defining your own bindings using fish_helix_command, be aware that it can break
4# stuff sometimes.
5#
6# It is safe to define a binding consisting of a lone call to fish_helix_command.
7# Calls to other functions and executables are allowed along with it, granted they don't mess
8# with fish's commandline buffer.
9#
10# Mixing multiple fish_helix_commandline and commandline calls in one binding MAY trigger issues.
11# Nothing serious, but don't be surprised. Just test it.
12
13function fish_helix_key_bindings --description 'helix-like key bindings for fish'
14    if contains -- -h $argv
15        or contains -- --help $argv
16        echo "Sorry but this function doesn't support -h or --help"
17        return 1
18    end
19
20    # Erase all bindings if not explicitly requested otherwise to
21    # allow for hybrid bindings.
22    # This needs to be checked here because if we are called again
23    # via the variable handler the argument will be gone.
24    set -l rebind true
25    if test "$argv[1]" = --no-erase
26        set rebind false
27        set -e argv[1]
28    else
29        bind --erase --all --preset # clear earlier bindings, if any
30    end
31
32    # Allow just calling this function to correctly set the bindings.
33    # Because it's a rather discoverable name, users will execute it
34    # and without this would then have subtly broken bindings.
35    if test "$fish_key_bindings" != fish_helix_key_bindings
36        and test "$rebind" = true
37        # Allow the user to set the variable universally.
38        set -q fish_key_bindings
39        or set -g fish_key_bindings
40        # This triggers the handler, which calls us again and ensures the user_key_bindings
41        # are executed.
42        set fish_key_bindings fish_helix_key_bindings
43        return
44    end
45
46    set -l init_mode insert
47
48    if contains -- $argv[1] insert default visual
49        set init_mode $argv[1]
50    else if set -q argv[1]
51        # We should still go on so the bindings still get set.
52        echo "Unknown argument $argv" >&2
53    end
54
55    function __fish_helix_key_bindings_shared
56        eval "$(__fish_shared_key_bindings)"
57    end
58
59    # Inherit shared key bindings.
60    # Do this first so helix-bindings win over default.
61    for mode in insert default visual
62        __fish_helix_key_bindings_shared -M $mode
63    end
64    functions -e __fish_helix_key_bindings_shared
65
66    bind -s --preset -M insert \r execute
67    bind -s --preset -M insert \n execute
68
69    bind -s --preset -M insert "" self-insert
70
71    # Space and other command terminators expand abbrs _and_ inserts itself.
72    bind -s --preset -M insert " " self-insert expand-abbr
73    bind -s --preset -M insert ";" self-insert expand-abbr
74    bind -s --preset -M insert "|" self-insert expand-abbr
75    bind -s --preset -M insert "&" self-insert expand-abbr
76    bind -s --preset -M insert "^" self-insert expand-abbr
77    bind -s --preset -M insert ">" self-insert expand-abbr
78    bind -s --preset -M insert "<" self-insert expand-abbr
79    # Closing a command substitution expands abbreviations
80    bind -s --preset -M insert ")" self-insert expand-abbr
81    # Ctrl-space inserts space without expanding abbrs
82    bind -s --preset -M insert ctrl-space 'commandline -i " "'
83
84    # Switching to insert mode
85    for mode in default visual
86        bind -s --preset -M $mode -m insert \cc end-selection cancel-commandline repaint-mode
87        bind -s --preset -M $mode -m insert \n end-selection execute
88        bind -s --preset -M $mode -m insert \r end-selection execute
89        bind -s --preset -M $mode -m insert o end-selection insert-line-under repaint-mode
90        bind -s --preset -M $mode -m insert O end-selection insert-line-over repaint-mode
91        # FIXME i/a should keep selection, maybe
92        bind -s --preset -M $mode i "fish_helix_command insert_mode"
93        bind -s --preset -M $mode I "fish_helix_command prepend_to_line"
94        bind -s --preset -M $mode a "fish_helix_command append_mode"
95        bind -s --preset -M $mode A "fish_helix_command append_to_line"
96    end
97
98    # Switching from insert mode
99    # Note if we are paging, we want to stay in insert mode
100    # See #2871
101    bind -s --preset -M insert \e "if commandline -P; commandline -f cancel; else; set fish_bind_mode default; commandline -f begin-selection repaint-mode; end"
102
103    # Switching between normal and visual mode
104    bind -s --preset -M default -m visual v repaint-mode
105    for key in v \e
106        bind -s --preset -M visual -m default $key repaint-mode
107    end
108
109    # Motion and actions in normal/select mode
110    for mode in default visual
111        if test $mode = default
112            set -f n_begin_selection begin-selection # only begin-selection if current mode is Normal
113            set -f ns_move_extend move
114            set -f commandline_v_repaint ""
115        else
116            set -f n_begin_selection
117            set -f ns_move_extend extend
118            set -f commandline_v_repaint "commandline -f repaint-mode"
119        end
120
121        for key in (seq 0 9)
122            bind -s --preset -M $mode $key "fish_bind_count $key"
123            # FIXME example to bind 0
124            # FIXME backspace to edit count
125        end
126        for key in h \e\[D \eOD left
127            bind -s --preset -M $mode $key "fish_helix_command "$ns_move_extend"_char_left"
128        end
129        for key in l \e\[C \eOC right
130            bind -s --preset -M $mode $key "fish_helix_command "$ns_move_extend"_char_right"
131        end
132        for key in k \e\[A \eOA up
133            bind -s --preset -M $mode $key "fish_helix_command char_up"
134        end
135        for key in j \e\[B \eOB down
136            bind -s --preset -M $mode $key "fish_helix_command char_down"
137        end
138
139        bind -s --preset -M $mode w "fish_helix_command next_word_start"
140        bind -s --preset -M $mode b "fish_helix_command prev_word_start"
141        bind -s --preset -M $mode e "fish_helix_command next_word_end"
142        bind -s --preset -M $mode W "fish_helix_command next_long_word_start"
143        bind -s --preset -M $mode B "fish_helix_command prev_long_word_start"
144        bind -s --preset -M $mode E "fish_helix_command next_long_word_end"
145
146        bind -s --preset -M $mode t "fish_helix_command till_next_char"
147        bind -s --preset -M $mode f "fish_helix_command find_next_char"
148        bind -s --preset -M $mode T "fish_helix_command till_prev_char"
149        bind -s --preset -M $mode F "fish_helix_command find_prev_char"
150
151        bind -s --preset -M $mode t\e ""
152        bind -s --preset -M $mode f\e ""
153        bind -s --preset -M $mode T\e ""
154        bind -s --preset -M $mode alt-f ""
155
156        for enter in \r \n
157            bind -s --preset -M $mode "t,$enter" "fish_helix_command till_next_cr"
158            bind -s --preset -M $mode "f,$enter" "fish_helix_command find_next_cr"
159            bind -s --preset -M $mode "T,$enter" "fish_helix_command till_prev_cr"
160            bind -s --preset -M $mode "F,$enter" "fish_helix_command find_prev_cr"
161        end
162
163        for key in gh \e\[H \eOH home
164            bind -s --preset -M $mode $key "fish_helix_command goto_line_start"
165        end
166        for key in gl \e\[F \eOF end
167            bind -s --preset -M $mode $key "fish_helix_command goto_line_end"
168        end
169        bind -s --preset -M $mode gs "fish_helix_command goto_first_nonwhitespace"
170        bind -s --preset -M $mode gg "fish_helix_command goto_file_start"
171        bind -s --preset -M $mode G "fish_helix_command goto_line"
172        bind -s --preset -M $mode ge "fish_helix_command goto_last_line"
173
174        # FIXME alt-. doesn't work with t/T
175        # FIXME alt-. doesn't work with [ftFT][\n\r]
176        bind -s --preset -M $mode \e. repeat-jump
177
178        # FIXME reselect after undo/redo
179        bind -s --preset -M $mode u undo begin-selection
180        bind -s --preset -M $mode U redo begin-selection
181
182        bind -s --preset -M $mode -m replace_one r repaint-mode
183
184        # FIXME registers
185        # bind -s --preset -M $mode y fish_clipboard_copy
186        # bind -s --preset -M $mode P fish_clipboard_paste
187        # bind -s --preset -M $mode R kill-selection begin-selection yank-pop yank
188
189        bind -s --preset -M $mode -m default d "fish_helix_command delete_selection; $commandline_v_repaint"
190        bind -s --preset -M $mode -m default \ed "fish_helix_command delete_selection_noyank; $commandline_v_repaint"
191        bind -s --preset -M $mode -m insert c "fish_helix_command delete_selection; commandline -f end-selection repaint-mode"
192        bind -s --preset -M $mode -m insert \ec "fish_helix_command delete_selection_noyank; commandline -f end-selection repaint-mode"
193
194        bind -s --preset -M $mode -m default y "fish_helix_command yank"
195        bind -s --preset -M $mode p "fish_helix_command paste_after"
196        bind -s --preset -M $mode P "fish_helix_command paste_before"
197        bind -s --preset -M $mode R "fish_helix_command replace_selection"
198
199        bind -s --preset -M $mode -m default " y" "fish_clipboard_copy; $commandline_v_repaint"
200        bind -s --preset -M $mode " p" "fish_helix_command paste_after_clip"
201        bind -s --preset -M $mode " P" "fish_helix_command paste_before_clip"
202        bind -s --preset -M $mode " R" "fish_helix_command replace_selection_clip"
203
204        # FIXME keep selection
205        bind -s --preset -M $mode '~' togglecase-selection
206        # FIXME ` and \e`
207
208        # FIXME .
209        # FIXME < and >
210        # FIXME =
211
212        # FIXME \ca \cx
213        # FIXME Qq
214
215        ## Shell
216        # FIXME
217
218        ## Selection manipulation
219        # FIXME & _
220
221        bind -s --preset -M $mode \; begin-selection
222        bind -s --preset -M $mode \e\; swap-selection-start-stop
223        # FIXME \e:
224
225        bind -s --preset -M $mode % "fish_helix_command select_all"
226
227        bind -s --preset -M $mode x end-selection beginning-of-line begin-selection end-of-line
228        # FIXME X \ex
229        # FIXME J
230        # FIXME \cc
231
232        ## Search
233        # FIXME
234
235        ## FIXME minor modes: g, m, space
236
237        ## FIXME [ and ] motions
238    end
239
240    # FIXME should replace the whole selection
241    # FIXME should be able to go back to visual mode
242    bind -s --preset -M replace_one -m default '' delete-char self-insert backward-char repaint-mode
243    bind -s --preset -M replace_one -m default \r 'commandline -f delete-char; commandline -i \n; commandline -f backward-char; commandline -f repaint-mode'
244    bind -s --preset -M replace_one -m default \e cancel repaint-mode
245
246    ## FIXME Insert mode keys
247
248    ## Old config from vi:
249
250    # Vi moves the cursor back if, after deleting, it is at EOL.
251    # To emulate that, move forward, then backward, which will be a NOP
252    # if there is something to move forward to.
253    bind -s --preset -M insert delete delete-char forward-single-char backward-char
254    bind -s --preset -M default delete delete-char forward-single-char backward-char
255
256    # Backspace deletes a char in insert mode, but not in normal/default mode.
257    bind -s --preset -M insert backspace backward-delete-char
258    bind -s --preset -M default backspace backward-char
259    bind -s --preset -M insert \ch backward-delete-char
260    bind -s --preset -M default \ch backward-char
261    bind -s --preset -M insert \x7f backward-delete-char
262    bind -s --preset -M default \x7f backward-char
263    bind -s --preset -M insert shift-delete backward-delete-char # shifted delete
264    bind -s --preset -M default shift-delete backward-delete-char # shifted delete
265
266    #    bind -s --preset '~' togglecase-char forward-single-char
267    #    bind -s --preset gu downcase-word
268    #    bind -s --preset gU upcase-word
269    #
270    #    bind -s --preset J end-of-line delete-char
271    #    bind -s --preset K 'man (commandline -t) 2>/dev/null; or echo -n \a'
272    #
273
274    # same vim 'pasting' note as upper
275    bind -s --preset '"*p' forward-char "commandline -i ( xsel -p; echo )[1]"
276    bind -s --preset '"*P' "commandline -i ( xsel -p; echo )[1]"
277
278    #
279    # visual mode
280    #
281
282    # bind -s --preset -M visual -m insert c kill-selection end-selection repaint-mode
283    # bind -s --preset -M visual -m insert s kill-selection end-selection repaint-mode
284    bind -s --preset -M visual -m default '"*y' "fish_clipboard_copy; commandline -f end-selection repaint-mode"
285    bind -s --preset -M visual -m default '~' togglecase-selection end-selection repaint-mode
286
287    # Set the cursor shape
288    # After executing once, this will have defined functions listening for the variable.
289    # Therefore it needs to be before setting fish_bind_mode.
290    fish_vi_cursor
291    set -g fish_cursor_selection_mode inclusive
292
293    set fish_bind_mode $init_mode
294
295end