-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.zsh_functions
238 lines (204 loc) · 5.44 KB
/
.zsh_functions
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
# vim: ft=zsh
function mkcd() { nocorrect mkdir -p "$@" && nocorrect cd "$@"; }
function ranger-cd() {
tempfile=$(mktemp /tmp/${tempfoo}.XXXXXX)
ranger --choosedir="$tempfile" "${@:-$(pwd)}" < $TTY
test -f "$tempfile" &&
if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
cd -- "$(cat "$tempfile")"
fi
rm -f -- "$tempfile" > /dev/null 2>&1
}
function fl() {
tmp="$(mktemp)"
lf -last-dir-path="$tmp" "$@"
if [ -f "$tmp" ]; then
dir="$(cat "$tmp")"
rm -qf "$tmp"
if [ -d "$dir" ]; then
if [ "$dir" != "$(pwd)" ]; then
cd "$dir"
fi
fi
fi
}
function auto-ls-after-cd() {
emulate -L zsh
# Only in response to a user-initiated `cd`, not indirectly (eg. via another function).
if [ "$ZSH_EVAL_CONTEXT" = "toplevel:shfunc" ]; then
# Only if there are less then $LINES items in the directory
if (( `ls | wc -l` < $LINES )); then ls; fi
fi
}
add-zsh-hook chpwd auto-ls-after-cd
function edit-fzf() {
local result=`skg`
local filename=`echo $result | cut -f1 -d:`
local linenum=`echo $result | cut -f2 -d:`
$EDITOR $filename +$linenum
}
# cf - fuzzy cd
function cf() {
local bin=$(which sk)
if ! [ -x "$bin" ]; then
bin=$(which fzf)
fi
if [ -d "$1" ]; then cd "$1"
elif [[ "$1" == ,* ]]; then cd `fasd -ldR ${1#\,} | head -1`
else
local dir=`fd -t d | $bin`
if [ "$dir" != "" ]; then cd $dir; fi
fi
# ls after cd, but only if there are less then $LINES items in the directory
local bin=$(which exa)
if ! [ -x $bin ]; then bin=ls; fi
# Only if there are less then $LINES items in the directory
if (( `ls | wc -l` < $LINES )); then $bin --group-directories-first -x --color=auto; fi
}
# ef - fuzzy edit
function ef() {
if [ -d "$1" ]; then
ranger-cd "$1"
elif [ "$1" != "" ]; then
rifle "$1";
else
local target=(`fd | sk -m`)
if [ "$target" != "" ]; then rifle "${target[@]}"; fi
fi
}
function pdftotext() {
pdftohtml -stdout "$1" | html2text
}
function clo() {
git clone --recursive -j4 ${@} && cd *(/om[1])
}
function sf() {
if [ -f ~/.box-name ]; then
local longhost=`hostname`
local shorthost=`cat ~/.box-name`
screenfetch -E | sed "s/"$longhost"/"$shorthost"/"
else
screenfetch -E
fi
}
# ring bell after given command
function bo() {
$@;tput bel;
}
# open applications, directories and files on macOS
function o() {
local bin="open"
if command -v xdg-open > /dev/null 2>&1; then
bin="xdg-open"
fi
if [ "$#" -eq 0 ]; then
$bin .
elif [ -d /Applications/"$1".app ]; then
$bin -a $1
else
$bin $1
fi
}
function br() {
if command -v xdg-open > /dev/null 2>&1; then
bin="xdg-open"
elif command -v open > /dev/null 2>&1; then
bin="open"
fi
local gitpath=`git rev-parse --show-prefix 2>/dev/null | head -1`
if [[ gitpath == '' ]]; then return; fi
local remote="origin"
if [ "$#" -ne 0 ]; then
remote="$1"
fi
local url=`git remote get-url $remote | head -1`
if [[ "$url" =~ "@" ]]; then
url="${url/':'//}"
url="${url/*\@/https://}"
fi
url="${url/".git"}"
local branch=`git rev-parse --abbrev-ref HEAD | head -1`
$bin "$url/tree/$branch/$gitpath"
}
function git_status_conditional() {
output="$(git status -s)"
nl="${output//[!$'\n']/}"
if [ "$((#nl + 8))" -lt "${LINES}" ]; then
s
fi
}
alias git="g"
function g() {
if [[ "$1" == "add" ]]; then
\git add -v ${@:2}
git_status_conditional
elif [[ "$1" == "reset" ]]; then
\git reset -q ${@:2}
git_status_conditional
elif [[ "$1" == "commit" ]] || [[ "$1" == "checkout" ]]; then
\git $@
git_status_conditional
else
\git $@
fi
}
function git_add() {
if [ "$#" -eq 0 ]; then
git add -v .
else
git add -v "$@"
fi
}
function git_commit() {
git commit "$@"
}
function cm() {
nocorrect git commit -m "$*"
}
function cv() {
git commit -v "$@"
git_status_conditional
}
function git_reset() {
git reset -q "$@"
}
function git_checkout() {
git checkout "$@"
}
function vman() {
nvim -R -c "Goyo | Man $@ | wincmd w | bd | nnoremap q :qa!<CR>"
}
compdef vman="man"
function Terminal() { tmux attach 2>/dev/null || tmux new }
function n() {
local root=$(dirname $(npm root))
local bin="npm"
if [ -f "${root}/pnpm-lock.yaml" ]; then
bin="pnpm"
elif [ -f "${root}/yarn.lock" ]; then
bin="yarn"
fi
if [ "$#" -eq 0 ]; then
echo "${bin} install"
$bin install
elif [[ "$bin" == "npm" ]] && [[ "$1" == "add" ]]; then
echo "$bin install ${@:2}"
$bin install ${@:2}
elif [[ "$1" == "t" ]]; then
$bin test ${@:2}
elif [[ "$(cat ${root}/package.json | jq '.scripts | keys' | grep "$1" 2>/dev/null)" != "" ]]; then
echo "$bin run $@"
$bin run $@
else
echo "$bin $@"
$bin $@
fi
}
function kubectl() {
if ! type __start_kubectl >/dev/null 2>&1; then
source <(command kubectl completion zsh)
fi
command kubectl "$@"
}
# Make zsh know about hosts already accessed by SSH
zstyle -e ':completion:*:(ssh|scp|sftp|rsh|rsync):hosts' hosts 'reply=(${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ })'