summaryrefslogtreecommitdiff
path: root/zsh/.zshrc
blob: 6d013c6164f16d823690de1a336fd6af0dbc0525 (plain)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# #############################################################################
# zsh
# #############################################################################

autoload -Uz compinit promptinit
fpath=(~/.zsh/completions $fpath)
compinit -u
compctl -K tln muxa
compdef mosh=ssh
promptinit

HISTFILE=~/.zshistory
HISTSIZE=99999
SAVEHIST=99999
MAILCHECK=0 # uberspace
LISTMAX=0

setopt completealiases
setopt complete_in_word
setopt no_multios
setopt nobgnice
setopt inc_append_history
setopt share_history
setopt no_beep

bindkey -v
bindkey '^R' history-incremental-search-backward
bindkey '^A' vi-beginning-of-line
bindkey '^E' vi-end-of-line

zstyle ':completion:*' completer _complete _ignored
zstyle ':completion:*' menu select=2
zstyle :compinstall filename '~/.zshrc'
zstyle '*' hosts # do not use /etc/hosts

ZDIR=~/.zsh/
_HOST_TYPE=$(uname)

_HOSTNAME=$(timeout --preserve-status 0.1 hostname -f 2>/dev/null)
if [[ $? != 0 ]]; then
  _HOSTNAME=$HOST # $HOST is set by zsh
fi

_USERNAME=$(whoami)

# #############################################################################
# ssh-agent
# #############################################################################

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
  echo "Initialising new SSH agent..."
  /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
  echo succeeded
  chmod 600 "${SSH_ENV}"
  . "${SSH_ENV}" > /dev/null
  /usr/bin/ssh-add
}


if [[ $_USERNAME != "root" ]]; then
  if [[ $_HOST_TYPE != "OpenBSD" ]]; then
    if [ -f "${SSH_ENV}" ]; then
      . "${SSH_ENV}" > /dev/null
      ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
          start_agent
      }
    else
      start_agent;
    fi
  fi
fi

# #############################################################################
# functions
# #############################################################################

diff ()
{
  if [[ -x $(which colordiff 2>/dev/null) ]]; then
    colordiff "$@"
  else
    command diff --color=auto "$@"
  fi
}

irb ()
{
  which pry &>/dev/null
  if [[ $? -eq 0 ]]; then
    pry "$@"
  else
    command irb "$@"
  fi
}

lsof_kill ()
{
  lsof $1 | grep -v COMMAND | cut -d " " -f 2- | strips | cut -d " " -f 1 | xargs kill
}

tln ()
{
  reply=( $(tmux list-sessions | cut -d: -f1) )
}

watch_pdflatex ()
{
  ls $@ | entr pdflatex --interaction=batchmode $1
  echo "tell application \"Preview\" to activate" | osascript -
  echo "tell application \"iTerm2\" to activate" | osascript -
}

pdfprevfile ()
{
  pdftoppm $1 -f 1 -l 1 -singlefile -png
}

getcert ()
{
  openssl s_client -showcerts -connect $1 <dev/null > $2
}

on_gcloud () {
   curl metadata.google.internal -i -m 1 &>/dev/null
   if [[ $? -eq 0 ]]; then
     echo "yes"
   else
     echo "no"
   fi
}

on_aws () {
  if [[ $(echo $_HOSTNAME | cut -d. -f 2) == "ec2" ]]; then
    echo "yes"
  else
    echo "no"
  fi
}

function chpwd
{
  if [[ -e $ZDIR/functions ]] {
    source ~/.zsh/functions/rtab.zsh
    if [[ $(pwd) == $HOME ]]; then
      psvar[1]=$(print -P %~)
    else
      psvar[1]=$(rtab)
    fi
  } else {
    psvar[1]=$(print -P %~)
  }
}
chpwd
zstyle ':prompt:rtab' fish yes
zstyle ':prompt:rtab' nameddirs yes

# #############################################################################
# exports
# #############################################################################

# https://consoledonottrack.com/
export DO_NOT_TRACK=1

if [[ $_USERNAME == "root" ]]; then
  umask 0022
else
  umask 0077
fi

export PATH=~/.local/bin:~/.local/sbin:$PATH
export PATH=/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH

export MANPATH=$MANPATH:$HOME/.local/share/man

export GOPATH=$HOME/.local/lib/pkg/go

function maybe_add_dir_to_path {
  if [[ -d $1 ]]; then
    export PATH=$1:$PATH;
  fi
}

maybe_add_dir_to_path ~/local_bin
maybe_add_dir_to_path /home/pks/local_bin
maybe_add_dir_to_path ~/.sync/local_bin
maybe_add_dir_to_path ~/.sync/sync/local_bin
maybe_add_dir_to_path /home/pks/.sync/local_bin
maybe_add_dir_to_path ~/nlp_scripts
maybe_add_dir_to_path /home/pks/nlp_scripts
maybe_add_dir_to_path ~/.sync/nlp_scripts
maybe_add_dir_to_path /home/pks/.sync/nlp_scripts
maybe_add_dir_to_path /usr/lib/colorgcc/bin
maybe_add_dir_to_path /opt/local/bin
maybe_add_dir_to_path /opt/local/sbin

which R &>/dev/null
if [[ $? == 0 ]]; then
  maybe_add_dir_to_path $HOME/.R
fi

export EDITOR='vim -p'
alias qmv='qmv -evim -fdo'

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

export RUBYOPT=-Ku
export GEM_HOME=$HOME/.local/lib/ruby/
export GEM_PATH=$HOME/.local/lib/ruby/:$GEM_PATH

export PYTHONPATH=$PYTHONPATH:~/.local/lib/python/site-packages:~/.local/lib/python3/site-packages
export PATH=~/.local/lib/python/site-packages:$PATH

export PATH=~/.local/lib/node/bin:$PATH

export TEXMFHOME='~/.texmf'

export CFLAGS="-march=native -mtune=native"
export CXXFLAGS="$CFLAGS"
export CCACHE_PATH=/usr/bin


case $_HOST_TYPE in
  Linux)
    case $_USERNAME in
      root)
        export PATH=/home/pks/.local/bin:$PATH
        ;;
    esac
  ;;
esac

function source_something {
  if [[ -f $1 ]]; then
    source $1;
  fi
}

source_something ~/.zshrc.personal
source_something ~/.zshrc.work
source_something ~/.local/src/zoxide/zoxide.plugin.zsh
export MCFLY_KEY_SCHEME=vim
export MCFLY_FUZZY=true
export MCFLY_RESULTS=48
source_something /usr/share/mcfly/mcfly.zsh

# #############################################################################
# terminal
# #############################################################################

case $TERM in
  screen-256color) ;;
  rxvt)
    TERM=rxvt-256color
  ;;
  *)
    TERM=xterm-256color
  ;;
esac

stty -ixon # enable mapping of C-s in vim..

# #############################################################################
# prompt
# #############################################################################

autoload -U colors && colors

# possible colors: black, white, green, cyan, red, yellow, magenta
case $_HOSTNAME in
  *.home|barkley*|bogues*|carter*|durant*|harden*|james*|ming*|mutombo*|nash*|oneal*|robinson*)
    _HOSTCOLOR_BG=magenta
    _HOSTCOLOR_FG=black
  ;;
  *.uberspace.de|*.in-berlin.de|*-hdh)
    _HOSTCOLOR_BG=green
    _HOSTCOLOR_FG=black
    alias tmux="tmux -f ~/.tmux/uberspace.conf"
  ;;
  *)
    _HOSTCOLOR_BG=black
    _HOSTCOLOR_FG=white
    alias tmux="tmux -f ~/.tmux/default.conf"
    # google cloud
    if [[ $(on_gcloud) == "yes" ]]; then
      _HOSTCOLOR_BG=blue
      _HOSTCOLOR_FG=white
      alias tmux="tmux -f ~/.tmux/gcloud.conf"
    # aws
    elif [[ $(on_aws) == "yes" ]]; then
      _HOSTCOLOR_BG=yellow
      _HOSTCOLOR_FG=black
      alias tmux="tmux -f ~/.tmux/aws.conf"
    fi
  ;;
esac

function chpwd
{
  if [[ -e $ZDIR/functions ]] {
    source ~/.zsh/functions/rtab.zsh
    if [[ $(pwd) == $HOME ]]; then
      psvar[1]=$(print -P %~)
    else
      psvar[1]=$(rtab)
    fi
  } else {
    psvar[1]=$(print -P %~)
  }
}
chpwd
zstyle ':prompt:rtab' fish yes
zstyle ':prompt:rtab' nameddirs yes

case $_USERNAME in
  root)
    PROMPT="%{$bg[red]%}%{$fg[black]%}%m:%1v#%{$reset_color%} "
  ;;
  *)
    PROMPT="%{$bg[$_HOSTCOLOR_BG]%}%{$fg[$_HOSTCOLOR_FG]%}%m%{$reset_color%}:%1v> "
  ;;
esac

case $TERM in
  *256color*)
     precmd () {print -Pn "\e]0;%n@%m%~\a"}
    preexec () {print -Pn "\e]0;%n@%m%~ -- $1\a"}
  ;;
esac

# #############################################################################
# aliases
# #############################################################################

case $_HOST_TYPE in
  OpenBSD)
    alias ls='colorls -G'
  ;;
  Darwin)
    alias ls='ls -h -G'
    alias grep='ggrep'
    export LD_LIBRARY_PATH=$HOME/.local/lib:$LD_LIBRARY_PATH
    if [[ -d /Library/TeX/texbin/ ]]; then
      export PATH=/Library/TeX/texbin:$PATH
    fi
    alias afk="/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend"
  ;;
  Linux)
    alias ls='ls -h --color=auto'
    export LD_LIBRARY_PATH=$HOME/.local/lib:$HOME/.local/lib64:$LD_LIBRARY_PATH
  ;;
esac

alias d='z'
alias l='ls'
alias la='ls -lA'
alias ll='ls -l'
alias lla='ls -A'
alias lsc='ls -1 | wc -l'
alias less='less -R'
alias lh='ls --color=always -lasth | less'

alias equery='sudo equery -N'
alias feh='feh -F --auto-rotate'
alias git='git -c color.status=always'
if [[ $_HOST_TYPE != "OpenBSD" ]]; then
  alias grep='grep --color=auto'
fi
alias pdf='zathura'
alias py='bpython'
alias vi=$EDITOR
alias jsonlint='python -mjson.tool'
alias jpegtran='jpegtran -trim'
alias pdfmerge='gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=/tmp/merged.pdf -dBATCH'
alias thumb='convert -resize 200x200'
alias radio='mocp ~/.moc/radio.m3u'

alias muxn='tmux new -s'
alias muxl='tmux list-sessions'
alias muxa='tmux attach -t'

alias easy_install_home='easy_install --install-dir=$HOME/.local/lib/python/site-packages'
alias pip_install_home='pip3 install --prefix $HOME/.local'
alias npm_install_home='npm install -g --prefix ~/.local/lib/node'
alias gem_install_home='gem install --install-dir=$HOME/.local/lib/ruby --bindir=$HOME/.local/bin'

if [[ $_HOST_TYPE == "Linux" ]]; then
  alias cal='while true; do clear; echo; khal calendar; sleep 600; done'
  alias wifi_list='sudo iwlist wlan0 scan'
  alias always_on='xset dpms 0 0 0'
  case $_HOSTNAME in
    bogues*)
      alias vgaoff='xrandr --output VGA1 --off && xrandr --output LVDS1 --mode 1024x768'
      alias vgaclone='xrandr --output VGA1 --mode 1024x768'
      ;;
  esac
  alias vgaext='xrandr --output VGA1 --mode 1024x768 --left-of LVDS1 --rotate right'
  alias vga21='xrandr --output VGA1 --mode 1600x1200 --rate 60 --rotate left && xrandr --output LVDS1 --off'
  alias reboot='sudo shutdown -r now'
  alias halt='sudo shutdown -h now'
  alias lp2p='/usr/bin/lp -o sides=two-sided-long-edge'
  alias lp4p='/usr/bin/lp -o sides=two-sided-short-edge -o number-up=2'
  alias lp8p='/usr/bin/lp -o sides=two-sided-short-edge -o number-up=4 -o landscape'
  alias lpimg='/usr/bin/lp -o fit-to-page'
fi