-
Notifications
You must be signed in to change notification settings - Fork 1
/
fzf-tools.zsh
242 lines (212 loc) · 8.99 KB
/
fzf-tools.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
#########################################################################
#///////////////////////////////////////////////////////////////////////#
#////// ///////#
#////// Name: FZF-Tools ///////#
#////// File: fzf-tools.zsh ///////#
#////// Author: happycod3r ///////#
#////// Use: Integrates FZF into the command line and much more. ///////#
#////// Check out the README.md for all of the documentation. ///////#
#////// ///////#
#///////////////////////////////////////////////////////////////////////#
#########################################################################
function fzf-command-widget() {
local full_command=$BUFFER
case "$full_command" in
ls*)
BUFFER="$full_command | fzf --multi --cycle --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333"
;;
man*)
BUFFER="fzf-man $full_command"
;;
printenv* | env*)
BUFFER="$full_command | fzf --multi --cycle --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333"
;;
set)
BUFFER="$full_command | fzf --multi --cycle --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333"
;;
grep*)
BUFFER="$full_command | fzf -i --multi --cycle --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333"
;;
find*)
BUFFER="$full_command | fzf -i --multi --cycle --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333"
;;
'ps aux')
BUFFER="$full_command | fzf --multi --cycle --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333"
;;
esac
zle accept-line
# Uncomment if the long command left over on the previous prompt bothers you.
# $(clear)
}
zle -N fzf-command-widget
bindkey '^M' fzf-command-widget
function fzf-man() {
local selected_command
selected_command=$(man -k . | awk '{print $1}' | sort | uniq | fzf --multi --cycle --preview='echo {}' --preview-window down:10%
)
if [[ -n "$selected_command" ]]; then
man "$selected_command" | fzf --multi --cycle --tac --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333
fi
}
# I want to add --multi and try to select multiple commands
# and concatenate them together.
function fzf-run-cmd-from-history() {
local selected_command
selected_command=$(history | awk '{$1=""; print $0}' | awk '!x[$0]++' | fzf --cycle --tac --no-sort --preview 'echo {}' --preview-window down:10% --color bg:#222222,preview-bg:#333333)
if [[ -n "$selected_command" ]]; then
eval "$selected_command"
fi
}
alias fzhist='fzf-run-cmd-from-history'
function fzf-exec-scripts() {
local directory="$1"
shift
local file_exts=("$@")
if [[ -z "$directory" || "${#file_exts[@]}" -eq 0 ]]; then
echo "Usage: fzf-exec-scripts <directory> <file_extension1> [<file_extension2> ...]"
return 1
fi
local selected_scripts=()
local selected_script
selected_script=$(find "$directory" -type f \( -name "*.${file_exts[1]}" $(printf -- "-o -name '*.%s' " "${file_exts[@]:1}") \) | fzf --multi --cycle --tac --no-sort --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333) && selected_scripts=("${(f)selected_script}")
if [[ "${#selected_scripts[@]}" -eq 0 ]]; then
echo "No scripts selected."
return
fi
for script in "${selected_scripts[@]}"; do
chmod +x "$script"
case "$script" in
*.sh)
bash "$script"
;;
*.zsh)
zsh "$script"
;;
*.bash)
bash "$script"
;;
*.js)
node "$script"
;;
*.py)
python "$script"
;;
*.rb)
ruby "$script"
;;
*.rs)
filename=$(basename "${directory}/${script}")
rustc "$script"
./$filename
;;
*)
filename=$(basename "${directory}/${script}")
extension="${filename##*.}"
if [[ "$extension" == "zsh" ]]; then
cat >&2 <<'EOF'
ZSH isn't installed on your system yet.
Check out https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH for more information on how to install it.
EOF
elif [[ "$extension" == "js" ]]; then
cat >&2 <<'EOF'
Node isn't installed on your system yet.
You can install it directly using the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Or check out https://nodejs.dev/en/ for more information.
EOF
elif [[ "$extension" == "py" ]]; then
cat >&2 <<'EOF'
Python isn't installed on your system yet.
Check out https://www.python.org/downloads/ for more information on how to install it.
EOF
elif [[ "$extension" == "rb" ]]; then
cat >&2 <<'EOF'
Ruby isn't installed on your system yet.
Check out https://www.ruby-lang.org/en/documentation/installation/ for more information on how to install it.
EOF
elif [[ "$extension" == "rs" ]]; then
cat >&2 <<'EOF'
Rust isn't installed on your system yet.
You can:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
or check out https://www.rust-lang.org/tools/install for more information.
EOF
return 1
fi
;;
esac
done
}
alias fzscripts='fzf-exec-scripts'
function fzf-search-files-on-path() {
local _path="$1"
find tree "$_path" -type f | fzf -i --multi --cycle --preview='echo {}' --preview-window down:10% --color bg:#222222,preview-bg:#333333
}
alias fzfop='fzf-search-files-on-path'
function fzf-git-log() {
local selected_commit
selected_commit=$(\
git log --oneline | fzf --multi --no-sort --cycle \
--preview='echo {}' \
--preview-window down:10% \
--layout='reverse-list' \
--color bg:#222222,preview-bg:#333333 \
) && git show "$selected_commit"
}
alias fzgl='fzf-git-log'
function fzf-ag() {
local selected_file
selected_file=$(\
ag "$1" . | fzf \
--multi --no-sort --cycle \
--preview='echo {}' \
--preview-window down:10% \
--layout='reverse-list' \
--color bg:#222222,preview-bg:#333333 \
) && $EDITOR "$selected_file"
}
alias fzag='fzf-ag'
function fzf-docker-ps() {
local selected_container
selected_container=$(docker ps -a | fzf --multi --no-sort --cycle --preview='echo {}' --preview-window down:10% --layout='reverse-list' --color bg:#222222,preview-bg:#333333 | awk '{print $1}') && docker logs "$selected_container"
}
alias fzdps='fzf-docker-ps'
function fzf-ssh() {
local selected_host
selected_host=$(\
cat ~/.ssh/known_hosts \
| cut -f 1 -d ' ' \
| sed -e s/,.*//g | uniq | fzf --multi --no-sort --cycle \
--preview='echo {}' \
--preview-window down:10% \
--layout='reverse-list' \
--color bg:#222222,preview-bg:#333333\
) && ssh "$selected_host"
}
alias fzssh='fzf-ssh'
function fzf-grep() {
local selected_file
selected_file=$(grep -Ril "$1" . | fzf --multi --no-sort --cycle \
--preview='echo {}' \
--preview-window down:10% \
--layout='reverse-list' \
--color bg:#222222,preview-bg:#333333\
) && $EDITOR "$selected_file"
}
alias fzgrep='fzf-grep'
function fzf-find() {
local selected_file
selected_file=$(find . -type f | fzf --multi --no-sort --cycle \
--preview='echo {}' \
--preview-window down:10% \
--layout='reverse-list' \
--color bg:#222222,preview-bg:#333333\
) && $EDITOR "$selected_file"
}
alias fzfind='fzf-find'
autoload -Uz fzf-command-widget fzf-man fzf-run-cmd-from-history fzf-exec-scripts fzf-search-files-on-path fzf-git-log fzf-ag fzf-docker-ps fzf-ssh fzf-grep fzf-find
# Initialize fzf
if [[ -x "$(command -v fzf)" ]]; then
#export FZF_DEFAULT_COMMAND='ag -g ""'
#export FZF_DEFAULT_OPTS='-m --preview-window=up:40%:wrap'
fi