diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2023-03-20 23:17:52 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2023-03-20 23:17:52 +0800 |
commit | 62d0ac148a5dbf32f3069010d02768664898458e (patch) | |
tree | 5fea846f3d74593de7079d8feb8982ac3db5b306 /zsh/key-bindings.zsh | |
parent | a5e1492da2997072eda4aa53e4acb5d2c287426d (diff) |
Update
Diffstat (limited to 'zsh/key-bindings.zsh')
-rw-r--r-- | zsh/key-bindings.zsh | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/zsh/key-bindings.zsh b/zsh/key-bindings.zsh new file mode 100644 index 0000000..ab77e52 --- /dev/null +++ b/zsh/key-bindings.zsh | |||
@@ -0,0 +1,123 @@ | |||
1 | # ____ ____ | ||
2 | # / __/___ / __/ | ||
3 | # / /_/_ / / /_ | ||
4 | # / __/ / /_/ __/ | ||
5 | # /_/ /___/_/ key-bindings.zsh | ||
6 | # | ||
7 | # - $FZF_TMUX_OPTS | ||
8 | # - $FZF_CTRL_T_COMMAND | ||
9 | # - $FZF_CTRL_T_OPTS | ||
10 | # - $FZF_CTRL_R_OPTS | ||
11 | # - $FZF_ALT_C_COMMAND | ||
12 | # - $FZF_ALT_C_OPTS | ||
13 | |||
14 | # Key bindings | ||
15 | # ------------ | ||
16 | |||
17 | # The code at the top and the bottom of this file is the same as in completion.zsh. | ||
18 | # Refer to that file for explanation. | ||
19 | if 'zmodload' 'zsh/parameter' 2>'/dev/null' && (( ${+options} )); then | ||
20 | __fzf_key_bindings_options="options=(${(j: :)${(kv)options[@]}})" | ||
21 | else | ||
22 | () { | ||
23 | __fzf_key_bindings_options="setopt" | ||
24 | 'local' '__fzf_opt' | ||
25 | for __fzf_opt in "${(@)${(@f)$(set -o)}%% *}"; do | ||
26 | if [[ -o "$__fzf_opt" ]]; then | ||
27 | __fzf_key_bindings_options+=" -o $__fzf_opt" | ||
28 | else | ||
29 | __fzf_key_bindings_options+=" +o $__fzf_opt" | ||
30 | fi | ||
31 | done | ||
32 | } | ||
33 | fi | ||
34 | |||
35 | 'emulate' 'zsh' '-o' 'no_aliases' | ||
36 | |||
37 | { | ||
38 | |||
39 | [[ -o interactive ]] || return 0 | ||
40 | |||
41 | # CTRL-T - Paste the selected file path(s) into the command line | ||
42 | __fsel() { | ||
43 | local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ | ||
44 | -o -type f -print \ | ||
45 | -o -type d -print \ | ||
46 | -o -type l -print 2> /dev/null | cut -b3-"}" | ||
47 | setopt localoptions pipefail no_aliases 2> /dev/null | ||
48 | local item | ||
49 | eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" $(__fzfcmd) -m "$@" | while read item; do | ||
50 | echo -n "${(q)item} " | ||
51 | done | ||
52 | local ret=$? | ||
53 | echo | ||
54 | return $ret | ||
55 | } | ||
56 | |||
57 | __fzfcmd() { | ||
58 | [ -n "$TMUX_PANE" ] && { [ "${FZF_TMUX:-0}" != 0 ] || [ -n "$FZF_TMUX_OPTS" ]; } && | ||
59 | echo "fzf-tmux ${FZF_TMUX_OPTS:--d${FZF_TMUX_HEIGHT:-40%}} -- " || echo "fzf" | ||
60 | } | ||
61 | |||
62 | fzf-file-widget() { | ||
63 | LBUFFER="${LBUFFER}$(__fsel)" | ||
64 | local ret=$? | ||
65 | zle reset-prompt | ||
66 | return $ret | ||
67 | } | ||
68 | zle -N fzf-file-widget | ||
69 | bindkey -M emacs '^T' fzf-file-widget | ||
70 | bindkey -M vicmd '^T' fzf-file-widget | ||
71 | bindkey -M viins '^T' fzf-file-widget | ||
72 | |||
73 | # ALT-C - cd into the selected directory | ||
74 | fzf-cd-widget() { | ||
75 | local cmd="${FZF_ALT_C_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \ | ||
76 | -o -type d -print 2> /dev/null | cut -b3-"}" | ||
77 | setopt localoptions pipefail no_aliases 2> /dev/null | ||
78 | local dir="$(eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse --bind=ctrl-z:ignore $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" $(__fzfcmd) +m)" | ||
79 | if [[ -z "$dir" ]]; then | ||
80 | zle redisplay | ||
81 | return 0 | ||
82 | fi | ||
83 | zle push-line # Clear buffer. Auto-restored on next prompt. | ||
84 | BUFFER="builtin cd -- ${(q)dir}" | ||
85 | zle accept-line | ||
86 | local ret=$? | ||
87 | unset dir # ensure this doesn't end up appearing in prompt expansion | ||
88 | zle reset-prompt | ||
89 | return $ret | ||
90 | } | ||
91 | zle -N fzf-cd-widget | ||
92 | bindkey -M emacs '\ec' fzf-cd-widget | ||
93 | bindkey -M vicmd '\ec' fzf-cd-widget | ||
94 | bindkey -M viins '\ec' fzf-cd-widget | ||
95 | |||
96 | # CTRL-R - Paste the selected command from history into the command line | ||
97 | fzf-history-widget() { | ||
98 | local selected num | ||
99 | setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null | ||
100 | selected=( $(fc -rl 1 | awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' | | ||
101 | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) ) | ||
102 | local ret=$? | ||
103 | if [ -n "$selected" ]; then | ||
104 | num=$selected[1] | ||
105 | zle insert-last-word -- $selected | ||
106 | if [[ "$num" =~ '^[0-9]+$' ]]; then | ||
107 | zle vi-fetch-history -n $num | ||
108 | else | ||
109 | BUFFER="$selected " && zle end-of-line | ||
110 | fi | ||
111 | fi | ||
112 | zle reset-prompt | ||
113 | return $ret | ||
114 | } | ||
115 | zle -N fzf-history-widget | ||
116 | bindkey -M emacs '^R' fzf-history-widget | ||
117 | bindkey -M vicmd '^R' fzf-history-widget | ||
118 | bindkey -M viins '^R' fzf-history-widget | ||
119 | |||
120 | } always { | ||
121 | eval $__fzf_key_bindings_options | ||
122 | 'unset' '__fzf_key_bindings_options' | ||
123 | } | ||