-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
finder-vim.yaml
1385 lines (1373 loc) · 63.4 KB
/
finder-vim.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# yaml-language-server: $schema=./schema/karabiner-mod-schema.json
title: "Finder Vim Controls"
homepage: https://github.com/chrisgrieser/finder-vim-mode
maintainers: [pseudometa (chrisgrieser)]
anchors:
- conditions:
- &finder-app
type: frontmost_application_if
bundle_identifiers: [^com\.apple\.finder$]
- &is-German-keyboard
type: input_source_if
input_sources: [language: ^de$]
- ¬-German-keyboard
type: input_source_unless
input_sources: [language: ^de$]
- &is-insert-mode
type: variable_if
name: FINDER_INSERT_MODE
value: true
- ¬-insert-mode
type: variable_unless
name: FINDER_INSERT_MODE
value: true
- &is-find-mode
type: variable_if
name: FINDER_FIND_MODE
value: true
- ¬-find-mode
type: variable_unless
name: FINDER_FIND_MODE
value: true
- &move-active
type: variable_if
name: FINDER_MOVE_ACTIVE
value: true
- &move-inactive
type: variable_unless
name: FINDER_MOVE_ACTIVE
value: true
- &menu-is-open
type: variable_if
name: FINDER_MENU_ACTIVE
value: true
- &no-menu-open
type: variable_unless
name: FINDER_MENU_ACTIVE
value: true
#─────────────────────────────────────────────────────────────────────────────
- to:
- &enter-insert-mode
set_variable:
name: FINDER_INSERT_MODE
value: true
- &leave-insert-mode
set_variable:
name: FINDER_INSERT_MODE
value: false
- &insert-notice
set_notification_message:
id: insert-notice
text: "Finder Vim Mode\n\n✏️ INSERT"
- &remove-insert-notice
set_notification_message:
id: insert-notice
text: ""
- &enter-find-mode
set_variable:
name: FINDER_FIND_MODE
value: true
- &leave-find-mode
set_variable:
name: FINDER_FIND_MODE
value: false
- &find-notice
set_notification_message:
id: find-notice
text: "Finder Vim Mode\n\n🔎 FIND"
- &remove-find-notice
set_notification_message:
id: find-notice
text: ""
- &remove-move-notice
set_notification_message:
id: move-notice
text: ""
- &move-notice
set_notification_message:
id: move-notice
text: "Finder Vim Mode\n\n✂️ Move active"
- &start-move
set_variable:
name: FINDER_MOVE_ACTIVE
value: true
- &end-move
set_variable:
name: FINDER_MOVE_ACTIVE
value: false
- &start-menu
set_variable:
name: FINDER_MENU_ACTIVE
value: true
- &end-menu
set_variable:
name: FINDER_MENU_ACTIVE
value: false
- &remove-menu-notice
set_notification_message:
id: menu-notice
text: ""
- &menu-notice
set_notification_message:
id: menu-notice
text: "Finder Vim Mode\n\n➡️ Menu Mode"
#───────────────────────────────────────────────────────────────────────────────
#───────────────────────────────────────────────────────────────────────────────
#───────────────────────────────────────────────────────────────────────────────
rules:
- description: "Finder Vim Controls"
manipulators:
# hjkl
- type: basic
from: { key_code: j }
to: # no to_if_alone since it blocks repeat
- key_code: down_arrow
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
- type: basic
from: { key_code: k }
to: # no to_if_alone since it blocks repeat
- key_code: up_arrow
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# h: goto parent
- type: basic
from: { key_code: h }
to_if_alone:
- { key_code: up_arrow, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# l: open
- type: basic
from: { key_code: l }
to_if_alone:
- { key_code: o, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# HJKL
# H: goto parent in new window
- type: basic
from: { key_code: h, modifiers: { mandatory: [shift] } }
to_if_alone:
- { key_code: up_arrow, modifiers: [command, control] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# L: open & if folder in new window
- type: basic
from: { key_code: l, modifiers: { mandatory: [shift] } }
to_if_alone:
- shell_command: |
osascript -e '
tell application "Finder"
if (selection is {}) then
beep
else
repeat with aFolder in (selection as list)
open aFolder
end repeat
end if
end tell'
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# J: 6j
- type: basic
from: { key_code: j, modifiers: { mandatory: [shift] } }
to:
- key_code: down_arrow
- key_code: down_arrow
- key_code: down_arrow
- key_code: down_arrow
- key_code: down_arrow
- key_code: down_arrow
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# K: 6k
- type: basic
from: { key_code: k, modifiers: { mandatory: [shift] } }
to:
- key_code: up_arrow
- key_code: up_arrow
- key_code: up_arrow
- key_code: up_arrow
- key_code: up_arrow
- key_code: up_arrow
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# g(g), G,
- type: basic
from: { key_code: g }
to:
- key_code: up_arrow
modifiers: [option]
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
- type: basic
from: { key_code: g, modifiers: { mandatory: [shift] } }
to_if_alone:
- key_code: down_arrow
modifiers: [option]
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
#─────────────────────────────────────────────────────────────────────────
# :help
# "?" on German keyboard
- type: basic
from: { key_code: hyphen, modifiers: { mandatory: [shift] } }
to:
- shell_command:
qlmanage -p "$HOME/.config/karabiner/assets/finder-vim-mode/cheatsheet.png"
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *is-German-keyboard]
# "?" on American keyboard
- type: basic
from: { key_code: slash, modifiers: { mandatory: [shift] } }
to:
- shell_command:
qlmanage -p "$HOME/.config/karabiner/assets/finder-vim-mode/cheatsheet.png"
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *not-German-keyboard]
#─────────────────────────────────────────────────────────────────────────
# INSERT MODE
# ENTER
# Rename
- type: basic
from: { key_code: return_or_enter }
to:
- key_code: return_or_enter
- *insert-notice
- *enter-insert-mode
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# Batch Rename
- type: basic
from: { key_code: return_or_enter, modifiers: { mandatory: [shift] } }
to_if_alone:
- shell_command: |
osascript -e '
tell application "System Events" to tell process "Finder"
click menu item "Rename…" of menu "File" of menu bar 1
end tell'
- *insert-notice
- *enter-insert-mode
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# LEAVE INSERT MODE
# Confirm Insert Mode
- type: basic
from: { key_code: return_or_enter }
to:
- key_code: return_or_enter
- *remove-insert-notice
- *leave-insert-mode
conditions: [*finder-app, *is-insert-mode]
# SPOTLIGHT
# Opening Alfred/Raycast/Spotlight cancels renaming and context menu,
# so the respective modes have to be aborter here as well
- type: basic
from: { key_code: spacebar, modifiers: { mandatory: [command] } }
to:
- { key_code: spacebar, modifiers: [command] }
- *remove-insert-notice
- *leave-insert-mode
- *remove-menu-notice
- *end-menu
#─────────────────────────────────────────────────────────────────────────
#─────────────────────────────────────────────────────────────────────────
# ABORT VARIOUS MODES
# Hyper
- type: basic
from: { key_code: caps_lock }
to_if_alone:
- key_code: escape
- *remove-insert-notice
- *leave-insert-mode
- *remove-menu-notice
- *end-menu
- *remove-move-notice
- *end-move
- *remove-find-notice
- *leave-find-mode
# keep hyper binding if not pressed alone
to:
- { key_code: right_command, modifiers: [control, option] }
conditions: [*finder-app]
# regular Esc
- type: basic
from: { key_code: escape }
to_if_alone:
- key_code: escape
- *remove-insert-notice
- *leave-insert-mode
- *remove-menu-notice
- *end-menu
- *remove-move-notice
- *end-move
- *remove-find-notice
- *leave-find-mode
conditions: [*finder-app]
# Closing window or quit
- type: basic
from: { key_code: q, modifiers: { mandatory: [command] } }
to:
- { key_code: q, modifiers: [command] }
- *remove-insert-notice
- *leave-insert-mode
- *remove-menu-notice
- *end-menu
- *remove-move-notice
- *end-move
- *remove-find-notice
- *leave-find-mode
conditions: [*finder-app]
#─────────────────────────────────────────────────────────────────────────
# WINDOWS
- type: basic
from: { key_code: w, modifiers: { mandatory: [command] } }
to:
- shell_command: |
current_win=$(osascript -e '
tell application "Finder"
activate
if (count windows) is 0 then return ""
try
set pathToBeClosed to (POSIX path of (insertion location as alias))
close window 1
return pathToBeClosed
on error # special folders without insertion location, like Smart Folders
close window 1
return ""
end try
end tell
')
if [[ -n "$current_win" ]] ; then
mkdir -p "$HOME/.cache/finder-vim-mode"
echo "$current_win" >"$HOME/.cache/finder-vim-mode/last-closed-window"
fi
# closing win should end insert, menu, and find mode, but not cancel move
# since cmd+w can be called from various modes, it should thus also
# end all those modes
- *remove-insert-notice
- *leave-insert-mode
- *remove-menu-notice
- *end-menu
- *remove-find-notice
- *leave-find-mode
conditions: [*finder-app]
# q: close window (:quit)
- type: basic
from: { key_code: q }
to:
- shell_command: |
current_win=$(osascript -e '
tell application "Finder"
activate
if (count windows) is 0 then return ""
try
set pathToBeClosed to (POSIX path of (insertion location as alias))
close window 1
return pathToBeClosed
on error # special folders without insertion location, like Smart Folders
close window 1
return ""
end try
end tell
')
if [[ -n "$current_win" ]] ; then
mkdir -p "$HOME/.cache/finder-vim-mode"
echo "$current_win" >"$HOME/.cache/finder-vim-mode/last-closed-window"
fi
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# r = re-open window
- type: basic
from: { key_code: r }
to:
- shell_command: |
open "$(cat "$HOME/.cache/finder-vim-mode/last-closed-window")"
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# e = close all other windows (:only)
- type: basic
from: { key_code: e }
to:
- shell_command: |
current_win=$(osascript -e '
tell application "Finder"
set winPath to ""
repeat while Finder window 2 exists
if ((count Finder windows) is 2) then
set winPath to POSIX path of (target of window 2 as alias)
end if
close Finder window 2
end repeat
return winPath
end tell')
mkdir -p "$HOME/.cache/finder-vim-mode"
[[ -n "$current_win" ]] && echo "$current_win" >"$HOME/.cache/finder-vim-mode/last-closed-window"
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# <BS> = cycle windows (:bnext)
- type: basic
from: { key_code: delete_or_backspace }
to_if_alone:
- { key_code: non_us_backslash, modifiers: [command] }
- *end-menu
- *remove-menu-notice
- *leave-find-mode
- *remove-find-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# t = recent folders
- type: basic
from: { key_code: t }
to_if_alone:
- shell_command: |
osascript -e '
tell application "System Events" to tell process "Finder"
set frontmost to true
click menu item "Recent Folders" of menu "Go" of menu bar 1
key code 125 using {option down} # opt+down
key code 126 # up
key code 126 # up
key code 124 # right
end tell
'
- *start-menu
- *menu-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
#─────────────────────────────────────────────────────────────────────────
# Selections
# a = select all
- type: basic
from: { key_code: a }
to_if_alone:
- { key_code: a, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# A = select only files
- type: basic
from: { key_code: a, modifiers: { mandatory: [shift] } }
to_if_alone:
# INFO use "Finder window" instead of "window" to target only regular
# windows https://www.reddit.com/r/applescript/comments/uz9axo/comment/iayjrn4/?context=3
- shell_command: |
osascript -e ' tell application "Finder" to set the selection to files of Finder window 1'
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# MULTI-SELECT
# <S-Space> = add down to selection
- type: basic
from: { key_code: spacebar, modifiers: { mandatory: [shift] } }
to:
- { key_code: down_arrow, modifiers: [shift] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
#─────────────────────────────────────────────────────────────────────────
# c = context menu ("right-click")
- type: basic
from: { key_code: c }
to:
# INFO Finder needs to activate even though it is already frontmost,
# since running a shellcommand seems to temporarily take focus, so
# that there is no `AXfocusedUIElement` during execution
- shell_command: |
osascript -e '
tell application "Finder"
if the selection is {} then return
activate
end tell
delay 0.1
tell application "System Events" to tell process "Finder"
tell value of attribute "AXFocusedUIElement" to perform action "AXShowMenu"
end tell'
- *start-menu
- *menu-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *no-menu-open]
# close context menu with q or c
- type: basic
from: { key_code: q }
to_if_alone:
- key_code: escape
- *end-menu
- *remove-menu-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *menu-is-open]
- type: basic
from: { key_code: c }
to_if_alone:
- key_code: escape
- *end-menu
- *remove-menu-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *menu-is-open]
# changed navigation in menu mode
- type: basic
from: { key_code: l }
to_if_alone:
- { key_code: right_arrow }
conditions: [*finder-app, *menu-is-open]
- type: basic
from: { key_code: h }
to_if_alone:
- { key_code: left_arrow }
conditions: [*finder-app, *menu-is-open]
- type: basic
from: { key_code: tab }
to_if_alone:
- { key_code: down_arrow }
conditions: [*finder-app, *menu-is-open]
- type: basic
from: { key_code: tab, modifiers: { mandatory: [shift] } } # y = z in German Keyboard
to_if_alone:
- { key_code: up_arrow }
conditions: [*finder-app, *menu-is-open]
# Confirm Menu Selection
- type: basic
from: { key_code: return_or_enter }
to:
- key_code: return_or_enter
- *end-menu
- *remove-menu-notice
conditions: [*finder-app, *menu-is-open]
# INFO: context menu cancellation configured with the insert mode cancellations
#─────────────────────────────────────────────────────────────────────────
# BASIC FILE OPERATIONS
# d = delete (or put back, when in Trash)
- type: basic
from: { key_code: d }
to_if_alone:
- { key_code: delete_or_backspace, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# D = open trash
- type: basic
from: { key_code: d, modifiers: { mandatory: [shift] } }
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
to:
# to open the "unified" trash (iCloud trash + user trash), needs to use
# AppleScript and not `open "$HOME/.Trash"`
- shell_command: |
osascript -e'
tell application "Finder"
activate
open the trash
end tell
tell application "System Events" to tell process "Finder"
set frontmost to true
end tell
'
# y = yank (copy) file
- type: basic
from: { key_code: z } # y = z in German Keyboard
to_if_alone:
- { key_code: c, modifiers: [command] }
- shell_command: |
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "✅ File(s) copied"
conditions:
[*finder-app, *not-insert-mode, *move-inactive, *not-find-mode, *is-German-keyboard]
- type: basic
from: { key_code: "y" }
to:
- { key_code: c, modifiers: [command] }
- shell_command: |
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "✅ File(s) copied"
conditions:
[*finder-app, *not-insert-mode, *move-inactive, *not-find-mode, *not-German-keyboard]
# Y = yank (copy) file *content*
- type: basic
from: { key_code: z, modifiers: { mandatory: [shift] } } # y = z in German Keyboard
to_if_alone:
- shell_command: |
export LC_CTYPE="en_US.UTF-8" # fixes encoding issue when copying
selection=$(osascript -e 'tell application "Finder" to return POSIX path of (selection as text)')
cat "$selection" | pbcopy
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "✅ File content copied"
conditions:
[*finder-app, *not-insert-mode, *move-inactive, *not-find-mode, *is-German-keyboard]
- type: basic
from: { key_code: y, modifiers: { mandatory: [shift] } }
to:
- shell_command: |
export LC_CTYPE="en_US.UTF-8" # fixes encoding issue when copying
selection=$(osascript -e 'tell application "Finder" to return POSIX path of (selection as text)')
cat "$selection" | pbcopy
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "✅ File content copied"
conditions:
[*finder-app, *not-insert-mode, *move-inactive, *not-find-mode, *not-German-keyboard]
# p = paste file
- type: basic
from: { key_code: p }
to_if_alone:
- { key_code: v, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *move-inactive, *not-find-mode]
# P = paste (append) text into file
- type: basic
from: { key_code: p, modifiers: { mandatory: [shift] } }
to:
- shell_command: |
export LC_CTYPE="en_US.UTF-8" # fixes encoding issue when pasting
selection=$(osascript -e 'tell application "Finder" to return POSIX path of (selection as text)')
pbpaste >> "$selection"
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "✅ Text appended to file"
conditions: [*finder-app, *not-insert-mode, *move-inactive, *not-find-mode]
# w = duplicate (mnemonic: double-u)
- type: basic
from: { key_code: w }
to_if_alone:
- { key_code: d, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# x = cross-move items to other Finder window (if exactly 2 Finder windows)
- type: basic
from: { key_code: x }
to_if_alone:
- shell_command: |
error=$(osascript -e'
tell application "Finder"
activate
if (number of Finder windows is not 2) then
beep
return "⚠️ Not exactly two windows"
else if (the selection is {}) then
beep
return "⚠️ No selection"
else
set otherWin to (target of Finder window 2 as alias)
repeat with aFile in (selection as list)
# AppleScript does not allow the "ask for confirmation" prompt if replacing,
# thus implementing it manually https://www.macscripter.net/t/mac-finder-native-copy-dialog/54200/13
try
move aFile to otherWin without replacing
on error
set promptText to "An item named " & quoted form of (name of aFile as string) & " already exists in this location."
set response to button returned of (display dialog promptText buttons {"Skip", "Replace"} default button "Replace" cancel button "Skip" with icon caution)
if response is "Replace" then move aFile to otherWin with replacing
end try
end repeat
return ""
end if
end tell
')
[[ -n "$error" ]] && "$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "$error"
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# X = cross-copy (duplicate)
- type: basic
from: { key_code: x, modifiers: { mandatory: [shift] } }
to_if_alone:
- shell_command: |
error=$(osascript -e'
tell application "Finder"
activate
if (number of Finder windows is not 2) then
beep
return "⚠️ Not exactly two windows"
else if (the selection is {}) then
beep
return "⚠️ No selection"
else
set otherWin to (target of Finder window 2 as alias)
repeat with aFile in (selection as list)
# AppleScript does not allow the "ask for confirmation" prompt if replacing,
# thus implementing it manually https://www.macscripter.net/t/mac-finder-native-copy-dialog/54200/13
try
duplicate aFile to otherWin without replacing
on error
set promptText to "An item named " & quoted form of (name of aFile as string) & " already exists in this location."
set response to button returned of (display dialog promptText buttons {"Skip", "Replace"} default button "Replace" cancel button "Skip" with icon caution)
if response is "Replace" then duplicate aFile to otherWin with replacing
end try
end repeat
return ""
end if
end tell
')
[[ -n "$error" ]] && "$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "$error"
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# v = open first volume
- type: basic
from: { key_code: v }
to_if_alone:
- shell_command: |
first_volume=$(df | grep --max-count=1 " /Volumes/" | awk -F ' ' '{print $NF}')
[[ -z "$first_volume" ]] && "$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "No ejectable volume found."
error=$(osascript -e "
set the_volume to (\"$first_volume\" as POSIX file)
tell application \"Finder\" to open the_volume
")
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# u = undo
- type: basic
from: { key_code: u }
to_if_alone:
- { key_code: z, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *not-German-keyboard]
- type: basic
from: { key_code: u }
to:
- { key_code: "y", modifiers: [command] } # y = z in German keyboard
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *is-German-keyboard]
# U = redo
- type: basic
from: { key_code: u, modifiers: { mandatory: [shift] } }
to:
- { key_code: z, modifiers: [shift, command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *not-German-keyboard]
- type: basic
from: { key_code: u, modifiers: { mandatory: [shift] } }
to:
- { key_code: "y", modifiers: [shift, command] } # y = z in German keyboard
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *is-German-keyboard]
# N = New Folder
- type: basic
from: { key_code: n, modifiers: { mandatory: [shift] } }
to:
- { key_code: n, modifiers: [shift, command] }
- *enter-insert-mode # enters insert mode since folder renaming is also triggered
- *insert-notice
- shell_command: |
# play sound at current volume
current_vol=$(osascript -e 'output volume of (get volume settings)')
[[ "$current_vol" == "missing value" ]] && current_vol=50
vol_percent=$(echo "scale=2 ; $current_vol / 100" | bc) # afplay play with 100% volume by default
afplay --volume "$vol_percent" "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/facetime/multiway_join.caf" & # codespell-ignore
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# M = Move selection to new folder
- type: basic
from: { key_code: m, modifiers: { mandatory: [shift] } }
to:
- { key_code: n, modifiers: [control, command] }
- *enter-insert-mode # since folder renaming is also triggered
- *insert-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# n = New File
- type: basic
from: { key_code: n }
to_if_alone:
- shell_command: |
osascript -e '
tell application "Finder"
-- create new file
activate
set thisFolder to the target of the Finder window 1 as alias
set newFile to (make new file at thisFolder)
select newFile
-- trigger rename once file is selected
set i to 0
set selectedFile to ""
repeat until (selectedFile is newFile)
set selectedFile to (item 1 of (get selection))
delay 0.1
if i > 25 then exit repeat
end repeat
tell application "System Events" to key code 36 -- 36 = enter
end tell
'
# play sound at current volume
current_vol=$(osascript -e 'output volume of (get volume settings)')
[[ "$current_vol" == "missing value" ]] && current_vol=50
vol_percent=$(echo "scale=2 ; $current_vol / 100" | bc) # afplay play with 100% volume by default
afplay --volume "$vol_percent" "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/begin_record.caf" &
- *enter-insert-mode # since renaming is triggered
- *insert-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# b = Toggle `.bkp` (Backup).
- type: basic
from: { key_code: b }
to:
- shell_command: |
# if the selection is a symlink, "selection as alias" returns the
# path of the target, while "selection as text" returns the path of
# the symlink (which is desired here)
selectedFiles=$(osascript -e '
set out to ""
tell application "Finder" to set selectedFiles to (every item of (get selection))
repeat with theFile in selectedFiles
set out to out & (POSIX path of (theFile as text)) & linefeed
end repeat
return out
' | sed -e 's|/$||')
if [[ -z "$selectedFiles" ]] ; then
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "⚠️ No selection."
exit 1
fi
echo "$selectedFiles" | while read -r file; do
# bash expansion does not work in karabiner
lastFourChars=$(echo "$file" | rev | cut -c-4 | rev)
if [[ "$lastFourChars" == "-bkp" ]]; then
newName=$(echo "$file" | rev | cut -c5- | rev)
else
newName="${file}-bkp"
fi
mv -n "$file" "$newName"
done
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/begin_record.caf" & # codespell-ignore
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# s = Create Symlink
- type: basic
from: { key_code: s }
to:
- shell_command: |
selectedFiles=$(osascript -e '
set out to ""
tell application "Finder" to set selectedFiles to (every item of (get selection))
repeat with theFile in selectedFiles
set out to out & (POSIX path of (theFile as text)) & linefeed
end repeat
return out
' | sed -e 's|/$||')
if [[ -z "$selectedFiles" ]] ; then
"$HOME/.config/karabiner/assets/finder-vim-mode/notificator" \
--title "Finder Vim Mode" --message "⚠️ No selection."
exit 1
fi
echo "$selectedFiles" | while read -r file; do
ln -sf "$file" "${file}-symlink"
done
first_symlink="$(echo "$selectedFiles" | head -n1)-symlink"
open -R "$first_symlink"
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/begin_record.caf" & # codespell-ignore
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# S = follow symlink (or alias)
- type: basic
from: { key_code: s, modifiers: { mandatory: [shift] } }
to:
- { key_code: r, modifiers: [command] }
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# z = zip files
# (if single directory is selected, zip its file contents instead)
- type: basic
from: { key_code: "y" } # y and z switched on German keyboard
to_if_alone:
# WARN needs to be written twice, once for German, once for English (see below)
- shell_command: |
sel=$(osascript -e 'tell application "Finder" to POSIX path of (item 1 of (get selection) as text)')
selection_count=$(osascript -e 'tell application "Finder" to count (selection as list)')
if [[ -d "$sel" && $selection_count -eq 1 ]] ; then
cd "$sel"
foldername=$(basename "$sel")
zip --recurse-paths --symlinks "../$foldername.zip" .
open -R "../$foldername.zip"
# play sound at current volume
current_vol=$(osascript -e 'output volume of (get volume settings)')
[[ "$current_vol" == "missing value" ]] && current_vol=50
vol_percent=$(echo "scale=2 ; $current_vol / 100" | bc) # afplay play with 100% volume by default
(afplay --volume "$vol_percent" "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif" &)
else
osascript -e '
tell application "System Events" to tell process "Finder"
click menu item "Compress" of menu "File" of menu bar 1
end tell'
fi
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *is-German-keyboard]
- type: basic
from: { key_code: z }
to:
# WARN needs to be written twice, once for German, once for English (see above)
- shell_command: |
sel=$(osascript -e 'tell application "Finder" to POSIX path of (item 1 of (get selection) as text)')
selection_count=$(osascript -e 'tell application "Finder" to count (selection as list)')
if [[ -d "$sel" && $selection_count -eq 1 ]] ; then
cd "$sel"
foldername=$(basename "$sel")
zip --recurse-paths --symlinks "../$foldername.zip" .
open -R "../$foldername.zip"
# play sound at current volume
current_vol=$(osascript -e 'output volume of (get volume settings)')
[[ "$current_vol" == "missing value" ]] && current_vol=50
vol_percent=$(echo "scale=2 ; $current_vol / 100" | bc) # afplay play with 100% volume by default
(afplay --volume "$vol_percent" "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif" &)
else
osascript -e '
tell application "System Events" to tell process "Finder"
click menu item "Compress" of menu "File" of menu bar 1
end tell'
fi
conditions: [*finder-app, *not-insert-mode, *not-find-mode, *not-German-keyboard]
# toggle hidden files visibility
- type: basic
from: { key_code: period }
to:
- { key_code: period, modifiers: [command, shift] }
- shell_command: |
afplay '/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/facetime/multiway_leave.caf' &
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
#─────────────────────────────────────────────────────────────────────────
# Navigation
# o = open/expand fold (O: do it recursively)
- type: basic
from: { key_code: o }
to_if_alone:
- key_code: right_arrow
- shell_command: |
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/mic_unmute.caf" &
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
- type: basic
from: { key_code: o, modifiers: { mandatory: [shift] } }
to_if_alone:
- { key_code: right_arrow, modifiers: [option] }
- shell_command: |
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/mic_unmute.caf" &
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# i = close/collapse fold (I: do it recursively)
- type: basic
from: { key_code: i }
to_if_alone:
- key_code: left_arrow
- shell_command: |
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/mic_mute.caf" &
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
- type: basic
from: { key_code: i, modifiers: { mandatory: [shift] } }
to_if_alone:
- { key_code: left_arrow, modifiers: [option] }
- shell_command: |
afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/mic_mute.caf" &
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# <C-h>/<C-l> = History back/forward
- type: basic
from: { key_code: h, modifiers: { mandatory: [control] } }
to_if_alone:
- { key_code: semicolon, modifiers: [command] }
- *leave-insert-mode # going back aborts searching / renames
conditions: [*finder-app, *not-find-mode, *is-German-keyboard]
- type: basic
from: { key_code: h, modifiers: { mandatory: [control] } }
to_if_alone:
- { key_code: open_bracket, modifiers: [command] }
- *leave-insert-mode
conditions: [*finder-app, *not-find-mode, *not-German-keyboard]
- type: basic
from: { key_code: l, modifiers: { mandatory: [control] } }
to_if_alone:
- { key_code: quote, modifiers: [command] }
- *leave-insert-mode
conditions: [*finder-app, *not-find-mode, *is-German-keyboard]
- type: basic
from: { key_code: l, modifiers: { mandatory: [control] } }
to_if_alone:
- { key_code: close_bracket, modifiers: [command] }
- *leave-insert-mode
conditions: [*finder-app, *not-find-mode, *not-German-keyboard]
#─────────────────────────────────────────────────────────────────────────
# FIND MODE
# Enter/Exit
- type: basic
from: { key_code: f }
to_if_alone:
- *enter-find-mode
- *find-notice
conditions: [*finder-app, *not-insert-mode, *not-find-mode]
# One-Char-Searches
- type: basic
from: { key_code: a, modifiers: { optional: [shift] } }
to: [{ key_code: a }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: b, modifiers: { optional: [shift] } }
to: [{ key_code: b }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: c, modifiers: { optional: [shift] } }
to: [{ key_code: c }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: d, modifiers: { optional: [shift] } }
to: [{ key_code: d }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: e, modifiers: { optional: [shift] } }
to: [{ key_code: e }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: f, modifiers: { optional: [shift] } }
to: [{ key_code: f }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: g, modifiers: { optional: [shift] } }
to: [{ key_code: g }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: h, modifiers: { optional: [shift] } }
to: [{ key_code: h }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: i, modifiers: { optional: [shift] } }
to: [{ key_code: i }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: j, modifiers: { optional: [shift] } }
to: [{ key_code: j }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: k, modifiers: { optional: [shift] } }
to: [{ key_code: k }, *leave-find-mode, *remove-find-notice]
conditions: [*finder-app, *is-find-mode]
- type: basic
from: { key_code: l, modifiers: { optional: [shift] } }
to: [{ key_code: l }, *leave-find-mode, *remove-find-notice]