Enable tracing of the initialization
[zsh.git] / .zsh / 55_KeyBindings.zsh
1 # -*- mode: sh;-*-
2 ###
3 # Key Bindings
4 zmodload zsh/terminfo
5 typeset -gA key_info
6 key_info=(
7 'Control' '\C-'
8 'Escape' '\e'
9 'Meta' '\M-'
10 'Backspace' "^?"
11 'Delete' "^[[3~"
12 'F1' "$terminfo[kf1]"
13 'F2' "$terminfo[kf2]"
14 'F3' "$terminfo[kf3]"
15 'F4' "$terminfo[kf4]"
16 'F5' "$terminfo[kf5]"
17 'F6' "$terminfo[kf6]"
18 'F7' "$terminfo[kf7]"
19 'F8' "$terminfo[kf8]"
20 'F9' "$terminfo[kf9]"
21 'F10' "$terminfo[kf10]"
22 'F11' "$terminfo[kf11]"
23 'F12' "$terminfo[kf12]"
24 'Insert' "$terminfo[kich1]"
25 'Home' "$terminfo[khome]"
26 'PageUp' "$terminfo[kpp]"
27 'End' "$terminfo[kend]"
28 'PageDown' "$terminfo[knp]"
29 'Up' "$terminfo[kcuu1]"
30 'Left' "$terminfo[kcub1]"
31 'Down' "$terminfo[kcud1]"
32 'Right' "$terminfo[kcuf1]"
33 'BackTab' "$terminfo[kcbt]"
34 )
35 # Set empty $key_info values to an invalid UTF-8 sequence to induce silent
36 # bindkey failure.
37 for key in "${(k)key_info[@]}"; do
38 if [[ -z "$key_info[$key]" ]]; then
39 key_info["$key"]='�'
40 fi
41 done
42
43 # First set it back to known defaults
44 bindkey -d
45
46 # now, our main map is the emacs style
47 bindkey -e
48
49 bindkey "$key_info[Home]" beginning-of-line
50 bindkey "$key_info[End]" end-of-line
51 bindkey "$key_info[Insert]" overwrite-mode
52 bindkey "$key_info[Delete]" delete-char
53 bindkey "$key_info[Backspace]" backward-delete-char
54 bindkey "$key_info[Left]" backward-char
55 bindkey "$key_info[Right]" forward-char
56
57 # Setup some more bindings to be more like emacs.
58 for key ("$key_info[Escape]"{B,b}) bindkey -M emacs "$key" emacs-backward-word
59 for key ("$key_info[Escape]"{F,f}) bindkey -M emacs "$key" emacs-forward-word
60 bindkey -M emacs "$key_info[Escape]$key_info[Left]" emacs-backward-word
61 bindkey -M emacs "$key_info[Escape]$key_info[Right]" emacs-forward-word
62
63 # Kill to the beginning of the line.
64 for key in "$key_info[Escape]"{K,k} bindkey -M emacs "$key" backward-kill-line
65
66 # Redo.
67 bindkey -M emacs "$key_info[Escape]_" redo
68
69 # "suspend" current line
70 bindkey "$key_info[Control]Z" push-input
71
72 # Bind Shift + Tab to go to the previous menu item.
73 bindkey "$key_info[BackTab]" reverse-menu-complete
74
75 #k# Insert a timestamp on the command line (yyyy-mm-dd)
76 zle -N insert-datestamp
77 bindkey "$key_info[Control]Ed" insert-datestamp
78
79 #k# Put the current command line into a \kbd{sudo} call
80 zle -N sudo-command-line
81 bindkey "$key_info[Control]Os" sudo-command-line
82
83 ## This function allows you type a file pattern,
84 ## and see the results of the expansion at each step.
85 ## When you hit return, they will be inserted into the command line.
86 if is4 && zrcautoload insert-files && zle -N insert-files; then
87 #k# Insert files and test globbing
88 bindkey "$key_info[Control]Xf" insert-files ## C-x-f
89 fi
90
91 ## This set of functions implements a sort of magic history searching.
92 ## After predict-on, typing characters causes the editor to look backward
93 ## in the history for the first line beginning with what you have typed so
94 ## far. After predict-off, editing returns to normal for the line found.
95 ## In fact, you often don't even need to use predict-off, because if the
96 ## line doesn't match something in the history, adding a key performs
97 ## standard completion - though editing in the middle is liable to delete
98 ## the rest of the line.
99 if is4 && zrcautoload predict-on && zle -N predict-on; then
100 #zle -N predict-off
101 bindkey "$key_info[Control]X$key_info[Control]R" predict-on ## C-x C-r
102 #bindkey "^U" predict-off ## C-u
103 fi
104
105 # in 'foo bar | baz' make a second ^W not eat 'bar |', but only '|'
106 # this has the disadvantage that in 'bar|baz' it eats all of it.
107 typeset WORDCHARS='|'$WORDCHARS
108
109 # press ctrl-x ctrl-e for editing command line in $EDITOR or $VISUAL
110 if is4 && zrcautoload edit-command-line && zle -N edit-command-line; then
111 bindkey '$key_info[Control]x$key_info[Control]e' edit-command-line
112 fi
113
114 # move cursor between chars when typing '', "", (), [], and {}
115 magic-single-quotes() { if [[ $LBUFFER[-1] == \' ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
116 magic-double-quotes() { if [[ $LBUFFER[-1] == \" ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
117 magic-parentheses() { if [[ $LBUFFER[-1] == \( ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
118 magic-square-brackets() { if [[ $LBUFFER[-1] == \[ ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
119 magic-curly-brackets() { if [[ $LBUFFER[-1] == \{ ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
120 magic-angle-brackets() { if [[ $LBUFFER[-1] == \< ]]; then zle self-insert; zle .backward-char; else zle self-insert; fi };
121 bindkey \' magic-single-quotes
122 bindkey \" magic-double-quotes
123 bindkey \) magic-parentheses
124 bindkey \] magic-square-brackets
125 bindkey \} magic-curly-brackets
126 bindkey \> magic-angle-brackets
127 zle -N magic-single-quotes
128 zle -N magic-double-quotes
129 zle -N magic-parentheses
130 zle -N magic-square-brackets
131 zle -N magic-curly-brackets
132 zle -N magic-angle-brackets
133
134 # Show what the completion system is trying to complete with at a given point
135 bindkey '$key_info[Control]Xh' _complete_help
136
137 bindkey " " magic-space