-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions
278 lines (229 loc) · 7.02 KB
/
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
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
# Create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@"
}
# whois a domain or a URL
function whois() {
local domain=$(echo "$1" | awk -F/ '{print $3}') # get domain from URL
if [ -z $domain ] ; then
domain=$1
fi
echo "Getting whois record for: $domain …"
# avoid recursion
/usr/bin/whois -h whois.internic.net $domain | sed '/NOTICE:/q'
}
# Style git diff tool
function strip_diff_leading_symbols(){
color_code_regex=$'(\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K])'
# simplify the unified patch diff header
sed -E "s/^($color_code_regex)diff --git .*$//g" | \
sed -E "s/^($color_code_regex)index .*$/\
\1$(rule)/g" | \
sed -E "s/^($color_code_regex)\+\+\+(.*)$/\1\+\+\+\5\\
\1$(rule)/g" | \
# actually strips the leading symbols
sed -E "s/^($color_code_regex)[\+\-]/\1 /g"
}
# Change iterm2 profile. Usage it2prof ProfileName (case sensitive)
profile() {
[ -f ~/.iterm_profile ] && rm -f ~/.iterm_profile
echo $1 > ~/.iterm_profile
setItermProfile
}
light() {
profile light
setVimColors 'light' 'base16-kaye-light'
}
dark() {
profile dark
setVimColors 'dark' 'base16-kaye'
}
setItermProfile() {
if [ -f ~/.iterm_profile ]; then
local profile
profile="`cat ~/.iterm_profile | tr -d '\040\011\012\015'`"
export ITERM_PROFILE="$profile"
if ! { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then
echo -e "\033Ptmux;\033\033]50;SetProfile=$profile\a\033\\"
else
echo -e "\033]50;SetProfile=$profile\a"
fi
fi
}
setVimColors() {
[ -f ~/.vimrc_background ] && rm -f ~/.vimrc_background
local lightlineTheme
lightlineTheme=$(echo "$2" | sed -e 's/-/_/g')
echo "set background=$1
colorscheme $2
let g:lightlineTheme='$lightlineTheme'" > ~/.vimrc_background
}
# Display a truecolor gradient
truecolor() {
awk 'BEGIN{
s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;
for (colnum = 0; colnum<77; colnum++) {
r = 255-(colnum*255/76);
g = (colnum*510/76);
b = (colnum*255/76);
if (g>255) g = 510-g;
printf "\033[48;2;%d;%d;%dm", r,g,b;
printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
printf "%s\033[0m", substr(s,colnum+1,1);
}
printf "\n";
}'
}
# Search history from previous line
up-line-or-search-prefix () {
local CURSOR_before_search=$CURSOR
zle up-line-or-search "$LBUFFER"
CURSOR=$CURSOR_before_search
}
## Print a horizontal rule
rule () {
printf "%$(tput cols)s\n"|tr " " "─"
}
# fe [FUZZY PATTERN] - Open the selected file with the default editor
function fe() {
local file
file=$(fzf-tmux --query="$1" --select-1 --exit-0)
[ -n "$file" ] && ${EDITOR:-vim} "$file"
}
# 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
}
# fda - including hidden directories
function fda() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf-tmux +m) && cd "$dir"
}
# fkill - kill process
function fkill() {
ps -ef | sed 1d | fzf-tmux -m | awk '{print $2}' | xargs kill -${1:-9}
}
# cdf - cd into the directory of the selected file
function cdf() {
local file
local dir
file=$(fzf-tmux +m -q "$1") && dir=$(dirname "$file") && cd "$dir"
}
# z - display z results within fzf-tmux pane
unalias z 2> /dev/null
function z() {
if [[ -z "$*" ]]; then
cd "$(_z -l 2>&1 | fzf-tmux +s --tac | sed 's/^[0-9,.]* *//')"
else
_z "$@"
fi
}
# fbr - checkout git branch (including remote branches)
function fco() {
local branches branch
branches=$(git branch --all | grep -v HEAD) &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
# fshow - git commit browser
function fshow() {
local out sha q
while out=$(
git log --decorate=short --graph --oneline --color=always |
fzf-tmux --ansi --multi --no-sort --reverse --query="$q" --print-query); do
q=$(head -1 <<< "$out")
while read sha; do
[ -n "$sha" ] && git show --color=always $sha | less -R
done < <(sed '1d;s/^[^a-z0-9]*//;/^$/d' <<< "$out" | awk '{print $1}')
done
}
# fstash - easier way to deal with stashes
# type fstash to get a list of your stashes
# enter shows you the contents of the stash
# ctrl-d shows a diff of the stash against your current HEAD
# ctrl-b checks the stash out as a branch, for easier merging
fstash() {
local out q k sha
while out=$(
git stash list --pretty="%C(yellow)%h %>(14)%Cgreen%cr %C(blue)%gs" |
fzf --ansi --no-sort --query="$q" --print-query \
--expect=ctrl-d,ctrl-b);
do
mapfile -t out <<< "$out"
q="${out[0]}"
k="${out[1]}"
sha="${out[-1]}"
sha="${sha%% *}"
[[ -z "$sha" ]] && continue
if [[ "$k" == 'ctrl-d' ]]; then
git diff $sha
elif [[ "$k" == 'ctrl-b' ]]; then
git stash branch "stash-$sha" $sha
break;
else
git stash show -p $sha
fi
done
}
# 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)
function fs() {
local session
session=$(tmux list-sessions -F "#{session_name}" | \
fzf-tmux --query="$1" --select-1 --exit-0) &&
tmux switch-client -t "$session"
}
# who is using the laptop's iSight camera?
function camerausedby() {
echo "Checking to see who is using the iSight camera… 📷"
usedby=$(lsof | grep -w "AppleCamera\|USBVDC\|iSight" | awk '{printf $2"\n"}' | xargs ps)
echo -e "Recent camera uses:\n$usedby"
}
## Change cursor based on vi mode
# - Insert mode displays _ cursor
# - Command mode displays █ cursor
zle-keymap-select () {
zle editor-info
if [[ ("$TERM" == "xterm-256color") ||
("$TERM" == "screen-256color") ||
("$TERM" == "xterm-256color-italic") ||
("$TERM" == "screen-256color-italic") ]]; then
if [ "$KEYMAP" = "vicmd" ]; then
echo -ne "\e[2 q"
else
echo -ne "\e[4 q"
fi
fi
}
## Default to insert mode
zle-line-init () {
zle -K viins
}
## Set layout
workman() {
export KEYBOARD_LAYOUT="workman"
sed -i "" "1s/.*/export KEYBOARD_LAYOUT='workman'/" ~/.profile
source ~/.zshrc
echo "Keyboard layout switched to $KEYBOARD_LAYOUT"
}
## Set qwerty layout
qwerty() {
export KEYBOARD_LAYOUT="qwerty";
sed -i "" "1s/.*/export KEYBOARD_LAYOUT='qwerty'/" ~/.profile
source ~/.zshrc
echo "Keyboard layout switched to $KEYBOARD_LAYOUT"
}