-
Notifications
You must be signed in to change notification settings - Fork 4
/
fzf_aliases
220 lines (190 loc) · 7.88 KB
/
fzf_aliases
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
#! /usr/bin/env zsh
# TODO: consider --bind 'space:jump' for some of the git stuff?
# put some fzf-related overrides here too
# NOTE: removed "-I" (don't ignore git-ignored files) as they tend to be really noisy.
# TODO: is there a nice way to one-shot have fzf look for them if i want them to show up?
export FZF_DEFAULT_COMMAND='fd --ignore-file ~/.gitignore_global --type file --color=always'
# --color options modified from what pywal generates by default
export FZF_DEFAULT_OPTS='
--ansi --bind "ctrl-p:preview-up,ctrl-n:preview-down"
--color fg:7,hl:1,fg+:2,hl+:255
--color info:3,marker:1,pointer:3,spinner:1,gutter:7
'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND --ignore-file ~/.gitignore_global"
export FZF_ALT_C_COMMAND='fd -t d --ignore-file ~/.gitignore_global'
# requires overriding fzf-history-widget (see below)
# immediately execute a line with c-x (or inspect with enter, as usual)
export FZF_CTRL_R_OPTS="--expect=ctrl-x"
export FZF_CTRL_T_OPTS="--border"
__git_log () {
# format str implies:
# --abbrev-commit
# --decorate
git log \
--color=always \
--graph \
--all \
--date=short \
--format="%C(bold blue)%h%C(reset) %C(green)%ad%C(reset) | %C(white)%s %C(red)[%an] %C(bold yellow)%d"
}
# fuzzy find file content (open results with <Enter>)
fgr() {
local line
line=$(
ggrep --line-buffered --color=never -r '' * |
fzf --nth=1,2
) && ${EDITOR:-vim} $(echo "${line%%:*}")
}
# fuzzy completion with 'z' when called without args
unalias z 2> /dev/null
z() {
[ $# -gt 0 ] && zshz "$*" && return
cd "$(zshz -l 2>&1 | fzf --height 40% --nth 2.. --reverse --inline-info +s --tac --query "${*##-* }" | sed 's/^[0-9,.]* *//')"
}
# ftags - search ctags
ftags() {
local line
[ -e tags ] &&
line=$(
awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |
cut -c1-80 | fzf --nth=1,2
) && ${EDITOR:-vim} $(cut -f3 <<< "$line") -c "set nocst" \
-c "silent tag $(cut -f2 <<< "$line")"
}
# pass completion suggested by @d4ndo (#362) (slightly modified)
_fzf_complete_pass() {
_fzf_complete '+m' "$@" < <(
local pwdir=${PASSWORD_STORE_DIR-~/.password-store/}
find "$pwdir" -name "*.gpg" -print |
sed -e "s#${pwdir}/\{0,1\}##" |
sed -e 's/\(.*\)\.gpg/\1/'
)
}
# fshow_preview - git commit browser with previews
fshow_preview() {
local _gitLogLineToHash="echo -n {1} | head -1"
local _viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
local _viewPreview="$_gitLogLineToHash | xargs -I % sh -c '(git show --stat --color=always % && echo '' && git show --color=always %) | diff-so-fancy'"
git log --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an" |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewPreview" \
--header "enter to view, alt-y to copy hash" \
--bind "enter:execute:$_viewGitLogLine | less -R" \
--bind "alt-y:execute:$_gitLogLineToHash | pbcopy"
}
_fzf_complete_git() {
ARGS="$@"
# these are commands I commonly call on commit hashes.
# cp->cherry-pick, co->checkout
if [[ $ARGS == *'git cp'* || \
$ARGS == *'git cherry-pick'* || \
$ARGS == *'git co '* || \
$ARGS == *'git checkout'* || \
$ARGS == *'git reset'* ]]; then
_fzf_complete "--reverse --multi" "$@" < <(__git_log)
else
eval "zle ${fzf_default_completion:-expand-or-complete}"
fi
}
_fzf_complete_git_post() {
sed -e 's/^[^a-z0-9]*//' | awk '{print $1}'
}
f_git_hist () {
# fuzzy search commits from `git log`
git rev-parse --is-inside-work-tree > /dev/null 2>&1 || return
local item
__git_log |
fzf --height '50%' --border --ansi --no-sort --reverse --multi \
--delimiter="\|" --nth="-1" |
grep -o "[a-f0-9]\{7,\}" |
head -n1 |
while read -r item; do echo -n "${(q)item} "; done
}
fgst () {
# fuzzy search files from `git status`
git rev-parse --is-inside-work-tree > /dev/null 2>&1 || return
header="$(git -c color.status=always status -uno --short --branch | head -n1)"
local include_untracked=0
if [[ "$1" == "--fzf-alias-include-untracked" ]]; then
include_untracked=1
shift
fi
# remove any files that already have been accepted into LBUFFER
# zsh: parameter expansion
current=( ${=@} )
local output
# output of these commands is specifically not wrapped in quotes
if [[ $include_untracked -eq 0 ]]; then
output=$(command git -c color.status=always status --short -uno)
else
output=$(command git -c color.status=always status --short -uall)
fi
for word in $current; do
if [[ -f "$word" ]]; then
# disgusting, sorry...
output=$(grep -Fv "$word" <<< $output)
fi
done
local preview_cmd
if [[ $include_untracked -eq 0 ]]; then
# note that this preview_cmd will not show staged changes. should i figure out some kind of fallback?
# i could do something like this, but then i'm always only showing staged or unstaged changes in a file that might have both.
# is there a way to toggle?
# preview_cmd='git diff --exit-code --color=always {-1} && git diff --staged --color=always {-1}'
# there is a way from within fzf to toggle/cycle --preview by using --bind 'keybind:preview:new preview cmd'
# however this only cycles through for the selection entry. not sure if there's a good way to cycle the preview
# for that invocation of fzf.
# For now, adding a toggle to `git diff --staged` so I at least have that available.
preview_cmd='git diff --color=always {-1}'
else
preview_cmd='bat --style=changes --color=always --file-name={-1} {-1}'
fi
command echo "$output" |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-98%} \
--reverse \
$FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS" \
fzf -1 -m --cycle --border none --preview="$preview_cmd" --preview-window border-none --header="$header" \
--bind 'ctrl-g:preview:git diff --staged --color=always {-1}' |
while read -r item; do
echo "$item" | awk '{print $NF}'
done
}
if [[ $SHELL == *"zsh" ]]; then
fzf-gh-widget () { local result="$(f_git_hist)"; zle reset-prompt; LBUFFER+="$result" ; }
zle -N fzf-gh-widget
bindkey '^G^G' fzf-gh-widget
fzf-git-status-widget () { local result="$(fgst $LBUFFER)"; zle reset-prompt; LBUFFER+="$result"; }
zle -N fzf-git-status-widget
bindkey '^G^S' fzf-git-status-widget
fzf-git-untracked-widget () { local result="$(fgst --fzf-alias-include-untracked $LBUFFER)"; zle reset-prompt; LBUFFER+="$result"; }
zle -N fzf-git-untracked-widget
bindkey '^G^U' fzf-git-untracked-widget
fi
# copied+modified from FZF install: shell/key-bindings.zsh
# see:
# - https://github.com/junegunn/fzf/issues/477
# - checkout out https://github.com/junegunn/fzf/pull/1492
# CTRL-R - Paste the selected command from history into the command line
fzf-history-widget() {
local selected num
setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases 2> /dev/null
selected=( $(fc -rl 1 | perl -ne 'print if !$seen{(/^\s*[0-9]+\**\s+(.*)/, $1)}++' |
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} $FZF_DEFAULT_OPTS -n2..,.. --tiebreak=index --bind=ctrl-r:toggle-sort,ctrl-z:ignore $FZF_CTRL_R_OPTS --query=${(qqq)LBUFFER} +m" $(__fzfcmd)) )
local ret=$?
if [ -n "$selected" ]; then
local accept=0
if [[ $selected[1] = ctrl-x ]]; then
accept=1
shift selected
fi
num=$selected[1]
if [ -n "$num" ]; then
zle vi-fetch-history -n $num
[[ $accept = 1 ]] && zle accept-line
fi
fi
zle reset-prompt
return $ret
}
zle -N fzf-history-widget
bindkey '^R' fzf-history-widget