-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzsh-vimode-visual.zsh
954 lines (871 loc) · 22.7 KB
/
zsh-vimode-visual.zsh
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
#!/bin/zsh -f
bindkey -N vivis
get-x-clipboard()
{
local clippaste clipboard
if (( $+commands[pbpaste] )); then
clippaste="pbpaste"
elif (( $+commands[xsel] )); then
clippaste="xsel --clipboard --output"
else
return 1
fi
clipboard="$( ${=clippaste} )"
if [[ -n $clipboard && $clipboard != $CUTBUFFER ]]; then
killring=("$CUTBUFFER" "${(@)killring[1,-2]}")
CUTBUFFER="$clipboard"
fi
}
set-x-clipboard()
{
local clipcopy clipboard
if (( $+commands[pbcopy] )); then
clipcopy="pbcopy"
elif (( $+commands[xsel] )); then
clipcopy="xsel --clipboard --input"
else
return 1
fi
printf -- "$@" | ${=clipcopy}
}
vi-set-buffer()
{
read -k keys
if [[ $keys == '+' ]]; then
_clipcopy='+'
else
zle -U $keys
zle .vi-set-buffer
fi
}
zle -N vi-set-buffer
vi-put-after()
{
local cbuf
if [[ $_clipcopy == '+' ]]; then
cbuf="$CUTBUFFER"
get-x-clipboard
zle .vi-put-after
unset _clipcopy
CUTBUFFER="$cbuf"
else
zle .vi-put-after
fi
}
zle -N vi-put-after
vi-put-before()
{
local cbuf
if [[ $_clipcopy == '+' ]]; then
cbuf="$CUTBUFFER"
get-x-clipboard
zle .vi-put-before
unset _clipcopy
CUTBUFFER="$cbuf"
else
zle .vi-put-before
fi
}
zle -N vi-put-before
# _ _ _ _
# __ _(_) _ __ ___ ___ __| | ___ __ _(_)___ _ _ __ _| |
# \ \ / / | | '_ ` _ \ / _ \ / _` |/ _ \ \ \ / / / __| | | |/ _` | |
# \ V /| | | | | | | | (_) | (_| | __/ \ V /| \__ \ |_| | (_| | |
# \_/ |_| |_| |_| |_|\___/ \__,_|\___| \_/ |_|___/\__,_|\__,_|_|
#
# create a new keymap and remap a few key's with other or new widget's
# Main Highlighting Widget for VISUAL Mode
vi-visual-highlight()
{
integer CURSOR_HL MARK_HL
if [[ $CURSOR -gt $MARK ]];then
(( CURSOR_HL = CURSOR + 1 ))
__regstart=$MARK
__regend=$CURSOR_HL
region_highlight=("${MARK} ${CURSOR_HL} standout")
elif [[ $MARK -gt $CURSOR ]];then
(( MARK_HL = MARK + 1 ))
__regstart=$CURSOR
__regend=$MARK_HL
region_highlight=("${CURSOR} ${MARK_HL} standout")
elif [[ $MARK -eq $CURSOR ]];then
__regstart=$CURSOR
__regend=$MARK
region_highlight=("${CURSOR} ${MARK} standout")
fi
}
zle -N vi-visual-highlight
# Start Vi Visual mode
vi-visual-mode()
{
zle -K vivis
MARK=$CURSOR
zle vi-visual-highlight
}
zle -N vi-visual-mode
# Exit Vi Visual mode and go to vi-cmd-mode
vi-visual-exit()
{
region_highlight=("0 0 standout")
(( CURSOR = CURSOR + 1 ))
MARK=0
__regstart=0
__regend=0
zle .vi-cmd-mode
}
zle -N vi-visual-exit
# Vi Visual Kill
vi-visual-kill()
{
if [[ $CURSOR -gt $MARK ]];then
(( CURSOR = CURSOR + 1 ))
elif [[ $MARK -gt $CURSOR ]];then
(( MARK = MARK + 1 ))
elif [[ $MARK -eq $CURSOR ]];then
zle .vi-delete-char
return 0
fi
zle .kill-region
}
zle -N vi-visual-kill
# Exit Vi Visual and enter Insert mode
vi-visual-exit-to-insert()
{
region_highlight=("0 0 standout")
MARK=0
__regstart=0
__regend=0
zle .vi-insert
}
zle -N vi-visual-exit-to-insert
# Exit VISUAL mode and enter VLines Mode keeping the region
vi-visual-exit-to-vlines()
{
zle -K vivli
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start2=$CURSOR
zle .end-of-line -N
__end2=$CURSOR
CURSOR=$MARK
zle .vi-beginning-of-line -N
__start1=$CURSOR
zle .end-of-line -N
__end1=$CURSOR
zle vi-vlines-highlight
}
zle -N vi-visual-exit-to-vlines
# Exit Vi Visual and open line above
vi-visual-open-above()
{
region_highlight=("0 0 standout")
MARK=0
zle .vi-open-line-above
}
zle -N vi-visual-open-above
# Vi Visual move to matched bracket
vi-visual-match-bracket()
{
zle .vi-match-bracket
zle vi-visual-highlight
}
zle -N vi-visual-match-bracket
# Vi Visual move to column
vi-visual-goto-column()
{
zle .vi-goto-column
zle vi-visual-highlight
}
zle -N vi-visual-goto-column
# Vi Visual move back to first non-blank char
vi-visual-first-non-blank()
{
zle .vi-first-non-blank
zle vi-visual-highlight
}
zle -N vi-visual-first-non-blank
# Vi Visual repeat find
vi-visual-repeat-find()
{
zle .vi-repeat-find
zle vi-visual-highlight
}
zle -N vi-visual-repeat-find
# Vi Visual reverse repeat find
vi-visual-rev-repeat-find()
{
zle .vi-rev-repeat-find
zle vi-visual-highlight
}
zle -N vi-visual-rev-repeat-find
# Vi Visual kill whole line and enter to viins
vi-visual-substitute-lines()
{
local EOL
integer n
CURSOR=$__regend
zle .vi-end-of-line -N
EOL=$CURSOR
CURSOR=$__regstart
zle .vi-first-non-blank
n=$CURSOR
while [[ $n -lt $EOL ]]
do
zle .delete-char
(( n++ ))
done
zle vi-visual-exit-to-insert
}
zle -N vi-visual-substitute-lines
# Vi Visual move backward-blank-word
vi-visual-backward-blank-word()
{
zle .vi-backward-blank-word
zle vi-visual-highlight
}
zle -N vi-visual-backward-blank-word
# Vi Visual move forward-blank-word
vi-visual-forward-blank-word-end()
{
zle .vi-forward-blank-word-end
zle vi-visual-highlight
}
zle -N vi-visual-forward-blank-word-end
# Vi Visual move to prev char x
vi-visual-find-prev-char()
{
zle .vi-find-prev-char
zle vi-visual-highlight
}
zle -N vi-visual-find-prev-char
# Vi Visual insert bol
vi-visual-insert-bol()
{
zle vi-visual-exit-to-insert
zle .vi-insert-bol
}
zle -N vi-visual-insert-bol
# Vi Visual Join Lines
vi-visual-join()
{
CURSOR=$__regstart
while [[ $RBUFFER == *$'\n'* && $CURSOR -lt $__regend ]]
do
zle .vi-join
done
zle vi-visual-exit
}
zle -N vi-visual-join
# Vi Visual move to prev char x and skip
vi-visual-find-prev-char-skip()
{
zle .vi-find-prev-char-skip
zle vi-visual-highlight
}
zle -N vi-visual-find-prev-char-skip
# Vi Visual move forward blank word
vi-visual-forward-blank-word()
{
zle .vi-forward-blank-word
zle vi-visual-highlight
}
zle -N vi-visual-forward-blank-word
# Vi Visual move backward word
vi-visual-backward-word()
{
zle .vi-backward-word
zle vi-visual-highlight
}
zle -N vi-visual-backward-word
# Vi Visual change
vi-visual-change()
{
zle vi-visual-kill
set-x-clipboard "$CUTBUFFER"
zle vi-visual-exit-to-insert
}
zle -N vi-visual-change
# Vi Visual Kill and enter vicmd
vi-visual-kill-and-vicmd()
{
zle vi-visual-kill
set-x-clipboard "$CUTBUFFER"
zle vi-visual-exit
}
zle -N vi-visual-kill-and-vicmd
# Vi Visual move forward to word end
vi-visual-forward-word-end()
{
zle .vi-forward-word-end
zle vi-visual-highlight
}
zle -N vi-visual-forward-word-end
# Vi Visual move to next char x
vi-visual-find-next-char()
{
zle .vi-find-next-char
zle vi-visual-highlight
}
zle -N vi-visual-find-next-char
# Vi Visual move backward
vi-visual-backward-char()
{
zle .vi-backward-char
zle vi-visual-highlight
}
zle -N vi-visual-backward-char
# Vi Visual move down
vi-visual-down-line()
{
setopt extended_glob
local NL_CHAR NL_SUM
NL_CHAR=${RBUFFER//[^$'\n']/}
if [[ $RBUFFER == *$'\n'* && $#NL_CHAR -ge $NUMERIC ]]; then
zle .down-line-or-history
zle vi-visual-highlight
else
return 1
fi
}
zle -N vi-visual-down-line
# Vi Visual move up
vi-visual-up-line()
{
setopt extended_glob
local NL_CHAR
NL_CHAR=${LBUFFER//[^$'\n']/}
if [[ $LBUFFER == *$'\n'* && $#NL_CHAR -ge $NUMERIC ]]; then
zle .up-line-or-history
zle vi-visual-highlight
else
return 1
fi
}
zle -N vi-visual-up-line
# Vi Visual move forward
vi-visual-forward-char()
{
zle .vi-forward-char
zle vi-visual-highlight
}
zle -N vi-visual-forward-char
# Vi Visual Put
vi-visual-put()
{
zle vi-visual-kill
zle vi-visual-exit
(( CURSOR = CURSOR - 1 ))
if [[ $_clipcopy == '+' ]]; then
local cbuf
cbuf="$CUTBUFFER"
get-x-clipboard
zle .vi-put-after
unset _clipcopy
CUTBUFFER="$cbuf"
else
zle -U 2 && zle .vi-set-buffer
zle .vi-put-after
fi
}
zle -N vi-visual-put
# Vi Visual exchange start and end of region
vi-visual-exchange-points()
{
local CS_SAVE
CS_SAVE=$CURSOR
CURSOR=$MARK
MARK=$CS_SAVE
zle vi-visual-highlight
}
zle -N vi-visual-exchange-points
# Vi Visual move to till char x
vi-visual-find-next-char-skip()
{
zle .vi-find-next-char-skip
zle vi-visual-highlight
}
zle -N vi-visual-find-next-char-skip
# Vi Visual lowercase region
vi-visual-lowercase-region()
{
local LCSTART LCEND
(( LCSTART = __regstart + 1 ))
LCEND=$__regend
if [[ $__regstart == $__regend ]]; then
BUFFER[${LCSTART}]="${(L)BUFFER[${LCSTART}]}"
zle vi-visual-exit
else
BUFFER[${LCSTART},${LCEND}]="${(L)BUFFER[${LCSTART},${LCEND}]}"
zle vi-visual-exit
fi
}
zle -N vi-visual-lowercase-region
# Vi Visual uppercase region
vi-visual-uppercase-region()
{
local LCSTART LCEND
(( LCSTART = __regstart + 1 ))
LCEND=$__regend
if [[ $__regstart == $__regend ]]; then
BUFFER[${LCSTART}]="${(U)BUFFER[${LCSTART}]}"
CURSOR=$__regstart
zle vi-visual-exit
else
BUFFER[${LCSTART},${LCEND}]="${(U)BUFFER[${LCSTART},${LCEND}]}"
CURSOR=$__regstart
zle vi-visual-exit
fi
}
zle -N vi-visual-uppercase-region
# Vi Visual replace region
vi-visual-replace-region()
{
integer n
local LCSTART LCEND
(( LCSTART = __regstart + 1 ))
LCEND=$__regend
if [[ $__regstart == $__regend ]]; then
read -k key
BUFFER[${LCSTART}]="$key"
zle vi-visual-exit
else
read -k key
n=$LCSTART
while [[ $n -le ${LCEND} ]]
do
if [[ ! $BUFFER[$n] == $'\n' ]] && [[ -n $BUFFER[$n] ]]; then
BUFFER[$n]="$key"
fi
(( n++ ))
done
CURSOR=$__regstart
zle vi-visual-exit
fi
}
zle -N vi-visual-replace-region
# Vi Visual move word forward
vi-visual-forward-word()
{
zle .vi-forward-word
zle vi-visual-highlight
}
zle -N vi-visual-forward-word
# Vi Visual Yank
vi-visual-yank()
{
if [[ $__regstart == $__regend ]]; then
zle .vi-yank
zle vi-visual-exit
else
zle .copy-region-as-kill "$BUFFER[${__regstart}+1,${__regend}]"
zle vi-visual-exit
fi
set-x-clipboard "$CUTBUFFER"
}
zle -N vi-visual-yank
# Vi Visual move to bol
vi-visual-bol()
{
zle .vi-digit-or-beginning-of-line
zle vi-visual-highlight
}
zle -N vi-visual-bol
# Vi Visual move to eol
vi-visual-eol()
{
zle .vi-end-of-line
zle .vi-backward-char
zle vi-visual-highlight
}
zle -N vi-visual-eol
# Some use(less|ful) keybindings
# I took this from grml's zshrc
if [[ "$TERM" != emacs ]] ; then
[[ -z "$terminfo[kcuu1]" ]] || bindkey -M vivis "$terminfo[kcuu1]" vi-visual-up-line
[[ -z "$terminfo[kcud1]" ]] || bindkey -M vivis "$terminfo[kcud1]" vi-visual-down-line
[[ -z "$terminfo[kcuf1]" ]] || bindkey -M vivis "$terminfo[kcuf1]" vi-visual-forward-char
[[ -z "$terminfo[kcub1]" ]] || bindkey -M vivis "$terminfo[kcub1]" vi-visual-backward-char
# ncurses stuff:
[[ "$terminfo[kcuu1]" == $'\eO'* ]] && bindkey -M vivis "${terminfo[kcuu1]/O/[}" vi-visual-up-line
[[ "$terminfo[kcud1]" == $'\eO'* ]] && bindkey -M vivis "${terminfo[kcud1]/O/[}" vi-visual-down-line
[[ "$terminfo[kcuf1]" == $'\eO'* ]] && bindkey -M vivis "${terminfo[kcuf1]/O/[}" vi-visual-forward-char
[[ "$terminfo[kcub1]" == $'\eO'* ]] && bindkey -M vivis "${terminfo[kcub1]/O/[}" vi-visual-backward-char
fi
# _ _ _ _
# __ _(_) _ __ ___ ___ __| | ___ __ _| (_)_ __ ___ ___
# \ \ / / | | '_ ` _ \ / _ \ / _` |/ _ \ \ \ / / | | '_ \ / _ \/ __|
# \ V /| | | | | | | | (_) | (_| | __/ \ V /| | | | | | __/\__ \
# \_/ |_| |_| |_| |_|\___/ \__,_|\___| \_/ |_|_|_| |_|\___||___/
#
# Create new keymap using existing vicmd keymap
bindkey -N vivli vicmd
bindkey -M vivli -r 'i'
bindkey -M vivli -r 'I'
bindkey -M vivli -r 'a'
bindkey -M vivli -r 'A'
bindkey -M vivli 'u' vi-visual-lowercase-region
bindkey -M vivli 'U' vi-visual-uppercase-region
bindkey -M vivli 'r' vi-visual-replace-region
bindkey -M vivli 'J' vi-visual-join
bindkey -M vivli 'y' vi-visual-yank
bindkey -M vivli 'Y' vi-visual-yank
bindkey -M vivli '^[' vi-visual-exit
#bindkey -M vivli 'jj' vi-visual-exit
bindkey -M vivli 'V' vi-visual-exit
bindkey -M vivli 'c' vi-visual-substitute-lines
bindkey -M vivli 'C' vi-visual-substitute-lines
#bindkey -M vivli 'S' vi-visual-substitute-lines
bindkey -M vivli 'R' vi-visual-substitute-lines
# Highlight Lines
vi-vlines-highlight()
{
if [[ $__start1 == $__start2 ]] && [[ $__end1 == $__end2 ]]; then
__regstart=$__start1
__regend=$__end1
region_highlight=("${__regstart} ${__regend} standout")
CURSOR=$__savepos
elif [[ $__start1 -lt $__start2 ]] && [[ $__end1 -lt $__end2 ]]; then
__regstart=$__start1
__regend=$__end2
region_highlight=("${__regstart} ${__regend} standout")
CURSOR=$__savepos
elif [[ $__start1 -gt $__start2 ]] && [[ $__end1 -gt $__end2 ]]; then
__regstart=$__start2
__regend=$__end1
region_highlight=("${__regstart} ${__regend} standout")
CURSOR=$__savepos
fi
}
zle -N vi-vlines-highlight
# Vi Visual Lines Mode
vi-vlines-mode()
{
zle -K vivli
__csorig=$CURSOR
__csbefore=$CURSOR
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start1=$CURSOR
__start2=$CURSOR
zle .end-of-line -N
__end1=$CURSOR
__end2=$CURSOR
zle vi-vlines-highlight
}
zle -N vi-vlines-mode
vi-visual-whole-line()
{
__csorig=$CURSOR
__csbefore=$CURSOR
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start1=$CURSOR
__start2=$CURSOR
zle .end-of-line -N
__end1=$CURSOR
__end2=$CURSOR
zle vi-vlines-highlight
}
zle -N vi-visual-whole-line
# Exchange Start and End Point of Visual Lines Mode
vi-vlines-exchange-points()
{
local SAVE_S1 SAVE_E1
__csbefore=$__csorig
__csorig=$CURSOR
__savepos=$__csbefore
SAVE_S1=$__start1
SAVE_E1=$__end1
__start1=$__start2
__start2=$SAVE_S1
__end1=$__end2
__end2=$SAVE_E1
zle vi-vlines-highlight
}
zle -N vi-vlines-exchange-points
# VI Visual Lines down
vi-vlines-down-line()
{
setopt extended_glob
local NL_CHAR NL_SUM
NL_CHAR=${RBUFFER//[^$'\n']/}
if [[ $RBUFFER == *$'\n'* && $#NL_CHAR -ge $NUMERIC ]]; then
zle .down-line-or-history
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start2=$CURSOR
zle .end-of-line -N
__end2=$CURSOR
zle vi-vlines-highlight
else
return 1
fi
}
zle -N vi-vlines-down-line
# VI Visual Lines up
vi-vlines-up-line()
{
setopt extended_glob
local NL_CHAR NL_SUM
NL_CHAR=${LBUFFER//[^$'\n']/}
if [[ $LBUFFER == *$'\n'* && $#NL_CHAR -ge $NUMERIC ]]; then
zle .up-line-or-history
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start2=$CURSOR
zle .end-of-line -N
__end2=$CURSOR
zle vi-vlines-highlight
else
return 1
fi
}
zle -N vi-vlines-up-line
# Kill highlighted region in VLines
vi-vlines-kill()
{
MARK=$__regend
CURSOR=$__regstart
zle .kill-region
if [[ $__regstart -le 1 ]]; then
zle .kill-whole-line
else
zle .backward-delete-char -N
zle .forward-char -N
fi
}
zle -N vi-vlines-kill
# Kill highlighted region in VLines
vi-vlines-kill-and-vicmd()
{
zle vi-vlines-kill
set-x-clipboard "$CUTBUFFER"
zle vi-visual-exit
}
zle -N vi-vlines-kill-and-vicmd
# Exit Visual Lines Mode and enter VISUAL Mode keeping the region
vi-vlines-exit-to-visual()
{
zle -K vivis
MARK=$__csorig
zle vi-visual-highlight
}
zle -N vi-vlines-exit-to-visual
# Vi VLines Put
vi-vlines-put()
{
MARK=$__regend
CURSOR=$__regstart
zle .kill-region
if [[ $_clipcopy == '+' ]]; then
local cbuf
cbuf="$CUTBUFFER"
get-x-clipboard
zle .vi-put-after
unset _clipcopy
CUTBUFFER="$cbuf"
else
zle -U 2 && zle .vi-set-buffer
zle .vi-put-after
fi
zle vi-visual-exit
}
zle -N vi-vlines-put
##################### zsh vi misc stuff ###############################################################################
#######################################################################################################################
# Vi go to line x
vi-goto-line()
{
setopt extended_glob
local LNL_CHAR RNL_CHAR NL_SUM CUR_LINE
integer n
LNL_CHAR=${LBUFFER//[^$'\n']/}
RNL_CHAR=${RBUFFER//[^$'\n']/}
(( CUR_LINE = $#LNL_CHAR + 1 ))
(( NL_SUM = $#LNL_CHAR + $#RNL_CHAR + 1 ))
if [[ $NUMERIC -gt NL_SUM ]]; then
return 1
fi
if [[ -z $NUMERIC || $NUMERIC == 0 ]]; then
CURSOR=$#BUFFER
zle .vi-first-non-blank
return 0
elif [[ -n $NUMERIC && $CUR_LINE -lt $NUMERIC ]]; then
n=$CUR_LINE
while [[ $n -lt $NUMERIC ]]
do
zle .down-line-or-history -N
(( n++ ))
done
zle .vi-first-non-blank
return 0
elif [[ -n $NUMERIC && $CUR_LINE -gt $NUMERIC ]]; then
n=$CUR_LINE
while [[ $n -gt $NUMERIC ]]
do
zle .up-line-or-history -N
(( n-- ))
done
zle .vi-first-non-blank
return 0
elif [[ -n $NUMERIC && $CUR_LINE -eq $NUMERIC ]]; then
zle .vi-first-non-blank
return 0
fi
}
zle -N vi-goto-line
# Vi go to first line
vi-goto-first-line()
{
CURSOR=0
zle .vi-first-non-blank
}
zle -N vi-goto-first-line
# Vi VISUAL go to line
vi-visual-goto-line()
{
zle vi-goto-line
zle vi-visual-highlight
}
zle -N vi-visual-goto-line
# Vi VISUAL go to first line
vi-visual-goto-first-line()
{
zle vi-goto-first-line
zle vi-visual-highlight
}
zle -N vi-visual-goto-first-line
# Vi Vlines go to line
vi-vlines-goto-line()
{
zle vi-goto-line
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start2=$CURSOR
zle .end-of-line -N
__end2=$CURSOR
zle vi-vlines-highlight
}
zle -N vi-vlines-goto-line
# Vi Vlines go to first line
vi-vlines-goto-first-line()
{
zle vi-goto-first-line
__savepos=$CURSOR
zle .vi-beginning-of-line -N
__start2=$CURSOR
zle .end-of-line -N
__end2=$CURSOR
zle vi-vlines-highlight
}
zle -N vi-vlines-goto-first-line
vi-visual-surround-dquote()
{
zle vi-visual-kill
BUFFER="$LBUFFER\"$CUTBUFFER\"$RBUFFER"
zle vi-visual-exit
}
zle -N vi-visual-surround-dquote
vi-visual-surround-squote()
{
zle vi-visual-kill
BUFFER="$LBUFFER'$CUTBUFFER'$RBUFFER"
zle vi-visual-exit
}
zle -N vi-visual-surround-squote
vi-visual-surround-parenthesis()
{
zle vi-visual-kill
BUFFER="$LBUFFER($CUTBUFFER)$RBUFFER"
zle vi-visual-exit
}
zle -N vi-visual-surround-parenthesis
vi-visual-surround-space()
{
zle vi-visual-kill
BUFFER="$LBUFFER $CUTBUFFER $RBUFFER"
zle vi-visual-exit
}
zle -N vi-visual-surround-space
#bindkey -M vivis '-' vi-visual-up-line
#bindkey -M vivis 'S' vi-visual-substitute-lines
#bindkey -M vivis 'v' vi-visual-exit
#bindkey -M vivli 'v' vi-vlines-exit-to-visual
bindkey -M vivis "\"" vi-set-buffer
bindkey -M vivis '1' digit-argument
bindkey -M vivis '2' digit-argument
bindkey -M vivis '3' digit-argument
bindkey -M vivis '4' digit-argument
bindkey -M vivis '5' digit-argument
bindkey -M vivis '6' digit-argument
bindkey -M vivis '7' digit-argument
bindkey -M vivis '8' digit-argument
bindkey -M vivis '9' digit-argument
bindkey -M vicmd 'G' vi-goto-line
bindkey -M vicmd 'V' vi-vlines-mode
bindkey -M vicmd 'gg' vi-goto-first-line
bindkey -M vicmd 'v' vi-visual-mode
bindkey -M vivis "S'" vi-visual-surround-squote
bindkey -M vivis ' ' vi-visual-forward-char
bindkey -M vivis '%' vi-visual-match-bracket
bindkey -M vivis '+' vi-visual-down-line
bindkey -M vivis ',' vi-visual-rev-repeat-find
bindkey -M vivis '0' vi-visual-bol
bindkey -M vivis ';' vi-visual-repeat-find
bindkey -M vivis 'B' vi-visual-backward-blank-word
bindkey -M vivis 'C' vi-visual-substitute-lines
bindkey -M vivis 'D' vi-visual-kill-and-vicmd
bindkey -M vivis 'E' vi-visual-forward-blank-word-end
bindkey -M vivis 'F' vi-visual-find-prev-char
bindkey -M vivis 'G' vi-visual-goto-line
bindkey -M vivis 'I' vi-visual-insert-bol
bindkey -M vivis 'J' vi-visual-join
bindkey -M vivis 'O' vi-visual-exchange-points
bindkey -M vivis 'R' vi-visual-substitute-lines
bindkey -M vivis 'S ' vi-visual-surround-space
bindkey -M vivis 'S"' vi-visual-surround-dquote
bindkey -M vivis 'S(' vi-visual-surround-parenthesis
bindkey -M vivis 'S)' vi-visual-surround-parenthesis
bindkey -M vivis 'T' vi-visual-find-prev-char-skip
bindkey -M vivis 'U' vi-visual-uppercase-region
bindkey -M vivis 'V' vi-visual-exit-to-vlines
bindkey -M vivis 'W' vi-visual-forward-blank-word
bindkey -M vivis 'Y' vi-visual-yank
bindkey -M vivis '\$' vi-visual-eol
bindkey -M vivis '\^' vi-visual-first-non-blank
bindkey -M vivis '\|' vi-visual-goto-column
bindkey -M vivis '^?' vi-visual-backward-char
bindkey -M vivis '^M' vi-visual-yank
bindkey -M vivis '^[' vi-visual-exit
bindkey -M vivis 'b' vi-visual-backward-word
bindkey -M vivis 'c' vi-visual-change
bindkey -M vivis 'd' vi-visual-kill-and-vicmd
bindkey -M vivis 'e' vi-visual-forward-word-end
bindkey -M vivis 'f' vi-visual-find-next-char
bindkey -M vivis 'gg' vi-visual-goto-first-line
bindkey -M vivis 'h' vi-visual-backward-char
bindkey -M vivis 'j' vi-visual-down-line
bindkey -M vivis 'jj' vi-visual-exit
bindkey -M vivis 'k' vi-visual-up-line
bindkey -M vivis 'l' vi-visual-forward-char
bindkey -M vivis 'o' vi-visual-exchange-points
bindkey -M vivis 'p' vi-visual-put
bindkey -M vivis 'r' vi-visual-replace-region
bindkey -M vivis 't' vi-visual-find-next-char-skip
bindkey -M vivis 'u' vi-visual-lowercase-region
bindkey -M vivis 'v' vi-visual-eol
bindkey -M vivis 'w' vi-visual-forward-word
bindkey -M vivis 'y' vi-visual-yank
bindkey -M vivis 'X' vi-visual-kill-and-vicmd
bindkey -M vivis 'x' vi-visual-kill-and-vicmd
bindkey -M vivli 'D' vi-vlines-kill-and-vicmd
bindkey -M vivli 'G' vi-vlines-goto-line
bindkey -M vivli 'O' vi-vlines-exchange-points
bindkey -M vivli 'P' vi-vlines-put
bindkey -M vivli 'd' vi-vlines-kill-and-vicmd
bindkey -M vivli 'gg' vi-vlines-goto-first-line
bindkey -M vivli 'j' vi-vlines-down-line
bindkey -M vivli 'k' vi-vlines-up-line
bindkey -M vivli 'o' vi-vlines-exchange-points
bindkey -M vivli 'p' vi-vlines-put
bindkey -M vivli 'v' vi-vlines-exit-to-visual