-
Notifications
You must be signed in to change notification settings - Fork 3
/
completion
386 lines (337 loc) · 10.9 KB
/
completion
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
#!/usr/bin/env bash
# shellcheck disable=SC2034
give_me_hostname="$HOME/.dotfiles/bin/give_me_hostname"
fzf_default_options="-i --select-1 --exit-0 --tac --tiebreak=length,begin,index --reverse --inline-info"
fzf_height="--height ${FZF_TMUX_HEIGHT:-50%} --min-height 25"
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
export FZF_COMPLETION_TRIGGER='**'
# Tools to complete
net_tools=(
ping
host
mtr
mosh
telnet
ftp
lftp
dig
drill
nslookup
ansible
nmap
)
ssh_tools=(
ssh
s
n
rsync
scp
sftp
proo
curl
ssh-audit
update-services
connect_to_proo
mysql_get_root_password
)
# Use fd (https://github.com/sharkdp/fd) instead of the default find
# command for listing path candidates.
# - The first argument to the function ($1) is the base path to start traversal
# - See the source code (completion.{bash,zsh}) for the details.
_fzf_compgen_path() {
fd --max-depth 5 --follow --exclude ".git" . "$1"
}
# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
fd --type d --max-depth 3 --follow --exclude ".git" . "$1"
}
# To redraw line after fzf closes (printf '\e[5n')
bind '"\e[0n": redraw-current-line'
# fzf default completion
#[[ -f /usr/share/fzf/completion.bash ]] && source /usr/share/fzf/completion.bash
_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
if [[ ${cur} == *@* ]] ; then
local opts="-P ${cur/@*/}@ -- ${cur/*@/}"
else
local opts=" -- ${cur}"
fi
# shellcheck disable=SC2086
COMPREPLY=( $(compgen -W "$(</dev/stdin)" ${opts}) )
return 0
}
_fzf_privkey() {
find "$HOME/.ssh" -type f -name 'id_*' | grep -v '.pub'
}
_fzf_playbook() {
find playbooks -maxdepth 1 -type f -name '*.yml'
}
_fzf_ansible_groups() {
local inventory
inventory=$(awk '/inventory/ {print $3}' "$HOME"/.ansible.cfg)
inventory=${inventory/\~/$HOME}
if [ -d "$inventory" ]; then
awk '/\[/' "${inventory}"* | tr -d '[]'
else
awk '/\[/' "$inventory" | tr -d '[]'
fi
}
_fzf_dsh_groups() {
local inventory
inventory="$HOME/.dsh/group"
if [ -d "$inventory" ]; then
ls -1 "$inventory"
fi
}
_fzf_archlinux_installed_packages() {
pacman -Qq
}
_fzf_log() {
local cmd log_file
cmd="command find /var/log/ -type f -name '*log' 2>/dev/null"
log_file=$(eval "$cmd" | fzf "$fzf_height" --tac --tiebreak=length,begin,index --reverse --inline-info) && less "$log_file"
}
_fzf_complete() {
local selected fzf post caller
local current="${COMP_WORDS[COMP_CWORD]}"
local user_prefix=''
local options=${1:-'--multi'}
local separator=${2:- }
local preview="fzf_preview {}"
caller=$(caller 0 | awk '{print $2}')
local delete="fzf_delete $caller {}"
post="${caller}_post"
type -t "$post" >/dev/null 2>&1 || post=cat
fzf="fzf $fzf_default_options $options $fzf_height --preview-window right:75%:hidden --bind ?:toggle-preview"
if [[ ${current} == *@* ]] ; then
user_prefix="${current/@*/}@"
current="${current/*@/}"
fi
selected=$(cat | $fzf --preview "$preview" --bind "alt-bspace:execute:$delete" --query "$current" | $post | tr '\n' "$separator")
selected=${selected%$separator} # Strip trailing space not to repeat "-o nospace"
printf '\e[5n'
if [ -n "$selected" ]; then
COMPREPLY=("${user_prefix}${selected}")
return 0
fi
}
__fzf_history__() {
local line
shopt -u nocaseglob nocasematch
line=$(
# shellcheck disable=SC1007
HISTTIMEFORMAT= history |
FZF_DEFAULT_OPTS="$fzf_height $fzf_default_options --tac -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort $FZF_CTRL_R_OPTS +m" fzf |
command grep '^ *[0-9]') &&
if [[ $- =~ H ]]; then
sed 's/^ *\([0-9]*\)\** .*/!\1/' <<< "$line"
else
sed 's/^ *\([0-9]*\)\** *//' <<< "$line"
fi
}
_fzf_complete_kill() {
[ -n "${COMP_WORDS[COMP_CWORD]}" ] && return 1
local selected fzf
fzf="fzf"
selected=$(ps -ef | sed 1d | FZF_DEFAULT_OPTS="$fzf_height \
--reverse $FZF_DEFAULT_OPTS --preview 'echo {}' --preview-window down:3:wrap \
$FZF_COMPLETION_OPTS" $fzf --multi | awk '{print $2}' | tr '\n' ' ')
printf '\e[5n'
if [ -n "$selected" ]; then
COMPREPLY=( "$selected" )
return 0
fi
}
__fzf_select__() {
local cmd="${FZF_CTRL_T_COMMAND:-"command find -L . -mindepth 1 \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2> /dev/null | cut -b3-"}"
eval "$cmd" | FZF_DEFAULT_OPTS="$fzf_height --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS --preview='fzf_preview {}'" fzf -m "$@" | while read -r item; do
printf '%q ' "$item"
done
echo
}
_fzf_file_widget() {
local selected
selected="$(__fzf_select__ "$@")"
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}"
READLINE_POINT=$(( READLINE_POINT + ${#selected} ))
}
__fzf_cd__() {
local cmd dir
cmd="${FZF_ALT_C_COMMAND:-"command find -L . \\( -path '*/\\.*' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune \
-o -type d -print 2> /dev/null | sed 1d | cut -b3-"}"
dir=$(eval "$cmd" | FZF_DEFAULT_OPTS="$fzf_height --reverse $FZF_DEFAULT_OPTS $FZF_ALT_C_OPTS" fzf +m) && printf 'cd %q' "$dir"
}
# v - open files in ~/.viminfo
v() {
local files
files=$(grep '^>' ~/.viminfo | cut -c3- |
while read -r line; do
[ -f "${line/\~/$HOME}" ] && echo "${line/\~/$HOME}"
done | fzf-tmux --select-1 --reverse --inline-info +s \
--tac --multi --preview 'highlight --force -O ansi -l {} 2> /dev/null | head -200' --query "$*" -1) \
&& vim "${files//\~/$HOME}"
}
fmpc() {
local song_position
song_position=$(mpc -f "%position%) %artist% - %title%" playlist | \
fzf-tmux --query="$1" "$fzf_height" --reverse --inline-info +s --select-1 --exit-0 | \
sed -ne 's/^\([0-9].*\)).*/\1/p') || return 1
[ -n "$song_position" ] && mpc -q play "$song_position"
}
# c - browse chrome history
ch() {
local cols sep google_history open
cols=$(( COLUMNS / 3 ))
sep='{::}'
if [ "$(uname)" = "Darwin" ]; then
google_history="$HOME/Library/Application Support/Google/Chrome/Default/History"
open=open
else
google_history="$HOME/.config/google-chrome/Profile 1/History"
open=xdg-open
fi
cp -f "$google_history" /tmp/h
# shellcheck disable=2086
sqlite3 -separator $sep /tmp/h \
"select substr(title, 1, $cols), url
from urls order by last_visit_time desc" |
awk -F $sep '{printf "%-'$cols's \x1b[36m%s\x1b[m\n", $1, $2}' |
fzf $fzf_default_options $fzf_height --ansi --multi | sed 's#.*\(https*://\)#\1#' | xargs $open > /dev/null 2> /dev/null
}
# fco - checkout git branch/tag
fco() {
local tags branches target
tags=$(
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
# shellcheck disable=2086
target=$(
(echo "$tags"; echo "$branches") |
fzf-tmux $fzf_default_options $fzf_height -l30 -- --no-hscroll --ansi +m -d "\t" -n 2) || return
git checkout "$(echo "$target" | awk '{print $2}')"
}
# fshow - git commit browser
fshow() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
# fcs - get git commit sha
# example usage: git rebase -i `fcs`
fcs() {
local commits commit
commits=$(git log --color=always --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e --ansi --reverse) &&
echo -n "$(echo "$commit" | sed "s/ .*//")"
}
_complete_ssh() {
_complete "$@" < <(give_me_hostname ssh)
}
_complete_net() {
_complete "$@" < <(give_me_hostname)
}
_fzf_complete_ssh() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
case $prev in
-i)
_fzf_complete '--no-multi' ' ' '' "$@" < <(_fzf_privkey)
return 0
;;
esac
_fzf_complete '--no-multi' ' ' '' "$@" < <(give_me_hostname ssh)
}
_fzf_complete_ansible() {
local prev="${COMP_WORDS[COMP_CWORD-1]}"
case $prev in
-i)
_fzf_complete '--multi' ',' "$@" < <(give_me_hostname ssh)
return 0
;;
-l)
_fzf_complete '--multi' ',' "$@" < <(_fzf_ansible_groups)
return 0
;;
-u)
return 0
;;
esac
_fzf_complete '--multi' ' ' "$@" < <(_fzf_playbook)
}
_fzf_complete_dsh() {
local prev="${COMP_WORDS[COMP_CWORD-1]}"
case $prev in
-g)
_fzf_complete '--multi' ',' "$@" < <(_fzf_dsh_groups)
return 0
;;
esac
}
_fzf_complete_pacman() {
local prev="${COMP_WORDS[COMP_CWORD-1]}"
case $prev in
-Rcs|-Qo|-Qi|-Ql|-Sii)
_fzf_complete '--multi' ' ' "$@" < <(_fzf_archlinux_installed_packages)
return 0
;;
esac
}
_fzf_complete_archlinux_installed_packages() {
_fzf_complete '--multi' ' ' "$@" < <(_fzf_archlinux_installed_packages)
}
_fzf_complete_pssh() {
local prev="${COMP_WORDS[COMP_CWORD-1]}"
case $prev in
--hosts|-h)
_fzf_complete '--no-multi' ' ' "$@" < <(_fzf_ansible_groups)
return 0
;;
esac
}
_fzf_complete_pssh_post() {
# shellcheck disable=2088
echo "~/.dsh/group/$(cat -)"
}
_fzf_complete_net() {
_fzf_complete '--no-multi' ' ' "$@" < <(give_me_hostname)
}
_fzf_complete_ips() {
_fzf_complete '--no-multi' ' ' "$@" < <(give_me_hostname ips)
}
if hash fzf 2>/dev/null; then
complete -o default -o bashdefault -o nospace -F _fzf_complete_net "${net_tools[@]}"
complete -o default -o bashdefault -o nospace -F _fzf_complete_ssh "${ssh_tools[@]}"
complete -o default -o bashdefault -o nospace -F _fzf_complete_ips whois
complete -o default -o bashdefault -o nospace -F _fzf_complete_ansible ansible-playbook ap
complete -o default -o bashdefault -o nospace -F _fzf_complete_pacman yaourt pacman yay
complete -o default -o bashdefault -o nospace -F _fzf_complete_archlinux_installed_packages pactree
complete -o default -o bashdefault -o nospace -F _fzf_complete_dsh dsh
complete -o default -o bashdefault -o nospace -F _fzf_complete_pssh pssh psshscp
else
complete -o default -o bashdefault -o nospace -F _complete_net "${net_tools[@]}"
complete -o default -o bashdefault -o nospace -F _complete_ssh "${ssh_tools[@]}"
fi
# Kill completion
complete -F _fzf_complete_kill -o nospace -o default -o bashdefault kill
# Required to refresh the prompt after fzf
bind '"\er": redraw-current-line'
bind '"\e^": history-expand-line'
# CTRL-R - Paste the selected command from history into the command line
# shellcheck disable=SC2016
bind '"\C-r": " \C-e\C-u`__fzf_history__`\e\C-e\e^\er"'
# CTRL-f - Paste the selected file path into the command line
bind -x '"\C-f": "_fzf_file_widget"'
# ALT-S - cd into the selected directory
# shellcheck disable=SC2016
bind '"\es": " \C-e\C-u`__fzf_cd__`\e\C-e\er\C-m"'