-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.shell.sh
374 lines (319 loc) · 10.5 KB
/
.shell.sh
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#############################
### Environment variables ###
#############################
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk"
export VISUAL="v"
export EDITOR="v"
# export PAGER="vimpager"
export CP="CHERRY_PICK_HEAD"
# respect gitignore by default (and I am not sure but probably fd is faster than find then this export also speed ups the fzf)
export FZF_DEFAULT_COMMAND='fd --type f --hidden'
export PATH="$HOME/.bin:$PATH"
export ANDROID_HOME="$HOME/.android/sdk"
# export FZF_DEFAULT_OPTS="\
# --height 60% --layout=reverse --multi \
# --bind 'ctrl-y:execute-silent(echo {} | xclip -selection clipboard)' \
# --bind 'ctrl-o:execute-silent(o {})'"
export NNN_PLUG='p:preview-tui'
export NNN_FIFO=/tmp/nnn.fifo # reuiqred for nnn-preview plugin
#export LESS="-i"
if isMacOs; then
# https://gist.github.com/skyzyx/3438280b18e4f7c490db8a2a2ca0b9da
BREW_BIN="/usr/local/bin/brew"
if [ -f "/opt/homebrew/bin/brew" ]; then
BREW_BIN="/opt/homebrew/bin/brew"
fi
if type "${BREW_BIN}" &> /dev/null; then
export BREW_PREFIX="$("${BREW_BIN}" --prefix)"
for bindir in "${BREW_PREFIX}/opt/"*"/libexec/gnubin"; do export PATH=$bindir:$PATH; done
for bindir in "${BREW_PREFIX}/opt/"*"/bin"; do export PATH=$bindir:$PATH; done
for mandir in "${BREW_PREFIX}/opt/"*"/libexec/gnuman"; do export MANPATH=$mandir:$MANPATH; done
for mandir in "${BREW_PREFIX}/opt/"*"/share/man/man1"; do export MANPATH=$mandir:$MANPATH; done
fi
export PATH="$HOME/.bin-macos:$PATH"
fi
if [ -d $ANDROID_HOME ]; then
export PATH="$PATH:$ANDROID_HOME/platform-tools"
fi
# If not running interactively, don't do anything
[[ $- != *i* ]] && return
simplified_mode() {
test -z "$DISPLAY" && ! isMacOs
}
CURRENT_SHELL=$1
if [ -z $CURRENT_SHELL ]; then
echo_red "Current shell is unknown. Pass it parameter to ~/.shell.sh"
fi
is_zsh() {
test $CURRENT_SHELL = "zsh"
return $?
}
echo_red() {
echo -e "\e[1;31m$@\e[0m"
}
xdotool_available() {
if simplified_mode; then
return 1
fi
return $(command -v xdotool > /dev/null)
}
join_to_string() { local IFS="$1"; shift; echo "$*"; }
########################
### General settings ###
########################
if ! isMacOs && [[ "$(sysctl kernel.sysrq)" != *"= 1" ]]; then
echo_red "sysrq kernel option is off. Enter the sudo password to turn it on"
sudo sysctl kernel.sysrq=1
fi
if ! simplified_mode; then
if ! xdotool_available; then
echo_red "xdotool isn't available. GUI notifications won't work"
fi
if ! command -v notify-send > /dev/null; then
echo_red "notify-send isn't installed. GUI notifications won't work"
fi
fi
if is_zsh; then
# Lines configured by ZSH setup wizard
HISTFILE=~/.zsh_history
HISTSIZE=5000
SAVEHIST=1000
# 0ms for key sequences (I did it for removing delay for ESC key)
export KEYTIMEOUT=0
source_package() {
for it in $@; do
if [ -f $it ]; then
source $it
else
echo_red "Cannot find $it"
fi
done
}
if isMacOs; then
source $(brew --prefix)/Cellar/antigen/*/share/antigen/antigen.zsh
else
source /usr/share/zsh/share/antigen.zsh
fi
# vim mode
# bindkey -v
antigen bundle jeffreytse/zsh-vi-mode
antigen bundle zsh-users/zsh-autosuggestions
antigen apply
zvm_after_init() {
if isMacOs; then
source_package $(brew --prefix)/Cellar/fzf/*/shell/key-bindings.zsh
source_package $(brew --prefix)/Cellar/fzf/*/shell/completion.zsh
# source_package $(brew --prefix)/Cellar/fzf/*/shell/fzf-extras.zsh
else
source_package /usr/share/fzf/key-bindings.zsh
source_package /usr/share/fzf/completion.zsh
source_package /usr/share/fzf/fzf-extras.zsh
fi
zvm_bindkey viins '^H' backward-kill-word
zvm_bindkey vicmd 'H' zle vi-first-non-blank
zvm_bindkey vicmd 'L' zle vi-end-of-line
# Disable because I used to press tab when I know that the first match
# will be ok. fzf-tab prompts unnecessary fuzzy search in such case
# source_package ~/.app/fzf-tab/fzf-tab.plugin.zsh
}
zvm_after_init
zvm_vi_yank() { # Hack from https://github.com/jeffreytse/zsh-vi-mode/issues/19
zvm_yank
printf %s "${CUTBUFFER}" | cl
zvm_exit_visual_mode
}
# source_package ~/.app/zsh-syntax-highlighting/zsh-syntax-highlighting.plugin.zsh # Disable because comments are not visible
# Starts the completion system
if type brew &>/dev/null; then
FPATH="$(brew --prefix)/share/zsh-completions:$FPATH"
FPATH="$(brew --prefix)/share/zsh/site-functions:$FPATH"
fi
autoload -Uz compinit
compinit
# Initialize colors
autoload -U colors
colors
# Use run-help for help for builtin commands like `zle`, `setopt`
unalias run-help
autoload run-help
# If globbing expression don't match anything then
# ZSH will pass it "as it is". (e.g. asterisk expansion)
setopt +o nomatch
# Enable comments in interactive shell
setopt INTERACTIVE_COMMENTS
# File completion after '=' character in args
setopt MAGIC_EQUAL_SUBST
# setopt MENU_COMPLETE
# Turn off additional zsh verification for rm smt/*
setopt rm_star_silent
# Share history between different instances of zsh
# setopt share_history
# Turn on case insensetive in completion
zstyle ':completion:*' matcher-list 'm:{[:lower:]}={[:upper:]}'
# Discard autosuggest with Escape
# bindkey '\e' autosuggest-clear # why do I need it?
fi
###############
### aliases ###
###############
# bookmarks
alias bk='cd ~/jb/kotlin'
alias bi='cd ~/jb/intellij'
alias bn='cd ~/a/notes'
alias jb='cd ~/jb'
alias a='cd ~/a'
alias sb='subl'
alias xdot='detach xdot'
alias vlc='detach vlc'
v-sh() { nvim "+source ~/.v/vim-sh.vim" "$@"; }
alias mount='udisksctl mount -b'
alias umount='udisksctl unmount -b'
alias gw='./gradlew'
alias gwDebug='./gradlew --stop && ./gradlew -Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket\\,server=y\\,suspend=y\\,address=5005" '
alias ls='ls -F --color=auto'
alias diff='diff -us --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
zd() { cd "$(interactive-cd)"; }
alias egrep='egrep --color=auto'
alias ll='ls -alFh'
alias cp='cp -r'
alias scp='scp -r'
alias mkdir='mkdir -p'
alias gs='echo fucking ghostscript'
alias fd='fd --hidden'
alias vanila-vim='vim -u NONE'
vanila-tmux() { tmux -L "$RANDOM" -f /dev/null "$@"; }
alias lf='ls | fzf'
alias cal='cal -y'
alias i='idea-bobko'
alias help='run-help' # For shell built-ins
tmp() { cd $(mktemp -d); }
killvlc() {
# It freezes so often that it deserves a separate alias
ps -A | grep vlc | awk '{print$1}' | while read it; do kill -9 $it; done
}
alias n='nnn -p /tmp/nnn-select && ! test -s /tmp/nnn-select || v /tmp/nnn-select'
alias ta='tmux attach || tmux new-session -s MMM'
alias tmux-source='tmux source ~/.tmux.conf'
alias rm='rm-trash'
alias cg='cd "$(git rev-parse --show-toplevel)"'
if is_zsh; then
true # empty then cannot be parsed in bash
# compdef git d
# compadd d _path_files
# compdef '_dispatch git git' d
else
# complete -o bashdefault -o default -o nospace -F __git_wrap__git_main dotfiles
_completion_loader git
fi
if ! isMacOs && [[ "$(tty)" == "/dev/tty"* ]]; then
alias x='startx'
alias p='poweroff'
alias r='reboot'
fi
##############
### Prompt ###
##############
if is_zsh; then
# Enable $(cmd) in $PS1 etc.
setopt prompt_subst
fi
displaytime() {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d days ' $D
(( $H > 0 )) && printf '%d hours ' $H
(( $M > 0 )) && printf '%d minutes ' $M
(( $D > 0 || $H > 0 || $M > 0 )) && printf 'and '
printf '%d seconds\n' $S
}
LAST_CMD_START_SECONDS=0
LAST_EXECUTED_CMD=""
CURRENT_SESSION_WINDOW_ID=""
update_terminal_title() {
echo -ne "\033]0;${PWD} 🚀 ${LAST_EXECUTED_CMD}\007"
# In tmux window too (see allow-rename in `man tmux`)
if ! [ -z "$TMUX" ]; then
# echo -ne "\ek${PWD} 🚀 ${LAST_EXECUTED_CMD}\e\\"
true
fi
}
# Before executig command (ZSH only)
preexec() {
LAST_CMD_START_SECONDS="$SECONDS"
LAST_EXECUTED_CMD="$1"
update_terminal_title
if [ -z "$CURRENT_SESSION_WINDOW_ID" ] && xdotool_available; then
CURRENT_SESSION_WINDOW_ID=$(xdotool getwindowfocus)
fi
}
prompt_green() {
if is_zsh; then
echo "%F{green}$@%f"
else
echo -e "\e[1;32m$@\e[0m"
fi
}
prompt_red() {
if is_zsh; then
echo "%F{red}$@%f"
else
echo -e "\e[1;31m$@\e[0m"
fi
}
prompt_blue() {
if is_zsh; then
echo "%F{blue}$@%f"
else
echo -e "\e[1;34m$@\e[0m"
fi
}
# Before showing PROMPT
precmd() {
# Capture exit code of last command
local ex=$?
update_terminal_title
local spent_time_in_seconds=$(expr $SECONDS - $LAST_CMD_START_SECONDS)
local spent_time_if_too_long=""
if is_zsh && [ $spent_time_in_seconds -gt 5 ] && ! [ -z $LAST_EXECUTED_CMD ]; then
spent_time_if_too_long="\"${LAST_EXECUTED_CMD}\" took $(displaytime $spent_time_in_seconds)"
fi
if is_zsh &&
xdotool_available &&
! [ -z "$LAST_EXECUTED_CMD" ] &&
[ "$(xdotool getwindowfocus)" -ne "$CURRENT_SESSION_WINDOW_ID" ] &&
command -v notify-send > /dev/null; then
local notif_body="Finished successfully"
if [ "$ex" -ne 0 ]; then
local notif_body="Exited with exit code: $ex"
fi
# notify-send "$LAST_EXECUTED_CMD" "$notif_body"
fi
LAST_EXECUTED_CMD=""
local prev_exit_command=""
if [ "$ex" -ne 0 ]; then
prev_exit_command=$(prompt_red "Exit Code: $ex ")
fi
if is_zsh; then
local current_path="%~"
local spent_time_prompt=$(prompt_green $spent_time_if_too_long)
if [ "$ex" -ne 0 ]; then
local spent_time_prompt=$(prompt_red $spent_time_if_too_long)
fi
RPROMPT=$(join_to_string " " $spent_time_prompt)
else
local current_path="\w"
fi
if [ ! -z "$VIRTUAL_ENV" ]; then
local python_env="($(realpath --relative-to=$PWD $VIRTUAL_ENV)) "
fi
tmux_status=$([ -z "$TMUX" ] && prompt_red "NOT A TMUX SESSION " || prompt_blue "$(tmux display-message -p '#S') ")
PS1="${PROMPT_LABEL}${python_env}${prev_exit_command}$(prompt_green "${current_path} \$") "
}
if ! is_zsh; then
export PROMPT_COMMAND=precmd
fi