-
Notifications
You must be signed in to change notification settings - Fork 3
/
myterminalrc
271 lines (238 loc) · 9.3 KB
/
myterminalrc
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
#!/usr/bin/env bash
# Bash/ZSH custom configurations
# vim: ft=sh syntax=sh
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
# === Simple custom aliases
if hash nvim 2>/dev/null; then
alias vim="nvim"
export EDITOR=nvim
export VISUAL=nvim
else
export EDITOR=vim
export VISUAL=vim
fi
alias e='$EDITOR'
alias v='$VISUAL'
alias path='echo $PATH | tr -s ":" "\n"'
#alias ls="ls -FG --color=auto"
alias rsync2="rsync -avz --delete --progress"
alias sshProxySocks="ssh -CNf -D 1234"
alias gitsearch="git rev-list --all | xargs git grep -F"
# eg: keygen -f ~/.ssh/id_foo
alias keygen='ssh-keygen -t rsa -b 4096 -C ""'
rgl() { rg -p "$@" | less -XFR ; }
ssh_copy_pub() { ssh "$@" "set -x; mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys" < "$HOME/.ssh/id_rsa.pub"; }
generate_password() {
tr -dc 'a-zA-Z0-9!@#$%\-_+~' < /dev/urandom | fold -w "$@" | head -n 1
}
generate_password2() {
openssl rand -base64 1024 | tr -d /=+ | cut -c -"$*"
}
# or use apg
# check strongeness with https://dl.dropboxusercontent.com/u/209/zxcvbn/test/index.html
alias shortcuts="stty -a | awk 'BEGIN{RS=\"[;\n]+ ?\"}; /= ..$/'"
alias g='git'
alias gc='git commit'
alias gp='git push'
alias gpd='git push --dry-run'
alias gpf='git push --force-with-lease --force-if-includes'
alias gup='git pull --rebase'
alias gupv='git pull --rebase --verbose'
alias gupa='git pull --rebase --autostash'
alias gupav='git pull --rebase --autostash --verbose'
alias gupom='git pull --rebase origin $(git_main_branch)'
alias gupomi='git pull --rebase=interactive origin $(git_main_branch)'
# === tmux aliases
alias tmn="tmux new -s \$(basename \$(pwd))"
alias tml="tmux list-sessions"
tma() {
tmux attach-session -t "$@" || tmux new-session -s "$@"
}
is_linux () {
[ ! -f /sbin/launchd ] && [ "$(uname -s)" = "Linux" ]
}
is_osx () {
#[[ $('uname') == 'Darwin' ]]
# [ "$OSTYPE" =~ darwin* ]
[ -f /sbin/launchd ]
}
# === osx specific
if is_osx; then
if [[ -z $TMUX ]]; then
HOMEBREW_PREFIX=/usr/local # or brew --prefix
export PATH="$HOMEBREW_PREFIX/bin:$HOMEBREW_PREFIX/sbin:$PATH"
for d in \
"$HOMEBREW_PREFIX"/opt/*/libexec/gnubin \
"$HOMEBREW_PREFIX"/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin \
; do export PATH=$d:$PATH; done
for d in \
/usr/local/opt/*/libexec/gnuman \
; do export MANPATH=$d:$MANPATH; done
fi
#alias grep="grep --color=always"
#alias egrep="egrep --color=always"
alias swap_off="sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist"
alias swap_on="sudo launchctl load /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist"
alias swap_rm="sudo rm /private/var/vm/swapfile*"
# python/virtualenv setup, use workon
#[ -s "/usr/local/bin/python" ] && export VIRTUALENVWRAPPER_PYTHON="/usr/local/bin/python"
#[ -s "/usr/local/bin/virtualenvwrapper_lazy.sh" ] && . "/usr/local/bin/virtualenvwrapper_lazy.sh"
quick_look() {
(( $# > 0 )) && qlmanage -p $* &>/dev/null &
}
# Open the current directory in a Finder window
alias ofd='open_command $PWD'
fi
# === linux specific
if is_linux; then
[ "$TMUX" = "" ] && export TERM="xterm-256color"
# this needs ncurses-term installed
[ "$TMUX" = "" ] && [ "$COLORTERM" = "gnome-terminal" ] && [[ $TERM =~ xterm.* ]] && export TERM="gnome-256color"
# https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/clipboard.zsh
alias pbcopy="xclip -selection clipboard"
alias pbpaste="xclip -selection clipboard -o"
if [ -s "$HOME/.linuxbrew/bin" ]; then
export PATH="$HOME/.linuxbrew/bin:$PATH"
export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH"
export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"
fi
hash xcape 2>/dev/null && xcape -e "Control_L=Escape"
fi
# === dev utilities
alias curl_response_time='curl -w "@$HOME/.dotfiles/curltime.txt" -o /dev/null -s '
# === JVM, Java, Scala, etc
alias java_ls="/usr/libexec/java_home -Vq 2>&1 | grep -E '\d\.\d\.\d(_\d+)?.*,' | cut -d , -f 1 | cut -c 5-"
# https://www.jetbrains.com/help/idea/2017.1/getting-started-with-sbt.html#edit_sbt_project_settings
#export SBT_OPTS="-Xmx2G"
sbtf8() {
# http://www.scala-sbt.org/1.x/docs/Howto-Interactive-Mode.html#Run+commands+before+entering+interactive+mode
JAVA_HOME=$(/usr/libexec/java_home -v 11) PATH=$JAVA_HOME/bin:$PATH sbt "onFailure shell" "$@" shell
}
sbtf11() {
JAVA_HOME=$(/usr/libexec/java_home -v 11) PATH=$JAVA_HOME/bin:$PATH sbt "onFailure shell" "$@" shell
}
# === docker aliases
# more here: https://www.commandlinefu.com/commands/tagged/2647/docker
dockercleanimagesold(){
docker images --no-trunc --format '{{.ID}} {{.CreatedSince}} {{.Tag}}' \
| grep -e ' months' -e '[2-9]* weeks' -e 'DIRTY' | awk '{ print $1 }' \
| xargs -t docker rmi
}
dockercleanrepo(){
docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}}' \
| grep "$@" | awk '{ print $1 }' | xargs -t docker rmi
}
dockercleanrepoforce(){
docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}}' \
| grep "$@" | awk '{ print $1 }' | xargs -t docker rmi --force
}
alias dockerkillall="docker ps -q | xargs docker kill"
# alias to extract files
extract () {
set -x
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "don't know how to extract '$1'..." ;;
esac
else
echo "'$1' is not a valid file!"
fi
set +x
}
# shows example section of man pages
eg(){
MAN_KEEP_FORMATTING=1 man "$@" 2>/dev/null \
| sed --quiet --expression='/^E\(\x08.\)X\(\x08.\)\?A\(\x08.\)\?M\(\x08.\)\?P\(\x08.\)\?L\(\x08.\)\?E/{:a;p;n;/^[^ ]/q;ba}' \
| ${MANPAGER:-${PAGER:-pager -s}}
}
tldr() { curl "https://raw.githubusercontent.com/tldr-pages/tldr/master/pages/common/$1.md"; }
# === fzf config
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules,target,venv,.venv}/*" 2> /dev/null'
export FZF_DEFAULT_OPTS="--extended --cycle"
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_CTRL_T_OPTS="
--preview '(bat --style=numbers,changes,header --color=always --theme=base16 --line-range :500 {} || cat {} || tree -C {}) 2> /dev/null | head -200'
--bind 'ctrl-e:execute(nvim {} < /dev/tty > /dev/tty 2>&1)' > selected
--bind 'ctrl-p:toggle-preview'
--bind 'ctrl-b:execute(bat {})'
--bind 'ctrl-v:execute(code {+})'
--bind 'ctrl-l:execute(qlmanage -p {+})'
"
export FZF_ALT_C_OPTS="--preview 'tree -C {} | head -200'"
## === fzf aliases
fshow() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
tm() {
local session
newsession=${1:-new}
session=$(tmux list-sessions -F "#{session_name}" | \
fzf --query="$1" --select-1 --exit-0) &&
tmux attach-session -t "$session" || tmux new-session -s "$newsession"
}
# Load NVM, if available
#[[ -s "$HOME/.nvm/nvm.sh" ]] && source "$HOME/.nvm/nvm.sh" # This loads nvm
# Loads jEnv, if available
#[[ -s "$HOME/.jenv/bin" ]] && export PATH="$HOME/.jenv/bin:$PATH" && eval "$($HOME/.jenv/bin/jenv init -)"
pyenv_activate() {
command -v pyenv 1>/dev/null 2>&1 && eval "$(pyenv init - --no-rehash)" # pyenv adds ~150 ms to the startup :(
#eval "$(pyenv init -)"
#eval "$(pyenv virtualenv-init -)"
}
load_base16_theme() {
export BASE16_THEME="$1"
echo "colorscheme base16-$BASE16_THEME" > ~/.vimrc_background
BASE16_SHELL=$ZGEN_DIR/chriskempson/base16-shell-master/
if [[ -d $BASE16_SHELL ]]; then
source "$BASE16_SHELL/scripts/base16-$BASE16_THEME.sh"
fi
}
#load_base16_theme tomorrow-night
alias yaml2js="python -c 'import sys, yaml, json; json.dump(yaml.safe_load_all(sys.stdin), sys.stdout, indent=4)'"
y2j() {
python -c "import yaml, json, sys; [sys.stdout.write(json.dumps(d, sort_keys=True, indent=2, default=lambda x:str(x))) for d in yaml.safe_load_all(sys.stdin)]"
}
j2y() {
python -c 'import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))'
}
cachedcall() {
# Cache slow network calls output into a file for one hour
# Cache can be cleared by removing cache file
# When used a message is emitted to stderr
# Uses find and chsum (part of coreutils)
local fp
mkdir -p /tmp/cachedcall/
fp=/tmp/cachedcall/$(echo "$@" | cksum | cut -f1 -d' ')
if [[ $(find "$fp" -mmin -60 -print 2> /dev/null) ]]; then
>&2 echo -e "\033[0;33mUsing cached result for \`$*\` from $fp\033[0m"
cat "$fp"
else
"$@" | tee "$fp"
fi
}
# show some quote when opening the terminal
[[ -x /usr/games/fortune ]] && [[ -f ~/quotes.dat ]] && /usr/games/fortune ~/quotes
pyenv_activate
if [[ -z $TMUX ]]; then
# set PATH so it includes user's private bin if it exists
PATH="$HOME/bin:$HOME/.local/bin:$HOME/.dotfiles/bin:$PATH"
fi