-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.zshfunc
339 lines (301 loc) · 8 KB
/
.zshfunc
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- mode: sh -*-
function gim() {
vim `git ls-files | peco`
}
function cdgem() {
local gem_name=$(bundle list | sed -e 's/^ *\* *//g' | peco | cut -d \ -f 1)
if [ -n "$gem_name" ]; then
local gem_dir=$(bundle show ${gem_name})
echo "cd to ${gem_dir}"
cd ${gem_dir}
fi
}
function pcd() {
local base="$1"
if [ ! -z "$1" ] ; then
cd
fi
local dir="$( find . -maxdepth 1 -type d | sed -e 's;\./;;' | peco )"
if [ ! -z "$dir" ] ; then
cd "$dir"
fi
}
function dev_pcd()
{
cd ~/dev
pcd
my_reset_prompt
}
zle -N dev_pcd
function peco-select-history() {
local tac
if which tac > /dev/null; then
tac="tac"
else
tac="tail -r"
fi
BUFFER=$(\history -n 1 | \
eval $tac | \
peco --query "$LBUFFER")
CURSOR=$#BUFFER
zle clear-screen
}
zle -N peco-select-history
function peco-cdr () {
local selected_dir=$(cdr -l | awk '{ print $2 }' | peco)
if [ -n "$selected_dir" ]; then
BUFFER="cd ${selected_dir}"
zle accept-line
fi
zle clear-screen
}
zle -N peco-cdr
function peco-find-file () {
ls | peco | xargs vim -nw
zle clear-screen
}
zle -N peco-find-file
ls_abbrev() {
if [[ ! -r $PWD ]]; then
return
fi
# -a : Do not ignore entries starting with ..
# -C : Force multi-column output.
# -F : Append indicator (one of */=>@|) to entries.
local cmd_ls='ls'
local -a opt_ls
opt_ls=('-aCF' '--color=always')
case "${OSTYPE}" in
freebsd*|darwin*)
if type gls > /dev/null 2>&1; then
cmd_ls='gls'
else
# -G : Enable colorized output.
opt_ls=('-aCFG')
fi
;;
esac
local ls_result
ls_result=$(CLICOLOR_FORCE=1 COLUMNS=$COLUMNS command $cmd_ls ${opt_ls[@]} | sed $'/^\e\[[0-9;]*m$/d')
local ls_lines=$(echo "$ls_result" | wc -l | tr -d ' ')
if [ $ls_lines -gt 10 ]; then
echo "$ls_result" | head -n 5
echo '...'
echo "$ls_result" | tail -n 5
echo "$(command ls -1 -A | wc -l | tr -d ' ') files exist"
else
echo "$ls_result"
fi
}
function do_enter() {
if [ -n "$BUFFER" ]; then
zle accept-line
return 0
fi
echo
echo -e "\e[0;33m--- ls ---\e[0m"
# ls -aCFG
ls_abbrev
if [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" = 'true' ]; then
echo
echo -e "\e[0;33m--- git status ---\e[0m"
git status -sb
echo
fi
echo -e "\e[0;33m--- end ---\e[0m"
my_reset_prompt
return 0
}
zle -N do_enter
function tmux_window_name()
{
tmux rename-window ${PWD:t}
}
if [ -n "$TMUX" ]; then
autoload -Uz add-zsh-hook
add-zsh-hook precmd tmux_window_name
fi
function my_reset_prompt()
{
if type precmd > /dev/null 2>&1; then
precmd
fi
local precmd_func
for precmd_func in $precmd_functions; do
if [[ "^notify-command-complete" =~ "${precmd_func}" ]] > /dev/null; then
echo "" > /dev/null
fi
$precmd_func
done
zle reset-prompt
}
function brew-file-brew()
{
brew-file-install()
{
brew-file brew install $*
}
case "$1" in
install | i)
echo "Brew-file update Brewfile"
shift
brew-file-install $*
;;
*)
brew $*
esac
}
# 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))
# if [ -n "$files" ]; then
# ${=EDITOR} "${files[@]}"
# fi
# 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 --expect=ctrl-o,ctrl-e)
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
if [ -n "$file" ]; then
[ "$key" = ctrl-o ] && open "$file" || ${=EDITOR} "$file"
fi
}
# fd - cd to selected directory
fd() {
local dir
dir=$(find ${1:-.} -path '*/\.*' -prune -d 3 \
-o -type d -print 2> /dev/null | fzf +m) && cd "$dir"
my_reset_prompt
}
zle -N fd
# fda - including hidden directories
fda() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
}
fdr() {
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 --tac)
cd "$DIR"
}
zle -N fdr
# 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 | \
fzf --query="$1" --select-1 --exit-0 | \
awk -F: '{ print $1 }') &&
tmux switch-client -t "$session"
}
zle -N fs
ftw() {
local window
window=$(tmux list-windows | \
fzf --query="$1" --select-1 --exit-0 | \
awk -F: '{ print $1 }') &&
tmux select-window -t "$window"
}
zle -N ftw
# ftpane - switch pane (@george-b)
ftpane() {
local panes current_window current_pane target target_window target_pane
panes=$(tmux list-panes -s -F '#I:#P - #{pane_current_path} #{pane_current_command}')
current_pane=$(tmux display-message -p '#I:#P')
current_window=$(tmux display-message -p '#I')
target=$(echo "$panes" | grep -v "$current_pane" | fzf +m --reverse) || return
target_window=$(echo $target | awk 'BEGIN{FS=":|-"} {print$1}')
target_pane=$(echo $target | awk 'BEGIN{FS=":|-"} {print$2}' | cut -c 1)
if [[ $current_window -eq $target_window ]]; then
tmux select-pane -t ${target_window}.${target_pane}
else
tmux select-pane -t ${target_window}.${target_pane} &&
tmux select-window -t $target_window
fi
}
# In tmux.conf
# bind-key 0 run "tmux split-window -l 12 'bash -ci ftpane'"
fcdr() {
local selected_dir=$(cdr -l | awk '{ print $2 }' | fzf +m)
if [ -n "$selected_dir" ]; then
BUFFER="cd ${selected_dir}"
zle accept-line
fi
}
zle -N fcdr
pf() {
ps aux | fzf -e
}
build_emacs() {
echo
echo "------------------------------------"
echo
echo "execute: make maintainer-clean"
make maintainer-clean
echo "finished: make maintainer-clean"
echo
echo "------------------------------------"
echo
echo "execute: ./autogen.sh"
./autogen.sh
echo "finished: ./autogen.sh"
echo
echo "------------------------------------"
echo
echo "execute: configure"
./configure --with-ns --without-x --with-imagemagick --with-xml2 --with-rsvg --disable-ns-self-contained --with-modules
echo "finished: configure"
echo
echo "------------------------------------"
echo
echo "execute: make bootstrap"
make bootstrap
echo "finished: make bootstrap"
echo
echo "------------------------------------"
echo
echo "execute: make install"
make install
echo "finished: make install"
}
mem() {
local command="$1"
ps aux | grep -i "$command" | awk -F' ' '{print $6, $11}'| grep -v grep | awk -F ' ' '{sum += $1} END {print sum/1000 " MB"}'
}
man() {
LESS_TERMCAP_md=$'\e[01;31m' \
LESS_TERMCAP_me=$'\e[0m' \
LESS_TERMCAP_se=$'\e[0m' \
# LESS_TERMCAP_so=$'\e[01;44;33m' \
LESS_TERMCAP_ue=$'\e[0m' \
LESS_TERMCAP_us=$'\e[01;32m' \
command man "$@"
}
fzf_src() {
local dir
dir=$(ghq list --full-path | fzf) && cd "$dir" && my_reset_prompt
}
zle -N fzf_src
convert_to_gif() {
input="$1"
output="${2:-out.gif}"
ffmpeg -i $input -vf scale=320:-1 -r 30 $output
}