-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
awesome-fzf.zsh
167 lines (144 loc) · 4.4 KB
/
awesome-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
AWESOME_FZF_LOCATION="/path/to/awsome-fzf.zsh"
#List Awesome FZF Functions
function fzf-awesome-list() {
if [[ -f $AWESOME_FZF_LOCATION ]]; then
selected=$(grep -E "(function fzf-)(.*?)[^(]*" $AWESOME_FZF_LOCATION | sed -e "s/function fzf-//" | sed -e "s/() {//" | grep -v "selected=" | fzf --reverse --prompt="awesome fzf functions > ")
else
echo "awesome fzf not found"
fi
case "$selected" in
"");; #don't throw an exit error when we dont select anything
*) "fzf-"$selected;;
esac
}
#Enhanced rm
function fzf-rm() {
if [[ "$#" -eq 0 ]]; then
local files
files=$(find . -maxdepth 1 -type f | fzf --multi)
echo $files | xargs -I '{}' rm {} #we use xargs so that filenames to capture filenames with spaces in them properly
else
command rm "$@"
fi
}
# Man without options will use fzf to select a page
function fzf-man() {
MAN="/usr/bin/man"
if [ -n "$1" ]; then
$MAN "$@"
return $?
else
$MAN -k . | fzf --reverse --preview="echo {1,2} | sed 's/ (/./' | sed -E 's/\)\s*$//' | xargs $MAN" | awk '{print $1 "." $2}' | tr -d '()' | xargs -r $MAN
return $?
fi
}
#Eval commands on the fly
function fzf-eval() {
echo | fzf -q "$*" --preview-window=up:99% --preview="eval {q}"
}
## Search list of your aliases and functions
function fzf-aliases-functions() {
CMD=$(
(
(alias)
(functions | grep "()" | cut -d ' ' -f1 | grep -v "^_" )
) | fzf | cut -d '=' -f1
);
eval $CMD
}
## File Finder (Open in $EDITOR)
function fzf-find-files() {
local file=$(fzf --multi --reverse) #get file from fzf
if [[ $file ]]; then
for prog in $(echo $file); #open all the selected files
do; $EDITOR $prog; done;
else
echo "cancelled fzf"
fi
}
# Find Dirs
function fzf-cd() {
local dir
dir=$(find ${1:-.} -path '*/\.*' -prune \
-o -type d -print 2> /dev/null | fzf +m) &&
cd "$dir"
ls
}
# Find Dirs + Hidden
function fzf-cd-incl-hidden() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
ls
}
# cd into the directory of the selected file
function fzf-cd-to-file() {
local file
local dir
file=$(fzf +m -q "$1") && dir=$(dirname "$file") && cd "$dir"
ls
}
# fdr - cd to selected parent directory
function fzf-cd-to-parent() {
local declare dirs=()
get_parent_dirs() {
if [[ -d "${1}" ]]; then dirs+=("$1"); else return; fi
if [[ "${1}" == '/' ]]; then
for _dir in "${dirs[@]}"; do echo $_dir; done
else
get_parent_dirs $(dirname "$1")
fi
}
local DIR=$(get_parent_dirs $(realpath "${1:-$PWD}") | fzf-tmux --tac)
cd "$DIR"
ls
}
# Search env variables
function fzf-env-vars() {
local out
out=$(env | fzf)
echo $(echo $out | cut -d= -f2)
}
# Kill process
function fzf-kill-processes() {
local pid
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
# Enhanced Git Status (Open multiple files with tab + diff preview)
fzf-git-status() {
git rev-parse --git-dir > /dev/null 2>&1 || { echo "You are not in a git repository" && return }
local selected
selected=$(git -c color.status=always status --short |
fzf --height 50% "$@" --border -m --ansi --nth 2..,.. \
--preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' |
cut -c4- | sed 's/.* -> //')
if [[ $selected ]]; then
for prog in $(echo $selected);
do; $EDITOR $prog; done;
fi
}
# Checkout to existing branch or else create new branch. gco <branch-name>.
# Falls back to fuzzy branch selector list powered by fzf if no args.
fzf-checkout(){
if git rev-parse --git-dir > /dev/null 2>&1; then
if [[ "$#" -eq 0 ]]; then
local branches branch
branches=$(git branch -a) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
elif [ `git rev-parse --verify --quiet $*` ] || \
[ `git branch --remotes | grep --extended-regexp "^[[:space:]]+origin/${*}$"` ]; then
echo "Checking out to existing branch"
git checkout "$*"
else
echo "Creating new branch"
git checkout -b "$*"
fi
else
echo "Can't check out or create branch. Not in a git repo"
fi
}