/
my-fzf.zsh
222 lines (195 loc) · 5.82 KB
/
my-fzf.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
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow --glob "!.git/*"'
export FZF_COMPLETION_TRIGGER='``'
export FZF_DEFAULT_OPTS="--height 70% --reverse --extended --no-inline-info
--prompt '➥ '
--color fg:-1,bg:-1,hl:230,fg+:3,bg+:233,hl+:229
--color info:150,prompt:168,spinner:150,pointer:167,marker:174"
# If current selection is a text file shows its content,
# if it's a directory shows its content, the rest is ignore
FZF_CTRL_T_OPTS="--preview-window wrap --preview '
if [[ -f {} ]]; then
file --mime {} | grep -q \"text\/.*;\" && bat --color \"always\" {} || (tput setaf 1; file --mime {})
elif [[ -d {} ]]; then
ls -l --color=auto {}
fi'"
#Jellyx
#--prompt '➤ ➥ ➢ ➠ ➜ ➛'
export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -200'"
#Jellybean color theme
#--color fg:188,bg:233,hl:103,fg+:222,bg+:234,hl+:104
#--color info:183,prompt:110,spinner:107,pointer:167,marker:215
#Molokai
#--color fg:252,bg:233,hl:67,fg+:252,bg+:235,hl+:81
#--color info:144,prompt:161,spinner:135,pointer:135,marker:118
#Dusk
#--color fg:242,bg:233,hl:65,fg+:15,bg+:239,hl+:108
#--color info:108,prompt:109,spinner:108,pointer:168,marker:168
# #####################GIT GOODIES###################
# KEYBINDINGS <C-G><C-F>, <C-G><C-H>, <C-G><C-R>, <C-G><C-B>, <C-G><C-T>
# ####################################################
# Described in:
# - https://junegunn.kr/2016/07/fzf-git/
# - https://github.com/junegunn/fzf/wiki/examples#git
# Will return non-zero status if the current directory is not managed by git
# GIT heart FZF
#
is_in_git_repo() {
git rev-parse HEAD > /dev/null 2>&1
}
# -------------
fzf-down() {
fzf --height 90% "$@" --border
}
FZF_PREFIX="fzf-git-"
function "${FZF_PREFIX}gs" () {
is_in_git_repo || return
git -c color.status=always status --short |
fzf-down -m --ansi --nth 2..,.. \
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' |
cut -c4- | sed 's/.* -> //'
}
function "${FZF_PREFIX}gb" () {
is_in_git_repo || return
git branch -a --color=always | grep -v '/HEAD\s' | sort |
fzf-down --ansi --multi --tac --preview-window right:70% \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES |
sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##'
}
# git tag
function "${FZF_PREFIX}gt" () {
is_in_git_repo || return
git tag --sort -version:refname |
fzf-down --multi --preview-window right:70% \
--preview 'git show --color=always {} | head -'$LINES
}
function "${FZF_PREFIX}gh" () {
is_in_git_repo || return
git log --date=short --format="%C(green)%C(bold)%cd %C(blue)%an %C(auto)%h %s" --graph --color=always |
fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
--header 'Press CTRL-S to toggle sort' \
--preview-window right:40% \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES |
grep -o "[a-f0-9]\{7,\}"
}
function "${FZF_PREFIX}gr" () {
is_in_git_repo || return
git remote -v | awk '{print $1 "\t" $2}' | uniq |
fzf-down --tac \
--preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1} | head -200' |
cut -d$'\t' -f1
}
join-lines() {
local item
while read item; do
echo -n "${(q)item} "
done
}
bind-git-helper() {
local char
for c in $@; do
eval "fzf-g$c-widget() { local result=\$(${FZF_PREFIX}g$c | join-lines); zle reset-prompt; LBUFFER+=\$result }"
eval "zle -N fzf-g$c-widget"
eval "bindkey '^g^$c' fzf-g$c-widget"
done
}
bind-git-helper f b t r h
unset -f bind-git-helper
###################END OF GIT GOODIES###################
# vf - fuzzy open with vim from anywhere
# ex: vf word1 word2 ... (even part of a file name)
# zsh autoload function
vv() {
local files
files=(${(f)"$(locate -Ai -0 $@ | grep -z -vE '~$' | fzf --read0 -0 -1 -m)"})
if [[ -n $files ]]
then
nvim -- $files
print -l $files[1]
fi
}
zle -N fdh
bindkey '^v' fdh
# fd - including hidden directories
fdh() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
zle redisplay
}
zle -N fdh
bindkey '^j' fdh
# fk - kill process
fk() {
local pid
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
zle redisplay
}
zle -N fk
bindkey '^k' fk
# ALT-I - Paste the selected entry from locate output into the command line
fzf-locate-widget() {
local selected
if selected=$(locate / | fzf -q "$LBUFFER"); then
LBUFFER=$selected
fi
zle redisplay
}
zle -N fzf-locate-widget
bindkey '^n' fzf-locate-widget
# fe - open files in ~/.viminfo
fe() {
local files
files=$(grep '^>' ~/.viminfo | cut -c3- |
while read line; do
[ -f "${line/\~/$HOME}" ] && echo "$line"
done | fzf-tmux -d -m -q "$*" -1) && nvim ${files//\~/$HOME}
zle redisplay
}
zle -N fe
bindkey '^e' fe
# search and open file
fo() {
local files
files=$(fd . '.' | fzf-tmux -d -m -q "$*" -1) && nvim ${files//\~/$HOME}
#if [[ -n $files ]]
#then
#nvim -- $files
#print -l $files[1]
#fi
zle redisplay
}
zle -N fo
bindkey '^o' fo
# fv - open files in vimwiki
fw() {
local files
files=$(fd -d 1 . '/home/marius/MEGA/vimwiki/' | fzf-tmux -d -m -q "$*" -1) && nvim ${files//\~/$HOME}
#if [[ -n $files ]]
#then
#nvim -- $files
#print -l $files[1]
#fi
zle redisplay
}
zle -N fw
bindkey '^w' fw
# F search all
rga-fzf() {
# local file
RG_PREFIX="rga --files-with-matches"
file="$(
FZF_DEFAULT_COMMAND="$RG_PREFIX '$1'" \
fzf --sort --preview="[[ ! -z {} ]] && rga --pretty --context 3 {q} {}" \
--phony -q "$1" \
--bind "change:reload:$RG_PREFIX {q}" \
--preview-window="70%:wrap"
)" &&
echo "opening $file" &&
nvim "$file"
}
zle -N rga-fzf
bindkey '^f' rga-fzf