aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/zsh/keybindings.zsh
blob: 31b17fda0f52fee976b36fbd21716f6183c25c77 (plain)
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
#!/usr/bin/env zsh

r-delregion() {
  if ((REGION_ACTIVE)) then
    zle kill-region
  else 
    local widget_name=$1
    shift
    zle $widget_name -- $@
  fi
}

r-deselect() {
  ((REGION_ACTIVE = 0))
  local widget_name=$1
  shift
  zle $widget_name -- $@
}

r-select() {
  ((REGION_ACTIVE)) || zle set-mark-command
  local widget_name=$1
  shift
  zle $widget_name -- $@
}

r-select-a() {
  r-deselect beginning-of-line
  r-select end-of-line
}

r-undo() {
  zle undo
}

r-copy() {
  if ((REGION_ACTIVE)) then
    zle copy-region-as-kill
    printf "$CUTBUFFER" | xclip -i
  else
    zle kill-whole-line
  fi
}

r-cut() {
  if ((REGION_ACTIVE)) then
    zle kill-region
  else
    zle kill-whole-line
  fi
  printf "$CUTBUFFER" | xclip -i
}

for key     kcap   seq        mode   widget (
    sleft   kLFT   $'\e[1;2D' select   backward-char
    sright  kRIT   $'\e[1;2C' select   forward-char
    sup     kri    $'\e[1;2A' select   up-line-or-history
    sdown   kind   $'\e[1;2B' select   down-line-or-history

    send    kEND   $'\E[1;2F' select   end-of-line
    send2   x      $'\E[4;2~' select   end-of-line

    shome   kHOM   $'\E[1;2H' select   beginning-of-line
    shome2  x      $'\E[1;2~' select   beginning-of-line

    left    kcub1  $'\EOD'    deselect backward-char
    right   kcuf1  $'\EOC'    deselect forward-char

    end     kend   $'\EOF'    deselect end-of-line
    end2    x      $'\E4~'    deselect end-of-line

    home    khome  $'\EOH'    deselect beginning-of-line
    home2   x      $'\E1~'    deselect beginning-of-line

    csleft  x      $'\E[1;6D' select   backward-word
    csright x      $'\E[1;6C' select   forward-word
    csend   x      $'\E[1;6F' select   end-of-line
    cshome  x      $'\E[1;6H' select   beginning-of-line

    cleft   x      $'\E[1;5D' deselect backward-word
    cright  x      $'\E[1;5C' deselect forward-word

    del     kdch1   $'\E[3~'  delregion delete-char
    bs      x       $'^?'     delregion backward-delete-char
    
    cz      x       "^Z"      undo      ""
    cc      x       "^C"      copy      ""
    cx      x       "^X"      cut       ""
    ca      x       "^A"      select-a  "" 
  ) {
  eval "key-$key() {
    r-$mode $widget \$@
  }"
  zle -N key-$key
  bindkey ${terminfo[$kcap]-$seq} key-$key
}

bindkey '^ ' autosuggest-accept
bindkey '^[[Z' reverse-menu-complete

# register ctrl+c as interupt 
function ctrl_c_intr() {
  stty intr \^C <$TTY >$TTY 
}

# register ctrl+y as interupt 
function ctrl_y_intr() {
  stty intr \^Y <$TTY >$TTY
}

# make sure we can register hooks
autoload -Uz add-zsh-hook || return

# register the functions as hooks
add-zsh-hook preexec ctrl_c_intr
add-zsh-hook precmd ctrl_y_intr