~tomterl/zshorg

ref: 2566f07de7bf8361e9241e68afed12875e8d7afb zshorg/zshrc.org -rw-r--r-- 26.0 KiB
2566f07d — Tom Regner Add org export options 1 year, 7 months ago
                                                                                
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
* Introduction
:LOGBOOK:
- Note taken on [2012-01-30 Mo 11:53] \\
  tangle to the correct  locations; use logbook drawer
- Note taken on [2012-01-29 So 01:15] \\
  Git functions added; prompt added; html export style
:END:

  This is a literate programming zsh configuration written in
  org-mode/org-babel.
    
** History
   Up until January 2012 I used [[https://github.com/robbyrussell/oh-my-zsh][oh-my-zsh]] to configure my shell
   environment. It contains a lot of good ideas and a plethora of tools
   but, it also contains many errors and small mistakes; the code-quality
   overall is lousy.

   After discovering the [[https://github.com/eschulte/emacs24-starter-kit][emacs starter kit]] I thought a lot about a good
   project to start to learn about and use [[http://orgmode.org/worg/org-contrib/babel/][org-babel]] for a literate
   programming adventure.

   I'm usualy slow to take action, considering a lot, thinking ahead as
   many paths as I can -- think of. Since, as of january 3d, oh-my-zsh
   get's on my nerves for a couple of weeks now instead of making my
   day, taking on my first LP project wasn't such a long process after all.

   The first version considered complete enough to replace oh-my-zsh,
   finaly ridden of the command substitution bug, was finished
   <2012-01-10 Tue 13:21>.
** =org-mode= configuration                                   :ignoreExport:
#+TITLE: zsh Configuration
#+STARTUP: overview
#+STARTUP: logdone
#+STARTUP: indent
#+PROPERTY: padline yes
#+PROPERTY: LOG_INTO_DRAWER LOGBOOK
#+EXPORT_EXCLUDE_TAGS: ignoreExport
#+OPTIONS:   H:3 num:t toc:t \n:nil @:t ::t |:t ^:nil -:t f:t *:t <:t
#+OPTIONS:   TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
#+INFOJS_OPT: view:nil toc:t ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js
#+STYLE: <link rel="stylesheet" type="text/css" href="http://orgmode.org/org-manual.css" />
* =zshrc=
#+BEGIN_SRC sh :tangle ~/.zshrc :noweb tangle :exports code
  <<variables>>
  <<modules>>
  <<aliases>>
  <<functions>>
  <<options>>
  <<gnuglobal>>
  <<keybindings>>
  <<command_completion>>
  <<commandline_editing>>
  <<history>>
  <<dired>>
  <<hooks>>
  <<terminal>>
  <<prompt>>
  <<groovy>>
  <<git>>
  <<packages>>
#+END_SRC 

** Variables
:PROPERTIES:
:header-args: :noweb-ref variables
:header-args: :padline yes
:END:

*** Emacs setup

On different systems emacs 24 will be installed in different
locations; I set =EMACS_BASE= here; aliases etc. later will use it.

#+BEGIN_SRC sh 
  if [ -z "${EMACS_BASE}" ]; then
      EMACS_BASE="/usr/local"
      export EMACS_BASE
  fi
#+END_SRC
**** Emacs cask
#+BEGIN_SRC sh
  # emacs cask package/dev managment
  export PATH="$HOME/.cask/bin:$PATH"
#+END_SRC

*** Coloring for standard commands
Use =green= for matches, not =red=.
#+BEGIN_SRC sh 
  export GREP_COLORS="mt=01;32:ms=01;32:mc=01;32:sl=:cx=:fn=35:ln=32:bn=32:se=36"
#+END_SRC

*** Pager
#+BEGIN_SRC sh  
  export PAGER='less'
#+END_SRC

*** Characterset
#+BEGIN_SRC sh  
  export LC_CTYPE=$LANG
#+END_SRC

*** Path/Fpath
Newest GNU R in ~/opt
#+BEGIN_SRC sh 
  path=( ~/opt/bin $path )
#+END_SRC

Haskell development.
#+BEGIN_SRC sh
  path=( ~/.cabal/bin $path )
#+END_SRC
This should always be the last part of =path= tinkering, as I'm used
to be able to override any executable by putting a replacement or
wrapper in my =~/bin=.
#+BEGIN_SRC sh  
  path=( ~/bin $path )
#+END_SRC
Finaly adjust the =fpath=; a lot of the stuff following is tangled
into files autoloadable, and I use ~wd~ for easier filesystem navigation
#+BEGIN_SRC sh 
  fpath=( ~/bin/wd.zsh $fpath)
  fpath=( ~/.zsh.d/functions $fpath )
#+END_SRC
**** ~wd~ function
#+BEGIN_SRC sh
  wd() {
      . ~/bin/wd.zsh/wd.sh
  }
#+END_SRC
#
**** Ruby
***** =rvm=
#+BEGIN_SRC sh
  # Add RVM to PATH for scripting
  PATH=$PATH:$HOME/.rvm/bin
#+END_SRC

*** Bashlibs
#+BEGIN_SRC sh  
  export BASH_LIBS_DIR=~/lib/bash
#+END_SRC

*** Editor
#+BEGIN_SRC sh  
  export EDITOR="${EMACS_BASE}/bin/emacsclient -nw -c"
#+END_SRC

*** Java and tiling WMs
#+BEGIN_SRC sh
  export _JAVA_AWT_WM_NONREPARENTING=1
#+END_SRC
*** Vagrant
#+BEGIN_SRC sh
  export WEBDEV_ROOT="~/Projekte/devserver/"
#+END_SRC
*** Arduino / Kaleidoscope
#+BEGIN_SRC sh
  export ARDUINO_PATH=/usr/share/arduino
  export SKETCHBOOK_DIR=${HOME}/Arduino
#+END_SRC
*** Desktop Keyring variables
#+BEGIN_SRC sh
export $( dex /etc/xdg/autostart/gnome-keyring-ssh.desktop )
#+END_SRC
** Modules
:PROPERTIES:
:header-args: :noweb-ref modules
:END:

*** =colors=
#+BEGIN_SRC sh  
  autoload colors; colors
#+END_SRC

** Aliases
:PROPERTIES:
:header-args: :noweb-ref aliases
:END:
*** Coloring for standard commands
A few standard aliases to deal with colored output in terminals.
#+BEGIN_SRC sh  
  [ ! -L ~/bin/ls ] && alias ls='/opt/coreutils/bin/ls --color=auto'
  alias grep='grep --color=auto'
  alias less='less -R'
#+END_SRC
*** Emacs
My emacs installation is sometimes in special locations; the aliases
defined here reflect that.

#+BEGIN_SRC sh  
  alias emacs="${EMACS_BASE}/bin/emacs"
  alias emc="${EMACS_BASE}/bin/emacsclient -c -nw"  
#+END_SRC
*** Desktop
I use awesome and don't want =nautilus= to take over my desktop. 
Defining this alias prevents the =nautilus= started by
=gnome-session= to automaticaly take over.

#+BEGIN_SRC sh 
  alias nautilus='nautilus --no-desktop'  
#+END_SRC
*** vagrant ssh
#+BEGIN_SRC sh
  alias vssh="pushd ${WEBDEV_ROOT} && docker exec -it devserver /bin/bash; popd"
#+END_SRC
*** tmux update-env 
#+BEGIN_SRC sh
  alias tue="tmux update-environment"
#+END_SRC
*** system log with lnav
#+BEGIN_SRC sh
  alias syslog="journalctl -f -o json | lnav"
#+END_SRC
** Functions
:PROPERTIES:
:header-args: :noweb-ref functions
:END:
*** Archives
**** Extract
#+BEGIN_SRC sh  
  function extract() {
      unset REMOVE_ARCHIVE
      
      if test "$1" = "-r"; then
          REMOVE_ARCHIVE=1
          shift
      fi
      if [[ -f $1 ]]; then
          case $1 in
              *.tar.bz2) tar xvjf $1;;
              *.tar.gz) tar xvzf $1;;
              *.tar.xz) tar xvJf $1;;
              *.tar.lzma) tar --lzma -xvf $1;;
              *.bz2) bunzip $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 "'$1' cannot be extracted via >extract<";;
          esac
  
          if [[ $REMOVE_ARCHIVE -eq 1 ]]; then
              echo removing "$1";
              /bin/rm "$1";
          fi
  
      else
          echo "'$1' is not a valid file"
      fi
  }  
#+END_SRC

*** Source bash scripts
#+BEGIN_SRC sh  
  bash_source() {
      alias shopt=':'
      alias _expand=_bash_expand
      alias _complete=_bash_comp
      emulate -L sh
      setopt kshglob noshglob braceexpand
      
      source "$@"
  }
#+END_SRC

*** Date from unix timestamp
#+BEGIN_SRC sh 
  function fromut() {
      echo `perl -e "print scalar(localtime(${1})),\"\n\";"`
  }
#+END_SRC


*** SSH host keys
#+BEGIN_SRC sh
  function remove_host_keys() {
      host="${1}"
      if [ -n "${host}" ]; then
          ssh-keygen -f "${HOME}/.ssh/known_hosts" -R "${host}"
      else
          echo "remove_host_keys {IP|HOST}" > /dev/stderr
      fi
  }
#+END_SRC
*** ~phpunit~
#+BEGIN_SRC sh
  function phpunit() {
      command phpunit ${@}
      printf '\xb10m'
  }
#+END_SRC
*** Compress jpeg-Files via ~convert~
#+BEGIN_SRC sh
  function compress_jpeg() {
      for pic in "${@}"; do 
          convert -strip -interlace Plane -gaussian-blur 0.05 -quality 85% "${pic}" "${pic}"
      done
  }
#+END_SRC
*** Check for null-bytes
#+BEGIN_SRC sh
  function check_zero_bytes() {
      status=0
      for arg in "${@}"; do
          perl -ne '/\000/ and print' "${arg}"
          status=1
      done
      return $status
  }
#+END_SRC
** Options
:PROPERTIES:
:header-args: :noweb-ref options
:END:
#+BEGIN_SRC sh 
  setopt extended_glob \
      glob_complete \
      function_arg_zero \
      hash_cmds \
      hash_dirs \
      multios \
      re_match_pcre
#+END_SRC

** Keybindings
:PROPERTIES:
:header-args: :noweb-ref keybindings
:END:
*** Emacs style keybindings!
#+BEGIN_SRC sh  
  bindkey -e
#+END_SRC
** Command completion
:PROPERTIES:
:header-args: :noweb-ref command_completion
:END:
*** =compinit=
#+BEGIN_SRC sh  
  autoload -Uz compinit; compinit
#+END_SRC

*** Options
   #+BEGIN_SRC sh 
     unsetopt menu_complete \
         flowcontrol
     
     setopt auto_menu \
         complete_in_word \
         always_to_end
   #+END_SRC
*** =complist= module
   #+BEGIN_SRC sh  
     zmodload -i zsh/complist
   #+END_SRC

*** .zshrc
#+BEGIN_SRC sh  
  zstyle :compinstall filename "$HOME/.zshrc"
#+END_SRC

*** Bashcompletion
#+BEGIN_SRC sh  
  autoload -Uz bashcompinit; bashcompinit
#+END_SRC

*** Case sensitive, partial word and substring completion
   #+BEGIN_SRC sh    
     zstyle ':completion:*' matcher-list 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
   #+END_SRC
*** Colors/Menu
   #+BEGIN_SRC sh  
     zstyle ':completion:*' list-colors ''     
     zstyle ':completion:*:*:*:*:*' menu select
   #+END_SRC
*** Keybindings   
   #+BEGIN_SRC sh  
     bindkey -M menuselect '^o' accept-and-infer-next-history  
   #+END_SRC
*** Process completion
   Menu of own processes for kill.
   #+BEGIN_SRC sh  
     zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
     zstyle ':completion:*:*:*:*:processes' command "ps -u $(whoami) -o pid,user,comm -w -w"     
   #+END_SRC
*** Hostnames
   #+BEGIN_SRC sh  
     [ -r /etc/ssh/ssh_known_hosts ] && _global_ssh_hosts=(${${${${(f)"$(</etc/ssh/ssh_known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=()
     [ -r ~/.ssh/known_hosts ] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=()
     [ -r /etc/hosts ] && : ${(A)_etc_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}##[:blank:]#[^[:blank:]]#}}} || _etc_hosts=()       
     
     hosts=(
         "$_global_ssh_hosts[@]"
         "$_ssh_hosts[@]"
         "$_etc_hosts[@]"
         `hostname`
         localhost
     )
     zstyle ':completion:*:hosts' hosts $hosts
   #+END_SRC
*** Usernames
   Ignore most system users
   #+BEGIN_SRC sh  
     zstyle ':completion:*:*:*:users' ignored-patterns \
         adm amanda apache avahi beaglidx bin cacti canna clamav daemon \
         dbus distcache dovecot fax ftp games gdm gkrellmd gopher \
         hacluster haldaemon halt hsqldb ident junkbust ldap lp mail \
         mailman mailnull mldonkey mysql nagios \
         named netdump news nfsnobody nobody nscd ntp nut nx openvpn \
         operator pcap postfix postgres privoxy pulse pvm quagga radvd \
         rpc rpcuser rpm shutdown squid sshd sync uucp vcsa xfs
     
     # ... unless we really want to.
     zstyle '*' single-ignored show          
   #+END_SRC
*** Caching
   Some completions need caching (dpkg, ...)
   #+BEGIN_SRC sh  
     zstyle ':completion::complete:*' use-cache 1
     zstyle ':completion::complete:*' cache-path ~/.zsh.d/cache/     
   #+END_SRC   
*** Devserver
#+BEGIN_SRC sh  
  if [[ -r "/usr/share/bash-completion/devserver" ]]; then
      bash_source /usr/share/bash-completion/devserver
  fi  
#+END_SRC

** Commandline editing
:PROPERTIES:
:header-args: :noweb-ref commandline_editing
:END:
*** Jump to beginning/end of line/word
#+BEGIN_SRC sh 
  bindkey "^[[H" beginning-of-line
  bindkey "^[[1~" beginning-of-line
  bindkey "^[OH" beginning-of-line
  bindkey "^[[F"  end-of-line
  bindkey "^[[4~" end-of-line
  bindkey "^[OF" end-of-line
  bindkey "^[[1;5C" forward-word
  bindkey "^[[1;5D" backward-word
#+END_SRC

*** Deletion
#+BEGIN_SRC sh 
  bindkey '^?' backward-delete-char
  bindkey "^[[3~" delete-char
  bindkey "^[3;5~" delete-char
  bindkey "\e[3~" delete-char
  bindkey \^U backward-kill-line
#+END_SRC 
*** Insert the last shell word again
#+BEGIN_SRC sh 
  bindkey "^[m" copy-prev-shell-word
#+END_SRC

*** URLs
#+BEGIN_SRC sh 
  autoload -U url-quote-magic
  zle -N self-insert url-quote-magic
#+END_SRC

*** Options
#+BEGIN_SRC sh  
  setopt no_beep \
      rm_star_wait
#+END_SRC

*** sudo
#+BEGIN_SRC sh 
  run-with-sudo () { LBUFFER="sudo $LBUFFER" }
  zle -N run-with-sudo
  bindkey '^Xs' run-with-sudo  
#+END_SRC 

*** Empty ^I lists directory
#+BEGIN_SRC sh  
  complete-or-list() {
      [[ $#BUFFER != 0 ]] && { zle complete-word ; return 0 }
      echo
      ls
      zle reset-prompt
  }
  zle -N complete-or-list
  bindkey '^I' complete-or-list
#+END_SRC
*** Pipe to
#+BEGIN_SRC sh 
  typeset -Ag abbreviations
  abbreviations=(
      "Il"    "| less"
      "Ia"    "| awk"
      "Ig"    "| grep"
      "Ieg"   "| egrep"
      "Iag"   "| agrep"
      "Ih"    "| head"
      "Ik"    "| keep"
      "It"    "| tail"
      "Is"    "| sort"
      "Iv"    "| ${VISUAL:-${EDITOR}}"
      "Iw"    "| wc"
      "Ix"    "| xargs"
  )
  
  magic-abbrev-expand() {
      local MATCH
      LBUFFER=${LBUFFER%%(#m)[_a-zA-Z0-9]#}
      LBUFFER+=${abbreviations[$MATCH]:-$MATCH}
      zle self-insert
  }
          
  no-magic-abbrev-expand() {
      LBUFFER+=' '
  }
          
  zle -N magic-abbrev-expand
  zle -N no-magic-abbrev-expand
  bindkey " " magic-abbrev-expand
  bindkey "^x " no-magic-abbrev-expand
  bindkey -M isearch " " self-insert
#+END_SRC 

** History
:PROPERTIES:
:header-args: :noweb-ref history
:END:
*** Variables
   #+BEGIN_SRC sh 
     HISTFILE=~/.histfile
     HISTSIZE=10000
     SAVEHIST=10000
   #+END_SRC 
*** Options
#+BEGIN_SRC sh   
  setopt append_history
  setopt extended_history
  setopt hist_expire_dups_first
  setopt hist_ignore_dups
  setopt hist_ignore_space
  setopt hist_verify
  setopt inc_append_history
  setopt share_history
  setopt hist_fcntl_lock 
  setopt hist_ignore_all_dups
#+END_SRC

*** Aliases
   =lh= is short for =load history=, =fc= is a =zsh=-builtin to access
   the history. =-R= rereads the history - in this case from the
   standard history file. =-I= only appends new entries from this file
   to the current active history of the shell process.

   #+BEGIN_SRC sh  
	 alias lh='fc -RI'  
   #+END_SRC
*** Functions
   #+BEGIN_SRC sh  
  function zsh_stats() {
    history | awk '{print $2}' | sort | uniq -c | sort -rn | head
  }  
   #+END_SRC

*** Keybindings
   #+BEGIN_SRC sh  
  bindkey '^r' history-incremental-search-backward
  bindkey "^[[5~" up-line-or-history
  bindkey "^[[6~" down-line-or-history
  bindkey 'OA' up-line-or-search
  bindkey 'OB' down-line-or-search
  bindkey '^P' up-line-or-search
  bindkey '^N' down-line-or-search
   #+END_SRC

** Jobcontrol
*** Options
#+BEGIN_SRC sh  
  setopt long_list_jobs
#+END_SRC

** Autocorrection
I use autocorrection, but define a couple of aliases for commands for
which I don't want correction.
*** Options
#+BEGIN_SRC sh 
  setopt correct \
      correct_all 
  
#+END_SRC
*** Aliases
#+BEGIN_SRC sh 
  alias man='nocorrect man'
  alias mv='nocorrect mv'
  alias mkdir='nocorrect mkdir'
  alias gist='nocorrect gist'
  alias ebuild='nocorrect ebuild'
#+END_SRC

** Directory traversal and manipulation
:PROPERTIES:
:header-args: :noweb-ref dired
:END:
*** Options
#+BEGIN_SRC sh  
  setopt auto_name_dirs
  setopt auto_pushd
  setopt auto_cd
  setopt pushd_ignore_dups
  setopt pushd_silent
#+END_SRC
*** Aliases
#+BEGIN_SRC sh  
  alias ..='cd ..'
  alias cd..='cd ..'
  alias cd...='cd ../..'
  alias cd....='cd ../../..'
  alias cd.....='cd ../../../..'
  alias cd/='cd /'
  
  alias md='mkdir -p'
  alias rd=rmdir
  alias d='dirs -v'  
#+END_SRC 
*** Functions
#+BEGIN_SRC sh  
  cd () {
    if   [[ "x$*" == "x..." ]]; then
      cd ../..
    elif [[ "x$*" == "x...." ]]; then
      cd ../../..
    elif [[ "x$*" == "x....." ]]; then
      cd ../../..
    elif [[ "x$*" == "x......" ]]; then
      cd ../../../..
    else
      builtin cd "$@"
    fi
  }
  function mcd() { 
    mkdir -p "$1" && cd "$1"; 
  }    
#+END_SRC

** Hooks
:PROPERTIES:
:header-args: :noweb-ref hooks
:END:
I use hooks in my prompt setup
#+BEGIN_SRC sh  
  autoload -U add-zsh-hook 
#+END_SRC

** Terminal
:PROPERTIES:
:header-args: :noweb-ref terminal
:END:
*** Colors
#+BEGIN_SRC sh  
  eval $(dircolors)
#+END_SRC

*** Title
In =omz_termsupport_preexec= =$(= and =%= are escaped in the second argument to
=function title()=; this is to prevent multiple execution of the =$()=
command -- prompt substitution as used in =title= (option =-P= to
=print=) would execute the command at least once, and confusion of =%=
tags to commands like =date= with prompt =%= tags.
#+BEGIN_SRC sh 
  function title {
    [ "$DISABLE_AUTO_TITLE" != "true" ] || return
    if [[ "$TERM" == screen* ]]; then
      print -Pn "\ek$1:q\e\\" #set screen hardstatus, usually truncated at 20 chars
    elif [[ "$TERM" == xterm* ]] || [[ $TERM == rxvt* ]] || [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
     print -Pn "\e]2;$2:q\a" #set window name
     print -Pn "\e]1;$1:q\a" #set icon (=tab) name (will override window name on broken terminal)
    fi
  }
  
  ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  
  #Appears when you have the prompt
  function omz_termsupport_precmd {
    title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  }
  
  #Appears at the beginning of (and during) of command execution
  function omz_termsupport_preexec {
    emulate -L zsh
    setopt no_extended_glob
    local CMD_1=${1[(wr)^(*=*|sudo|ssh|-*)]} #cmd name only, or if this is sudo or ssh, the next cmd
    local CMD_2=${2//\$\(/\\\$(} # ')}
    CMD_1=${CMD_1//\%/%%}          
    CMD_2=${CMD_2//\%/%%}          
    #
    title "${CMD_1}" "%100>...>${CMD_2}%<<"
  }
  
  add-zsh-hook precmd  omz_termsupport_precmd
  add-zsh-hook preexec omz_termsupport_preexec
  
#+END_SRC

*** ~tmux~
tmux wrapper that adds new commands to update environment variables in running panes/windows
#+BEGIN_SRC sh
function tmux() {
    case "$1" in
        update-environment|update-env|env-update)
            local v
            command tmux show-environment | while read v; do
                if [[ "$v" == -* ]]; then
                    unset ${v/#-/}
                else
                    # Add quotes around the argument
                    v=${v/=/\=\"}
                    v=${v/%/\"}
                    eval export $v
                fi
            done 
            ;;
        *)
            command tmux "$@"
            ;;
    esac
}
#+END_SRC
** Prompt
:PROPERTIES:
:header-args: :noweb-ref prompt
:END:
*** Module
#+BEGIN_SRC sh 
  autoload -U promptinit; promptinit
#+END_SRC

#+BEGIN_SRC sh
  autoload -Uz vcs_info
  zstyle ':vcs_info:*' enable git svn
  precmd() {
      vcs_info
  }
#+END_SRC

#+BEGIN_SRC sh
  setopt prompt_subst
#+END_SRC 

*** Configure theme

**** Load all the icons...
#+BEGIN_SRC sh
if [ -f ~/.local/share/icons-in-terminal/icons_bash.sh ]; then
	. ~/.local/share/icons-in-terminal/icons_bash.sh
elif [ -f /usr/share/icons-in-terminal/icons_bash.sh ]; then
	. /usr/share/icons-in-terminal/icons_bash.sh
fi
#+END_SRC

**** ~php~ block

Do not show the ~nodejs~ block, show our new shiny php block instead.

#+BEGIN_SRC sh
  BLOX_BLOCK__PHP_SYMBOL="${BLOX_BLOCK__PHP_SYMBOL:-${file_php:-php} }"
  BLOX_BLOCK__PHP_COLOR="${BLOX_BLOCK__PHP_COLOR:-green}"
  function blox_block__php() {
	  if [ -n "$(find_root "$PWD" RoboFile.php)" ]; then
		  PHPV=$(php --version | head -n 1 | cut -d- -f1 | sed 's/PHP //' | cut -d\  -f1)
		  blox_helper__build_block \
			  "${BLOX_BLOCK__PHP_COLOR}" \
			  "${BLOX_BLOCK__PHP_SYMBOL}${PHPV}"
	  fi
  }
export BLOX_SEG__UPPER_RIGHT=( bgjobs php pyenv virtualenv time )
#+END_SRC


** GNU global
:PROPERTIES:
:noweb-ref: gnuglobal
:END:
#+BEGIN_SRC sh
  export GTAGSCONF=/usr/local/share/gtags/gtags.conf
  export GTAGSLABEL=pygments
#+END_SRC

** Groovy
:PROPERTIES:
:noweb-ref: groovy
:END:
#+BEGIN_SRC sh
source ~/.gvm/bin/gvm-init.sh
#+END_SRC
** Git
:PROPERTIES:
:header-args: :noweb-ref git
:END:
*** Prompt 
If there is on thing I had to name, that made me crave for oh-my-zsh
it was the right site prompt stating repository state; here are the
functions I ripped from it...
#+BEGIN_SRC sh  
  function git_prompt_info() {
      ref=$(git symbolic-ref HEAD 2> /dev/null) || return
      echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  }
  
  function parse_git_dirty() {
      if [[ -n $(git status -s --ignore-submodules=dirty 2> /dev/null) ]]; then
          echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
      else
          echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
      fi
  }
  
  function git_prompt_ahead() {
      if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
          echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
      fi
  }
  
  function git_prompt_short_sha() {
      SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  }
  
  function git_prompt_long_sha() {
      SH A=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  }
  
  function git_prompt_status() {
      INDEX=$(git status --porcelain 2> /dev/null)
      STATUS=""
      if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
      fi
      if $(echo "$INDEX" | grep '^A  ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
      elif $(echo "$INDEX" | grep '^M  ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
      fi
      if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
      elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
      elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
      fi
      if $(echo "$INDEX" | grep '^R  ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
      fi
      if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
      elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
      fi
      if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
          STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
      fi
      echo $STATUS
  }
  
#+END_SRC

*** Commands
#+BEGIN_SRC sh  
  autoload -Uz gitaliasinit; gitaliasinit
  autoload -Uz find_root
#+END_SRC
** Build packages
:PROPERTIES:
:header-args: :noweb-ref packages
:END:
#+BEGIN_SRC sh
  autoload -Uz stumpdeb
#+END_SRC
* =zlogin=
#+BEGIN_SRC sh :tangle ~/.zlogin
  # Load RVM into a shell session *as a function
  [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
  if [ "$0" = "/usr/sbin/lightdm-session" -a "$DESKTOP_SESSION" = "i3" ]; then
    export $(gnome-keyring-daemon -s)
  fi
#+END_SRC
* Library
:PROPERTIES:
:mkdirp: yes
:END:
** Plugins
*** Write plugin list
#+BEGIN_SRC sh :tangle ~/.zsh.d/plugins.txt
yardnsm/blox-zsh-theme
hlissner/zsh-autopair
zsh-users/zsh-completions
zsh-users/zsh-autosuggestions
zsh-users/zsh-history-substring-search
zsh-users/zsh-syntax-highlighting
#+END_SRC
*** source the generated shell-file
#+BEGIN_SRC sh :tangle ~/.zshrc
[ -f ~/.zsh.d/plugins.sh ] && source ~/.zsh.d/plugins.sh
#+END_SRC
** Git
*** Init
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/gitaliasinit
  _GITALIAS_DIR=${:-~/.zsh.d/functions/git}
  gitaliasinit() {
      fpath=($_GITALIAS_DIR $fpath)
      for gitalias in $_GITALIAS_DIR/*; do
          if [[ -r $gitalias ]]; then
              name=$gitalias:t
              autoload -Uz $name
          fi
      done
  }
  
  [[ -o kshautoload ]] || gitaliasinit "$@"
#+END_SRC


*** Add
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/add
  git add "${argv[@]}"
#+END_SRC
*** Commit
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/commit
  git commit "${argv[@]}"
#+END_SRC
*** Diff
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/gdiff
  git diff "${argv[@]}"
#+END_SRC
*** Log
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/glog
  git log "${argv[@]}"
#+END_SRC
*** Stat
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/gstat
  git status "${argv[@]}"
#+END_SRC
*** TODO Pull
The normal git pull 
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/pull
  git pull "${argv[@]}"
#+END_SRC

Update a local branch from updated master
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/update_local
checkout master && pull && checkout local && git merge master
#+END_SRC
Update org-mode from maint, not master
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/update_org
checkout maint && pull && checkout local && git merge maint
#+END_SRC

*** Push
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/push
  git push "${argv[@]}"
#+END_SRC
*** Checkout
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/checkout
  git checkout "${argv[@]}"
#+END_SRC
*** Clone
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/git/clone
  git clone "${argv[@]}"
#+END_SRC
** Installation/Packaging
*** =stumpwm=
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/stumpdeb
  pushd ~/Projekte/stumpwm-contrib/
  git pull
  popd
  pushd ~/Projekte/stumpwm
  mkdir -p usr/local/bin
  mkdir -p usr/local/share/stumpwm/lisp
  mkdir -p usr/local/share/info
  cp stumpwm usr/local/bin
  find ~/Projekte/stumpwm-contrib -maxdepth 1 -mindepth 1 -name '[a-z]*' -type d -print0 | xargs -0 -ILISP cp -a LISP usr/local/share/stumpwm/lisp/
  cp stumpwm.info usr/local/share/info/
  gzip usr/local/share/info/stumpwm.info
  fpm -t deb -s dir -n stumpwm -v $(date +"%Y%m%d") -d sbcl usr/
  popd
#+END_SRC 

** Project tools
*** find project root
#+BEGIN_SRC sh :tangle ~/.zsh.d/functions/find_root
local look_from=${1:-$PWD}
local look_for=${2:-}
local look_until=${${3:-/}:A}

# Manually look in parent dirs. An extended Zsh glob should use Y1 for
#   # performance reasons, which is only available in zsh-5.0.5-146-g9381bb6.
local last
local parent_dir="$look_from"
local abs_parent_dir
while true; do
	abs_parent_dir=${parent_dir:A}
	if [[ $abs_parent_dir == $last ]]; then
		break
	fi
	local parent_file="${parent_dir}/${look_for}"

	if [[ -f $parent_file ]]; then
		if [[ ${parent_file[1,2]} == './' ]]; then
			echo ${parent_file#./}
		else
			echo ${parent_file:a}
		fi
		break
	fi

	if [[ $abs_parent_dir == $look_until ]]; then
		break
	fi
	last=$abs_parent_dir
	parent_dir="${parent_dir}/.."
done
#+END_SRC