-
Notifications
You must be signed in to change notification settings - Fork 1
/
zshrc.fzf
186 lines (162 loc) · 4.91 KB
/
zshrc.fzf
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
if [[ $- =~ i ]]; then
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse'
fvim() {
local files file
files=$(ag -p ~/.agignore -g "") &&
file=$(echo "$files" | fzf) &&
vim $file
}
fzf-cd-up() {
local declare dirs=()
get_parent_dirs() {
dirs+=("$1")
if [[ ${1} == '/' ]]; then
for _dir in ${dirs[@]}; do
echo $_dir
done
else
get_parent_dirs "$(dirname $1)"
fi
}
DIR=$(get_parent_dirs "$(pwd)" | fzf) && cd "$DIR"
zle reset-prompt
}
zle -N fzf-cd-up
bindkey '\eu' fzf-cd-up
gfv() {
local line
P=${1:-".*"}
line=($(ag --noheading -G $P . | awk -F':' '{s = "\x1b[31;1m" $1 "\x1b[m:" $2 "\t"; for (i = 3; i <= NF; i++) s = s $i; print s; }' | fzf --ansi -d ":" -n 2)) &&
f=($(echo $line | awk -F':' '{print $1;}')) &&
line_num=($(echo $line | awk -F':' '{print $2;}' | awk -F' ' '{print $1;}')) &&
vim +${line_num} "${f}"
}
# fco - checkout git branch/tag
fco() {
local tags branches target
tags=$(git tag --sort=-committerdate | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all --sort=-committerdate | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
awk '!x[$0]++' | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$branches"; echo "$tags") |
fzf --no-hscroll --ansi +m -d "\t" -n 2) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# fcoc - checkout git commit
fcoc() {
local commits commit
commits=$(git log --pretty=format:"%h - %an, %ar : %s" --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# 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"
}
# fs [FUZZY PATTERN] - Select selected tmux session
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
fs() {
local session
session=$(tmux list-sessions -F "#{session_name}" | \
fzf --query="$1" --select-1 --exit-0) &&
tmux switch-client -t "$session"
}
# fbr - checkout git branch (including remote branches)
fbr() {
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
# ALT-I - Paste the selected entry from locate output into the command line
fzf-locate-widget() {
local selected
if selected=$(locate / | fzf); then
LBUFFER=$LBUFFER$selected
fi
zle redisplay
}
zle -N fzf-locate-widget
bindkey '\ei' fzf-locate-widget
# fe [FUZZY PATTERN] - Open the selected file with the default editor
# - Bypass fuzzy finder if there's only one match (--select-1)
# - Exit if there's no match (--exit-0)
fe() {
IFS='
'
local declare files=($(fzf --query="$1" --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
unset IFS
}
# Modified version where you can press
# - CTRL-O to open with `open` command,
# - CTRL-E or Enter key to open with the $EDITOR
fo() {
local out file key
out=$(fzf --query="$1" --exit-0)
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
if [ -n "$file" ]; then
open "$file"
fi
}
# cdf - cd into the directory of the selected file
fcd() {
local file
local dir
file=$(fzf +m -q "$1") && dir=$(dirname "$file") && cd "$dir"
}
# fkill - kill process
fkill() {
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
kill -${1:-9} $pid
fi
}
_getDockerContainer() {
echo $(docker ps | sed 1d | sed -E 's/ +/ /g' | cut -d' ' -f1,2 | awk -F' ' '{print "\x1b[31;1m" $1 "\x1b[m: " $2;}' | fzf --ansi -d ' ' -n 2 | awk -F':' '{print $1}')
}
# fdbash - open bash for a docker
fdbash() {
cid=$(_getDockerContainer)
if [ "x$cid" != "x" ]
then
docker exec -it $cid bash
fi
}
# fdlogs - get docker logs
fdlogs() {
cid=$(_getDockerContainer)
if [ "x$cid" != "x" ]
then
docker logs $cid
fi
}
# fdstop - docker kill
fdstop() {
cid=$(_getDockerContainer)
if [ "x$cid" != "x" ]
then
docker stop $cid
fi
}
j() {
if [[ -z "$*" ]]; then
cd "$(_z -l 2>&1 | fzf +s --tac | sed 's/^[0-9,.]* *//')"
else
_z "$@"
fi
}
fi