guest@dotshare [~/groups/shells/zsh] $ ls sobored/ | cat

sobored (raw, dl) (+3 likes)

tyrannopath Feb 15, 2016 (shells/zsh)
  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
#-------------------#--------------------#
 #                                      #
#=- a dance amongst unicorns, by satan -=#
 #                                      #
#-------------------#--------------------#

if [[ ! -o interactive ]] then return; fi

zmodload zsh/complist zsh/parameter zsh/regex zsh/zutil 2>/dev/null

# options {{{

options=(
  autoresume         on
  beep               off
  completealiases    on
  completeinword     on
  extendedglob       on
  menucomplete       on
  multios            on
  notify             on
  promptsubst        on
  histignorealldups  on
  histignorespace    on
  histnofunctions    on
  histnostore        on
  histreduceblanks   on
  sharehistory       on
  vi                 on
)

# }}}
# parameters {{{

export \
  PATH FPATH CDPATH GREP_COLORS \
  HOSTNAME=${HOSTNAME:-localhost} \
  EDITOR=/usr/bin/vim \
  VISUAL=/usr/bin/vim \
  PAGER=/usr/bin/most \
  MANPAGER=/usr/bin/vimpager \
  BROWSER=/usr/bin/elinks \
  GIT_COMMITTER_NAME=$USER \
  GIT_AUTHOR_NAME=$USER \
  EMAIL='like100ninjas.mail@gmail.com' \
  ELINKS_XTERM='st -e' \
  XAUTHORITY=$HOME/var/cache/.Xauthority \
  XDG_CONFIG_HOME=$HOME/etc \
  XDG_DATA_HOME=$HOME/var \
  XDG_CACHE_HOME=$HOME/var/cache \
  XDG_DOWNLOAD_DIR=$HOME/downloads \
  TRASH_DIR=$HOME/var/tmp \
  ELINKS_CONFDIR=$HOME/etc/elinks \
  WWW_HOME='https://www.archlinux.org' \
  GNUPG_HOME=$HOME/etc/gpg

# saved_config_dirs:
# userdirs=/ / / / / /srv/ftp / / /srv/http /root / / /var/spool/mail / / /bin /home/tyrannopath / / / / / /var/lib/colord
# end_1

ZDOTDIR=${ZDOTDIR:-$HOME}
HISTFILE=$XDG_CACHE_HOME/.zsh_history
HISTSIZE=1000
SAVEHIST=$HISTSIZE
LOGCHECK=30
watch=(notme)

# }}}
# directories {{{

local \
  name val \
  exclude=( $WWW_HOME $XDG_RUNTIME_DIR ) \
  bin_home=$HOME/bin \
  dev_home=$HOME/src \
  fun_home=$HOME/etc/zsh/functions

umask 077

for name in ${(k)parameters}; do
  val=${(P)name:|exclude}

  if [[ ${(L)name} =~ .*(dir|home)$ && -n $val ]] then
    mkdir -p $val
  fi
done

# }}}
# paths {{{

local -U tmp_path

_path_add() {
  local dir; eval unset $1

  for dir in $tmp_path; do
    if [[ -d $dir ]] then
      eval $1+=$dir
    fi
  done
}

tmp_path=(
  $bin_home
  $( (print - $bin_home/**/*(/)) 2>/dev/null)
  {/usr{/local,},}{/bin,/sbin}
  $( (print - /usr/bin/*_perl(/)) 2>/dev/null)
)
_path_add path

tmp_path=(
  $fun_home
  $( (print - $fun_home/**/*(/)) 2>/dev/null)
  $( (print - /usr/share/zsh/*functions(/)) 2>/dev/null)
  $( (print - /usr/share/zsh/*functions/**/*(/)) 2>/dev/null)
)
_path_add fpath

tmp_path=( $HOME $XDG_CONFIG_DIR $XDG_DATA_DIR )
_path_add cdpath

unfunction _path_add

# }}}
# keybindings {{{

if [[ $TERM =~ .*color ]] then
  bindkey \
    '^[[H'  beginning-of-line \
    '^[[4h' overwrite-mode \
    '^[[P'  delete-char
fi

bindkey \
  '^[[1~' beginning-of-line \
  '^[[2~' overwrite-mode \
  '^[[3~' delete-char \
  '^[[4~' end-of-line \
  '^[[5~' beginning-of-buffer-or-history \
  '^[[6~' end-of-buffer-or-history \
  '^[[A'  up-line-or-history \
  '^[[B'  down-line-or-history \
  '^[[C'  forward-char \
  '^[[D'  backward-char \
  '^?'    backward-delete-char \
  ' '     magic-space \
  '\t'    complete-word

bindkey -M menuselect \
  '^[[2~' vi-insert \
  '^[[5~' backward-word \
  '^[[6~' forward-word \
  '^?'    undo \
  ' '     accept-and-menu-complete \
  '\t'    accept-and-infer-next-history

#}}}
# functions {{{

local func fun_spot=( $( (print - $fun_home/**/*~*.swp~*.zwc(.)) 2>/dev/null) )

for func in $fun_spot; do
  autoload -U $func
done

precmd() {
  PS1='%K{0} %F{1}error%F{7}: %F{3}prompt borken %k %F{2}» %f'
  _set_prompt 2>/dev/null

  if [[ $TERM =~ .*color ]] then
    print -Pn "\e]0;%n@%m:%~\a"
  fi

  print - ''
}

preexec() {
  if [[ $TERM =~ .*color ]] then
    print -Pn "\e]0;$1\a"
  fi

  print - ''
}

chpwd() { ls -AFX --group-directories-first --color='auto' }

autoload -U \
  run-help \
  tetris \
  zcalc \
  compinit && compinit -d $XDG_CACHE_DIR/zsh_comp_dump

# }}}
# _set_prompt() {{{

_set_prompt() {
  local \
    dir=$PWD:t \
    branch=$( (git symbolic-ref --short HEAD) 2>/dev/null) \

  dir=${dir//$USER/\\\$HOME}

  PS1="%K{0} %F{4}$dir %k %F{2}» %f"
  PS2="%K{0} %F{4}$_ %k %F{2}» %f"
  RPS1=''

  if [[ -n $branch ]] then
    local \
      gdir=$( (git --rev-parse --git-dir) 2>/dev/null) \
      behind=$( (git log --oneline ..@{u} | wc -l | tr -d ' ') 2>/dev/null) \
      ahead=$( (git log --oneline @{u}.. | wc -l | tr -d ' ') 2>/dev/null)

    if [[ -r $gdir/MERGE_HEAD ]] then
      RPS1+='%F{5}⚡ '
    fi

    if [[ -n $( (git ls-files --other --exclude-standard) 2>/dev/null) ]] then
      RPS1+='%F{1}•'
    fi

    if [[ -n $( (git diff | cat) 2>/dev/null) ]] then
      RPS1+='%F{3}•'
    fi

    if [[ -n $( (git diff --cached | cat) 2>/dev/null) ]] then
      RPS1+='%F{2}•'
    fi

    RPS1+=" %K{0} %F{3}$branch"

    if (( $behind )) then
      RPS1+="%F{1} -$behind"
    fi

    if (( $ahead )) then
      RPS1+="%F{2} +$ahead"
    fi

    RPS1+=" %f%k"
  fi
}

# }}}
# _save_config_dirs() {{{

_save_config_dirs() {
  local name val string where=$ZDOTDIR/.zshrc lol=saved_

  for name in ${(k)parameters}; do
    val=${(P)name}

    if [[ ${(L)name} =~ .*(dir|home) && -n $val ]] then
      if [[ -z $( (grep "$name=$val" $where) 2>/dev/null) ]] then
        string="export $name=$val"
        sed -i '/'$lol'config_dirs:/a '$string $where
      fi
    fi
  done
}

# }}}
# _save_aliases() {{{

_save_aliases() {
  local name num string val where=$ZDOTDIR/.zshrc

  while (( $# )); do
    if [[ ! $1 =~ (aliases|galiases|saliases) ]] then
      shift
      continue
    fi

    for name in $(eval 'print - ${(k)'$1'}'); do
      eval 'val=${'$1'['$name']}'

      if [[ -z $( (agrep -e $name'.*'$val $where) 2>/dev/null) && -n $val ]] then
        string=$name

        num=$(( 10 - ${#name} ))

        if (( num > 0 )) then
          while (( num )); do
            string+=' '
            num=$(( num - 1 ))
          done
        else
            string+=' '
        fi

        string+=\"$val\"

        sed -i '/saved_'$1':/a '$string $where
      fi
    done
    shift
  done
}

# }}}
# obliterate() {{{

obliterate() {
  local \
    where=$ZDOTDIR/.zshrc lol=saved_ \
    valid=( configdirs aliases galiases saliases ) \
    usage='obliterate: usage `obliterate (configdirs|aliases|galiases|saliases)'

  if (( $# == 0 )) then
    print - $usage
    return 1
  elif [[ -z ${@:*valid} ]] then
    print - $usage
    return 1
  elif [[ -n ${@:|valid} ]] then
    print - "obliterate: bad argument(s): ${@:|valid}"
    return 1
  fi

  while (( $# )); do
    case $1 in
      configdirs)
        sed -i '/'$lol'config_dirs:/,/end_1/c # '$lol'config_dirs\n# end_1' $where
      ;;
      aliases)
        sed -i '/'$lol'aliases:/,/end_2/c # '$lol'aliases\n# end_2' $where
      ;;
      galiases)
        sed -i '/'$lol'galiases:/,/end_3/c # '$lol'galiases\n# end_3' $where
      ;;
      saliases)
        sed -i '/'$lol'saliases:/,/end_4/c # '$lol'saliases\n# end_4' $where
      ;;
    esac
    shift
  done
}

# }}}
# trash() {{{

trash() {
  local piece

  if (( $# == 1 )) && [[ $1 == 'dump' ]] then
    rm -rf $TRASH_DIR/*
    return 0
  elif (( $# == 0 )) then
    print - 'trash: usage: `trash file [ ...file ]'\'
    print - '              OR `trash dump'\'
    return 0
  elif [[ ! -d $TRASH_DIR || ! -O $TRASH_DIR ]] then
    print - 'trash: go define $TRASH_DIR' &>2
    return 1
  fi

  for piece in $@; do
    if [[ ! -O $piece ]] then
      print - "trash: invalid piece of trash: $piece" &>2
      return 1
    fi
  done

  while (( $# )); do
    mv -f $1 $TRASH_DIR
    shift
  done
}


# }}}
# aliases {{{

unalias run-help

aliases=(
  ahack  'telnet nethack.alt.org'
  grep   'grep --color=auto'
  egrep  'egrep --color=auto'
  fgrep  'fgrep --color=auto'
  help   'run-help'
  la     'ls -AFX --group-directories-first --color=auto'
  ll     'ls -fgGhX --group-directories-first --color=auto --time-style=+'
  lla    'ls -AfgGhX --group-directories-first --color=auto --time-style=+'
  ls     'ls -FX --group-directories-first --color=auto'
  pg     "!! | $PAGER"
  rm     'rm -r'
  save   '_save_config_dirs; _save_aliases aliases galiases saliases'
  su     'su -'
# saved_aliases:
# end_2
)

galiases=(
  ...    '../..'
  ....   '../../..'
  xG     '| grep'
  xH     '| head'
  xL     '| less'
  xM     '| more'
  xP     "| $PAGER"
  xT     '| tail'
# saved_galiases:
# end_3
)

saliases=(
# saved_saliases:
# end_4
)

# }}}
# colors {{{

typeset -TUx LS_COLORS dircolors=(
  {fi,no}'=0;90'
  di'=00;34'
  ex'=00;32'
  ln'=00;33'
  or'=00;31'
  mi'=05;31'
  {pi,so}'=00;35'
  {bd,cd}'=00;36'
  lc'=\e['
)

GREP_COLORS='mt=00;34:fn=00;36:ln=00;33:se=00;90'

# }}}
# completion {{{

zstyle ':completion:*'                  squeeze-slashes true
zstyle ':completion:*'                  matcher-list \
  '' 'm:{a-z-}={A-Z_}' 'r:|[._-/]=* r:|=*'
zstyle ':completion:*'                  completer \
  _complete _match _ignored

zstyle ':completion:*:*:(cd|kill|ls|mv|rename|rm|vim):*' menu \
  yes select
zstyle ':completion:*:*:(cd|kill|ls|mv|rename|rm|vim):*' force-list \
  always
zstyle ':completion:*:*:(cd|kill|ls|mv|rename|rm|vim):*' ignore-parents \
  parent pwd
zstyle ':completion:*:*:(cd|kill|ls|mv|rename|rm|vim):*' list-dirs-first

zstyle ':completion:*:warnings'         format '%F{1}%d%f'
zstyle ':completion:*:messages'         format '%F{2}%d%f'
zstyle ':completion:*:corrections'      format '%F{3}%d%f'
zstyle ':completion:*:descriptions'     format '%F{4}%d%f'

zstyle ':completion:*:functions'        ignored-patterns '_*'
zstyle ':completion:*:*:*:users'        ignored-patterns \
  'avahi' 'bin' 'colord' 'daemon' 'dbus' 'ftp' 'git' 'http' 'mail' \
  'mpd' 'nobody' 'nvidia*' 'polkitd' 'systemd*' 'uuidd'

zstyle ':completion:*'                  list-colors $dircolors 'ma=00;40'
zstyle ':completion:*:*:kill:*'         list-colors "=(#b) #([0-9]#)*=36=31"
zstyle ':completion:*:*:kill:*:jobs'    list-colors "=(#b) #([0-9]#)*=35=31"

zstyle ':completion:*'                  use-cache on
zstyle ':completion:*'                  cache-path \
  "$XDG_CACHE_DIR/zsh_comp_cache"

# }}}

trap '_save_config_dirs; _save_aliases aliases galiases saliases' EXIT QUIT

# vim: set fdm=marker ts=8 sts=2 ft=zsh:

CLICK TO VIEW

x

Notes

tried bsd without backing up my old files, so i spent the past 2 days nerding out on my .zshrc.

Comments

tyrannopath said about 8 years ago

i fixed a bunch of things i screwed up on.

edited about 8 years ago

cockandballs said about 7 years ago

love everything about it definately the best around