Advertisement
v1ral_ITS

zsh notes

Aug 24th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 48.38 KB | None | 0 0
  1. ## Bazaar integration
  2. ## Just works with the GIT integration just add $(bzr_prompt_info) to the PROMPT
  3. function bzr_prompt_info() {
  4.     BZR_CB=`bzr nick 2> /dev/null | grep -v "ERROR" | cut -d ":" -f2 | awk -F / '{print "bzr::"$1}'`
  5.     if [ -n "$BZR_CB" ]; then
  6.         BZR_DIRTY=""
  7.         [[ -n `bzr status` ]] && BZR_DIRTY=" %{$fg[red]%} * %{$fg[green]%}"
  8.         echo "$ZSH_THEME_SCM_PROMPT_PREFIX$BZR_CB$BZR_DIRTY$ZSH_THEME_GIT_PROMPT_SUFFIX"
  9.     fi
  10. }# System clipboard integration
  11. #
  12. # This file has support for doing system clipboard copy and paste operations
  13. # from the command line in a generic cross-platform fashion.
  14. #
  15. # On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
  16. # Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
  17. # "system clipboard", and the X Windows `xclip` command must be installed.
  18.  
  19. # clipcopy - Copy data to clipboard
  20. #
  21. # Usage:
  22. #
  23. #  <command> | clipcopy    - copies stdin to clipboard
  24. #
  25. #  clipcopy <file>         - copies a file's contents to clipboard
  26. #
  27. function clipcopy() {
  28.   emulate -L zsh
  29.   local file=$1
  30.   if [[ $OSTYPE == darwin* ]]; then
  31.     if [[ -z $file ]]; then
  32.       pbcopy
  33.     else
  34.       cat $file | pbcopy
  35.     fi
  36.   elif [[ $OSTYPE == cygwin* ]]; then
  37.     if [[ -z $file ]]; then
  38.       cat > /dev/clipboard
  39.     else
  40.       cat $file > /dev/clipboard
  41.     fi
  42.   else
  43.     if (( $+commands[xclip] )); then
  44.       if [[ -z $file ]]; then
  45.         xclip -in -selection clipboard
  46.       else
  47.         xclip -in -selection clipboard $file
  48.       fi
  49.     elif (( $+commands[xsel] )); then
  50.       if [[ -z $file ]]; then
  51.         xsel --clipboard --input
  52.       else
  53.         cat "$file" | xsel --clipboard --input
  54.       fi
  55.     else
  56.       print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  57.       return 1
  58.     fi
  59.   fi
  60. }
  61.  
  62. # clippaste - "Paste" data from clipboard to stdout
  63. #
  64. # Usage:
  65. #
  66. #   clippaste   - writes clipboard's contents to stdout
  67. #
  68. #   clippaste | <command>    - pastes contents and pipes it to another process
  69. #
  70. #   clippaste > <file>      - paste contents to a file
  71. #
  72. # Examples:
  73. #
  74. #   # Pipe to another process
  75. #   clippaste | grep foo
  76. #
  77. #   # Paste to a file
  78. #   clippaste > file.txt
  79. function clippaste() {
  80.   emulate -L zsh
  81.   if [[ $OSTYPE == darwin* ]]; then
  82.     pbpaste
  83.   elif [[ $OSTYPE == cygwin* ]]; then
  84.     cat /dev/clipboard
  85.   else
  86.     if (( $+commands[xclip] )); then
  87.       xclip -out -selection clipboard
  88.     elif (( $+commands[xsel] )); then
  89.       xsel --clipboard --output
  90.     else
  91.       print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  92.       return 1
  93.     fi
  94.   fi
  95. }
  96. # Handle completions insecurities (i.e., completion-dependent directories with
  97. # insecure ownership or permissions) by:
  98. #
  99. # * Human-readably notifying the user of these insecurities.
  100. function handle_completion_insecurities() {
  101.   # List of the absolute paths of all unique insecure directories, split on
  102.   # newline from compaudit()'s output resembling:
  103.   #
  104.   #     There are insecure directories:
  105.   #     /usr/share/zsh/site-functions
  106.   #     /usr/share/zsh/5.0.6/functions
  107.   #     /usr/share/zsh
  108.   #     /usr/share/zsh/5.0.6
  109.   #
  110.   # Since the ignorable first line is printed to stderr and thus not captured,
  111.   # stderr is squelched to prevent this output from leaking to the user.
  112.   local -aU insecure_dirs
  113.   insecure_dirs=( ${(f@):-"$(compaudit 2>/dev/null)"} )
  114.  
  115.   # If no such directories exist, get us out of here.
  116.   (( ! ${#insecure_dirs} )) && return
  117.  
  118.   # List ownership and permissions of all insecure directories.
  119.   print "[oh-my-zsh] Insecure completion-dependent directories detected:"
  120.   ls -ld "${(@)insecure_dirs}"
  121.  
  122.   cat <<EOD
  123.  
  124. [oh-my-zsh] For safety, we will not load completions from these directories until
  125. [oh-my-zsh] you fix their permissions and ownership and restart zsh.
  126. [oh-my-zsh] See the above list for directories with group or other writability.
  127.  
  128. [oh-my-zsh] To fix your permissions you can do so by disabling
  129. [oh-my-zsh] the write permission of "group" and "others" and making sure that the
  130. [oh-my-zsh] owner of these directories is either root or your current user.
  131. [oh-my-zsh] The following command may help:
  132. [oh-my-zsh]     compaudit | xargs chmod g-w,o-w
  133.  
  134. [oh-my-zsh] If the above didn't help or you want to skip the verification of
  135. [oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
  136. [oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.
  137.  
  138. EOD
  139. }
  140. # fixme - the load process here seems a bit bizarre
  141. zmodload -i zsh/complist
  142.  
  143. WORDCHARS=''
  144.  
  145. unsetopt menu_complete   # do not autoselect the first completion entry
  146. unsetopt flowcontrol
  147. setopt auto_menu         # show completion menu on successive tab press
  148. setopt complete_in_word
  149. setopt always_to_end
  150.  
  151. # should this be in keybindings?
  152. bindkey -M menuselect '^o' accept-and-infer-next-history
  153. zstyle ':completion:*:*:*:*:*' menu select
  154.  
  155. # case insensitive (all), partial-word and substring completion
  156. if [[ "$CASE_SENSITIVE" = true ]]; then
  157.   zstyle ':completion:*' matcher-list 'r:|=*' 'l:|=* r:|=*'
  158. else
  159.   if [[ "$HYPHEN_INSENSITIVE" = true ]]; then
  160.     zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|=*' 'l:|=* r:|=*'
  161.   else
  162.     zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*'
  163.   fi
  164. fi
  165. unset CASE_SENSITIVE HYPHEN_INSENSITIVE
  166.  
  167. # Complete . and .. special directories
  168. zstyle ':completion:*' special-dirs true
  169.  
  170. zstyle ':completion:*' list-colors ''
  171. zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
  172.  
  173. if [[ "$OSTYPE" = solaris* ]]; then
  174.   zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm"
  175. else
  176.   zstyle ':completion:*:*:*:*:processes' command "ps -u $USER -o pid,user,comm -w -w"
  177. fi
  178.  
  179. # disable named-directories autocompletion
  180. zstyle ':completion:*:cd:*' tag-order local-directories directory-stack path-directories
  181.  
  182. # Use caching so that commands like apt and dpkg complete are useable
  183. zstyle ':completion::complete:*' use-cache 1
  184. zstyle ':completion::complete:*' cache-path $ZSH_CACHE_DIR
  185.  
  186. # Don't complete uninteresting users
  187. zstyle ':completion:*:*:*:users' ignored-patterns \
  188.         adm amanda apache at avahi avahi-autoipd beaglidx bin cacti canna \
  189.         clamav daemon dbus distcache dnsmasq dovecot fax ftp games gdm \
  190.         gkrellmd gopher hacluster haldaemon halt hsqldb ident junkbust kdm \
  191.         ldap lp mail mailman mailnull man messagebus  mldonkey mysql nagios \
  192.         named netdump news nfsnobody nobody nscd ntp nut nx obsrun openvpn \
  193.         operator pcap polkitd postfix postgres privoxy pulse pvm quagga radvd \
  194.         rpc rpcuser rpm rtkit scard shutdown squid sshd statd svn sync tftp \
  195.         usbmux uucp vcsa wwwrun xfs '_*'
  196.  
  197. # ... unless we really want to.
  198. zstyle '*' single-ignored show
  199.  
  200. if [[ $COMPLETION_WAITING_DOTS = true ]]; then
  201.   expand-or-complete-with-dots() {
  202.     # toggle line-wrapping off and back on again
  203.     [[ -n "$terminfo[rmam]" && -n "$terminfo[smam]" ]] && echoti rmam
  204.     print -Pn "%{%F{red}......%f%}"
  205.     [[ -n "$terminfo[rmam]" && -n "$terminfo[smam]" ]] && echoti smam
  206.  
  207.     zle expand-or-complete
  208.     zle redisplay
  209.   }
  210.   zle -N expand-or-complete-with-dots
  211.   bindkey "^I" expand-or-complete-with-dots
  212. fi
  213. if [[ "$ENABLE_CORRECTION" == "true" ]]; then
  214.   alias cp='nocorrect cp'
  215.   alias ebuild='nocorrect ebuild'
  216.   alias gist='nocorrect gist'
  217.   alias heroku='nocorrect heroku'
  218.   alias hpodder='nocorrect hpodder'
  219.   alias man='nocorrect man'
  220.   alias mkdir='nocorrect mkdir'
  221.   alias mv='nocorrect mv'
  222.   alias mysql='nocorrect mysql'
  223.   alias sudo='nocorrect sudo'
  224.  
  225.   setopt correct_all
  226. fi
  227. # diagnostics.zsh
  228. #
  229. # Diagnostic and debugging support for oh-my-zsh
  230.  
  231. # omz_diagnostic_dump()
  232. #
  233. # Author: Andrew Janke <andrew@apjanke.net>
  234. #
  235. # Usage:
  236. #
  237. # omz_diagnostic_dump [-v] [-V] [file]
  238. #
  239. # NOTE: This is a work in progress. Its interface and behavior are going to change,
  240. # and probably in non-back-compatible ways.
  241. #
  242. # Outputs a bunch of information about the state and configuration of
  243. # oh-my-zsh, zsh, and the user's system. This is intended to provide a
  244. # bunch of context for diagnosing your own or a third party's problems, and to
  245. # be suitable for posting to public bug reports.
  246. #
  247. # The output is human-readable and its format may change over time. It is not
  248. # suitable for parsing. All the output is in one single file so it can be posted
  249. # as a gist or bug comment on GitHub. GitHub doesn't support attaching tarballs
  250. # or other files to bugs; otherwise, this would probably have an option to produce
  251. # tarballs that contain copies of the config and customization files instead of
  252. # catting them all in to one file.
  253. #
  254. # This is intended to be widely portable, and run anywhere that oh-my-zsh does.
  255. # Feel free to report any portability issues as bugs.
  256. #
  257. # This is written in a defensive style so it still works (and can detect) cases when
  258. # basic functionality like echo and which have been redefined. In particular, almost
  259. # everything is invoked with "builtin" or "command", to work in the face of user
  260. # redefinitions.
  261. #
  262. # OPTIONS
  263. #
  264. # [file]   Specifies the output file. If not given, a file in the current directory
  265. #        is selected automatically.
  266. #
  267. # -v    Increase the verbosity of the dump output. May be specified multiple times.
  268. #       Verbosity levels:
  269. #        0 - Basic info, shell state, omz configuration, git state
  270. #        1 - (default) Adds key binding info and configuration file contents
  271. #        2 - Adds zcompdump file contents
  272. #
  273. # -V    Reduce the verbosity of the dump output. May be specified multiple times.
  274. #
  275. # TODO:
  276. # * Multi-file capture
  277. # * Add automatic gist uploading
  278. # * Consider whether to move default output file location to TMPDIR. More robust
  279. #     but less user friendly.
  280. #
  281.  
  282. autoload -Uz is-at-least
  283.  
  284. function omz_diagnostic_dump() {
  285.   emulate -L zsh
  286.  
  287.   builtin echo "Generating diagnostic dump; please be patient..."
  288.  
  289.   local thisfcn=omz_diagnostic_dump
  290.   local -A opts
  291.   local opt_verbose opt_noverbose opt_outfile
  292.   local timestamp=$(date +%Y%m%d-%H%M%S)
  293.   local outfile=omz_diagdump_$timestamp.txt
  294.   builtin zparseopts -A opts -D -- "v+=opt_verbose" "V+=opt_noverbose"
  295.   local verbose n_verbose=${#opt_verbose} n_noverbose=${#opt_noverbose}
  296.   (( verbose = 1 + n_verbose - n_noverbose ))
  297.  
  298.   if [[ ${#*} > 0 ]]; then
  299.     opt_outfile=$1
  300.   fi
  301.   if [[ ${#*} > 1 ]]; then
  302.     builtin echo "$thisfcn: error: too many arguments" >&2
  303.     return 1
  304.   fi
  305.   if [[ -n "$opt_outfile" ]]; then
  306.     outfile="$opt_outfile"
  307.   fi
  308.  
  309.   # Always write directly to a file so terminal escape sequences are
  310.   # captured cleanly
  311.   _omz_diag_dump_one_big_text &> "$outfile"
  312.   if [[ $? != 0 ]]; then
  313.     builtin echo "$thisfcn: error while creating diagnostic dump; see $outfile for details"
  314.   fi
  315.  
  316.   builtin echo
  317.   builtin echo Diagnostic dump file created at: "$outfile"
  318.   builtin echo
  319.   builtin echo To share this with OMZ developers, post it as a gist on GitHub
  320.   builtin echo at "https://gist.github.com" and share the link to the gist.
  321.   builtin echo
  322.   builtin echo "WARNING: This dump file contains all your zsh and omz configuration files,"
  323.   builtin echo "so don't share it publicly if there's sensitive information in them."
  324.   builtin echo
  325.  
  326. }
  327.  
  328. function _omz_diag_dump_one_big_text() {
  329.   local program programs progfile md5
  330.  
  331.   builtin echo oh-my-zsh diagnostic dump
  332.   builtin echo
  333.   builtin echo $outfile
  334.   builtin echo
  335.  
  336.   # Basic system and zsh information
  337.   command date
  338.   command uname -a
  339.   builtin echo OSTYPE=$OSTYPE
  340.   builtin echo ZSH_VERSION=$ZSH_VERSION
  341.   builtin echo User: $USER
  342.   builtin echo umask: $(umask)
  343.   builtin echo
  344.   _omz_diag_dump_os_specific_version
  345.   builtin echo
  346.  
  347.   # Installed programs
  348.   programs=(sh zsh ksh bash sed cat grep ls find git posh)
  349.   local progfile="" extra_str="" sha_str=""
  350.   for program in $programs; do
  351.     extra_str="" sha_str=""
  352.     progfile=$(builtin which $program)
  353.     if [[ $? == 0 ]]; then
  354.       if [[ -e $progfile ]]; then
  355.         if builtin whence shasum &>/dev/null; then
  356.           sha_str=($(command shasum $progfile))
  357.           sha_str=$sha_str[1]
  358.           extra_str+=" SHA $sha_str"
  359.         fi
  360.         if [[ -h "$progfile" ]]; then
  361.           extra_str+=" ( -> ${progfile:A} )"
  362.         fi
  363.       fi
  364.       builtin printf '%-9s %-20s %s\n' "$program is" "$progfile" "$extra_str"
  365.     else
  366.       builtin echo "$program: not found"
  367.     fi
  368.   done
  369.   builtin echo
  370.   builtin echo Command Versions:
  371.   builtin echo "zsh: $(zsh --version)"
  372.   builtin echo "this zsh session: $ZSH_VERSION"
  373.   builtin echo "bash: $(bash --version | command grep bash)"
  374.   builtin echo "git: $(git --version)"
  375.   builtin echo "grep: $(grep --version)"
  376.   builtin echo
  377.  
  378.   # Core command definitions
  379.   _omz_diag_dump_check_core_commands || return 1
  380.   builtin echo  
  381.  
  382.   # ZSH Process state
  383.   builtin echo Process state:
  384.   builtin echo pwd: $PWD
  385.   if builtin whence pstree &>/dev/null; then
  386.     builtin echo Process tree for this shell:
  387.     pstree -p $$
  388.   else
  389.     ps -fT
  390.   fi
  391.   builtin set | command grep -a '^\(ZSH\|plugins\|TERM\|LC_\|LANG\|precmd\|chpwd\|preexec\|FPATH\|TTY\|DISPLAY\|PATH\)\|OMZ'
  392.   builtin echo
  393.   #TODO: Should this include `env` instead of or in addition to `export`?
  394.   builtin echo Exported:
  395.   builtin echo $(builtin export | command sed 's/=.*//')
  396.   builtin echo
  397.   builtin echo Locale:
  398.   command locale
  399.   builtin echo
  400.  
  401.   # Zsh installation and configuration
  402.   builtin echo Zsh configuration:
  403.   builtin echo setopt: $(builtin setopt)
  404.   builtin echo
  405.   builtin echo zstyle:
  406.   builtin zstyle
  407.   builtin echo
  408.   builtin echo 'compaudit output:'
  409.   compaudit
  410.   builtin echo
  411.   builtin echo '$fpath directories:'
  412.   command ls -lad $fpath
  413.   builtin echo
  414.  
  415.   # Oh-my-zsh installation
  416.   builtin echo oh-my-zsh installation:
  417.   command ls -ld ~/.z*
  418.   command ls -ld ~/.oh*
  419.   builtin echo
  420.   builtin echo oh-my-zsh git state:
  421.   (cd $ZSH && builtin echo "HEAD: $(git rev-parse HEAD)" && git remote -v && git status | command grep "[^[:space:]]")
  422.   if [[ $verbose -ge 1 ]]; then
  423.     (cd $ZSH && git reflog --date=default | command grep pull)
  424.   fi
  425.   builtin echo
  426.   if [[ -e $ZSH_CUSTOM ]]; then
  427.     local custom_dir=$ZSH_CUSTOM
  428.     if [[ -h $custom_dir ]]; then
  429.       custom_dir=$(cd $custom_dir && pwd -P)
  430.     fi
  431.     builtin echo "oh-my-zsh custom dir:"
  432.     builtin echo "   $ZSH_CUSTOM ($custom_dir)"
  433.     (cd ${custom_dir:h} && command find ${custom_dir:t} -name .git -prune -o -print)
  434.     builtin echo
  435.   fi
  436.  
  437.   # Key binding and terminal info
  438.   if [[ $verbose -ge 1 ]]; then
  439.     builtin echo "bindkey:"
  440.     builtin bindkey
  441.     builtin echo
  442.     builtin echo "infocmp:"
  443.     command infocmp -L
  444.     builtin echo
  445.   fi
  446.  
  447.   # Configuration file info
  448.   local zdotdir=${ZDOTDIR:-$HOME}
  449.   builtin echo "Zsh configuration files:"
  450.   local cfgfile cfgfiles
  451.   # Some files for bash that zsh does not use are intentionally included
  452.   # to help with diagnosing behavior differences between bash and zsh
  453.   cfgfiles=( /etc/zshenv /etc/zprofile /etc/zshrc /etc/zlogin /etc/zlogout
  454.     $zdotdir/.zshenv $zdotdir/.zprofile $zdotdir/.zshrc $zdotdir/.zlogin $zdotdir/.zlogout
  455.     ~/.zsh.pre-oh-my-zsh
  456.     /etc/bashrc /etc/profile ~/.bashrc ~/.profile ~/.bash_profile ~/.bash_logout )
  457.   command ls -lad $cfgfiles 2>&1
  458.   builtin echo
  459.   if [[ $verbose -ge 1 ]]; then
  460.     for cfgfile in $cfgfiles; do
  461.       _omz_diag_dump_echo_file_w_header $cfgfile
  462.     done
  463.   fi
  464.   builtin echo
  465.   builtin echo "Zsh compdump files:"
  466.   local dumpfile dumpfiles
  467.   command ls -lad $zdotdir/.zcompdump*
  468.   dumpfiles=( $zdotdir/.zcompdump*(N) )
  469.   if [[ $verbose -ge 2 ]]; then
  470.     for dumpfile in $dumpfiles; do
  471.       _omz_diag_dump_echo_file_w_header $dumpfile
  472.     done
  473.   fi
  474.  
  475. }
  476.  
  477. function _omz_diag_dump_check_core_commands() {
  478.   builtin echo "Core command check:"
  479.   local redefined name builtins externals reserved_words
  480.   redefined=()
  481.   # All the zsh non-module builtin commands
  482.   # These are taken from the zsh reference manual for 5.0.2
  483.   # Commands from modules should not be included.
  484.   # (For back-compatibility, if any of these are newish, they should be removed,
  485.   # or at least made conditional on the version of the current running zsh.)
  486.   # "history" is also excluded because OMZ is known to redefine that
  487.   reserved_words=( do done esac then elif else fi for case if while function
  488.     repeat time until select coproc nocorrect foreach end '!' '[[' '{' '}'
  489.     )
  490.   builtins=( alias autoload bg bindkey break builtin bye cd chdir command
  491.     comparguments compcall compctl compdescribe compfiles compgroups compquote comptags
  492.     comptry compvalues continue dirs disable disown echo echotc echoti emulate
  493.     enable eval exec exit false fc fg functions getln getopts hash
  494.     jobs kill let limit log logout noglob popd print printf
  495.     pushd pushln pwd r read rehash return sched set setopt shift
  496.     source suspend test times trap true ttyctl type ulimit umask unalias
  497.     unfunction unhash unlimit unset unsetopt vared wait whence where which zcompile
  498.     zle zmodload zparseopts zregexparse zstyle )
  499.   if is-at-least 5.1; then
  500.     reserved_word+=( declare export integer float local readonly typeset )
  501.   else
  502.     builtins+=( declare export integer float local readonly typeset )
  503.   fi
  504.   builtins_fatal=( builtin command local )
  505.   externals=( zsh )
  506.   for name in $reserved_words; do
  507.     if [[ $(builtin whence -w $name) != "$name: reserved" ]]; then
  508.       builtin echo "reserved word '$name' has been redefined"
  509.       builtin which $name
  510.       redefined+=$name
  511.     fi
  512.   done
  513.   for name in $builtins; do
  514.     if [[ $(builtin whence -w $name) != "$name: builtin" ]]; then
  515.       builtin echo "builtin '$name' has been redefined"
  516.       builtin which $name
  517.       redefined+=$name
  518.     fi
  519.   done
  520.   for name in $externals; do
  521.     if [[ $(builtin whence -w $name) != "$name: command" ]]; then
  522.       builtin echo "command '$name' has been redefined"
  523.       builtin which $name
  524.       redefined+=$name
  525.     fi
  526.   done
  527.  
  528.   if [[ -n "$redefined" ]]; then
  529.     builtin echo "SOME CORE COMMANDS HAVE BEEN REDEFINED: $redefined"
  530.   else
  531.     builtin echo "All core commands are defined normally"
  532.   fi
  533.  
  534. }
  535.  
  536. function _omz_diag_dump_echo_file_w_header() {
  537.   local file=$1
  538.   if [[ ( -f $file || -h $file ) ]]; then
  539.     builtin echo "========== $file =========="
  540.     if [[ -h $file ]]; then
  541.       builtin echo "==========    ( => ${file:A} )   =========="
  542.     fi
  543.     command cat $file
  544.     builtin echo "========== end $file =========="
  545.     builtin echo
  546.   elif [[ -d $file ]]; then
  547.     builtin echo "File '$file' is a directory"
  548.   elif [[ ! -e $file ]]; then
  549.     builtin echo "File '$file' does not exist"
  550.   else
  551.     command ls -lad "$file"
  552.   fi
  553. }
  554.  
  555. function _omz_diag_dump_os_specific_version() {
  556.   local osname osver version_file version_files
  557.   case "$OSTYPE" in
  558.     darwin*)
  559.       osname=$(command sw_vers -productName)
  560.       osver=$(command sw_vers -productVersion)      
  561.       builtin echo "OS Version: $osname $osver build $(sw_vers -buildVersion)"
  562.       ;;
  563.     cygwin)
  564.       command systeminfo | command head -4 | command tail -2
  565.       ;;
  566.   esac
  567.  
  568.   if builtin which lsb_release >/dev/null; then
  569.     builtin echo "OS Release: $(command lsb_release -s -d)"
  570.   fi
  571.  
  572.   version_files=( /etc/*-release(N) /etc/*-version(N) /etc/*_version(N) )
  573.   for version_file in $version_files; do
  574.     builtin echo "$version_file:"
  575.     command cat "$version_file"
  576.     builtin echo
  577.   done
  578. }
  579.  
  580. # Changing/making/removing directory
  581. setopt auto_pushd
  582. setopt pushd_ignore_dups
  583. setopt pushdminus
  584.  
  585. alias -g ...='../..'
  586. alias -g ....='../../..'
  587. alias -g .....='../../../..'
  588. alias -g ......='../../../../..'
  589.  
  590. alias -- -='cd -'
  591. alias 1='cd -'
  592. alias 2='cd -2'
  593. alias 3='cd -3'
  594. alias 4='cd -4'
  595. alias 5='cd -5'
  596. alias 6='cd -6'
  597. alias 7='cd -7'
  598. alias 8='cd -8'
  599. alias 9='cd -9'
  600.  
  601. alias md='mkdir -p'
  602. alias rd=rmdir
  603. alias d='dirs -v | head -10'
  604.  
  605. # List directory contents
  606. alias lsa='ls -lah'
  607. alias l='ls -lah'
  608. alias ll='ls -lh'
  609. alias la='ls -lAh'
  610. function zsh_stats() {
  611.   fc -l 1 | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl |  head -n20
  612. }
  613.  
  614. function uninstall_oh_my_zsh() {
  615.   env ZSH=$ZSH sh $ZSH/tools/uninstall.sh
  616. }
  617.  
  618. function upgrade_oh_my_zsh() {
  619.   env ZSH=$ZSH sh $ZSH/tools/upgrade.sh
  620. }
  621.  
  622. function take() {
  623.   mkdir -p $@ && cd ${@:$#}
  624. }
  625.  
  626. function open_command() {
  627.   local open_cmd
  628.  
  629.   # define the open command
  630.   case "$OSTYPE" in
  631.     darwin*)  open_cmd='open' ;;
  632.     cygwin*)  open_cmd='cygstart' ;;
  633.     linux*)   ! [[ $(uname -a) =~ "Microsoft" ]] && open_cmd='xdg-open' || {
  634.                 open_cmd='cmd.exe /c start ""'
  635.                 [[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
  636.               } ;;
  637.     msys*)    open_cmd='start ""' ;;
  638.     *)        echo "Platform $OSTYPE not supported"
  639.               return 1
  640.               ;;
  641.   esac
  642.  
  643.   # don't use nohup on OSX
  644.   if [[ "$OSTYPE" == darwin* ]]; then
  645.     ${=open_cmd} "$@" &>/dev/null
  646.   else
  647.     nohup ${=open_cmd} "$@" &>/dev/null
  648.   fi
  649. }
  650.  
  651. #
  652. # Get the value of an alias.
  653. #
  654. # Arguments:
  655. #    1. alias - The alias to get its value from
  656. # STDOUT:
  657. #    The value of alias $1 (if it has one).
  658. # Return value:
  659. #    0 if the alias was found,
  660. #    1 if it does not exist
  661. #
  662. function alias_value() {
  663.     (( $+aliases[$1] )) && echo $aliases[$1]
  664. }
  665.  
  666. #
  667. # Try to get the value of an alias,
  668. # otherwise return the input.
  669. #
  670. # Arguments:
  671. #    1. alias - The alias to get its value from
  672. # STDOUT:
  673. #    The value of alias $1, or $1 if there is no alias $1.
  674. # Return value:
  675. #    Always 0
  676. #
  677. function try_alias_value() {
  678.     alias_value "$1" || echo "$1"
  679. }
  680.  
  681. #
  682. # Set variable "$1" to default value "$2" if "$1" is not yet defined.
  683. #
  684. # Arguments:
  685. #    1. name - The variable to set
  686. #    2. val  - The default value
  687. # Return value:
  688. #    0 if the variable exists, 3 if it was set
  689. #
  690. function default() {
  691.     test `typeset +m "$1"` && return 0
  692.     typeset -g "$1"="$2"   && return 3
  693. }
  694.  
  695. #
  696. # Set environment variable "$1" to default value "$2" if "$1" is not yet defined.
  697. #
  698. # Arguments:
  699. #    1. name - The env variable to set
  700. #    2. val  - The default value
  701. # Return value:
  702. #    0 if the env variable exists, 3 if it was set
  703. #
  704. function env_default() {
  705.     env | grep -q "^$1=" && return 0
  706.     export "$1=$2"       && return 3
  707. }
  708.  
  709.  
  710. # Required for $langinfo
  711. zmodload zsh/langinfo
  712.  
  713. # URL-encode a string
  714. #
  715. # Encodes a string using RFC 2396 URL-encoding (%-escaped).
  716. # See: https://www.ietf.org/rfc/rfc2396.txt
  717. #
  718. # By default, reserved characters and unreserved "mark" characters are
  719. # not escaped by this function. This allows the common usage of passing
  720. # an entire URL in, and encoding just special characters in it, with
  721. # the expectation that reserved and mark characters are used appropriately.
  722. # The -r and -m options turn on escaping of the reserved and mark characters,
  723. # respectively, which allows arbitrary strings to be fully escaped for
  724. # embedding inside URLs, where reserved characters might be misinterpreted.
  725. #
  726. # Prints the encoded string on stdout.
  727. # Returns nonzero if encoding failed.
  728. #
  729. # Usage:
  730. #  omz_urlencode [-r] [-m] [-P] <string>
  731. #
  732. #    -r causes reserved characters (;/?:@&=+$,) to be escaped
  733. #
  734. #    -m causes "mark" characters (_.!~*''()-) to be escaped
  735. #
  736. #    -P causes spaces to be encoded as '%20' instead of '+'
  737. function omz_urlencode() {
  738.   emulate -L zsh
  739.   zparseopts -D -E -a opts r m P
  740.  
  741.   local in_str=$1
  742.   local url_str=""
  743.   local spaces_as_plus
  744.   if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
  745.   local str="$in_str"
  746.  
  747.   # URLs must use UTF-8 encoding; convert str to UTF-8 if required
  748.   local encoding=$langinfo[CODESET]
  749.   local safe_encodings
  750.   safe_encodings=(UTF-8 utf8 US-ASCII)
  751.   if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
  752.     str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
  753.     if [[ $? != 0 ]]; then
  754.       echo "Error converting string from $encoding to UTF-8" >&2
  755.       return 1
  756.     fi
  757.   fi
  758.  
  759.   # Use LC_CTYPE=C to process text byte-by-byte
  760.   local i byte ord LC_ALL=C
  761.   export LC_ALL
  762.   local reserved=';/?:@&=+$,'
  763.   local mark='_.!~*''()-'
  764.   local dont_escape="[A-Za-z0-9"
  765.   if [[ -z $opts[(r)-r] ]]; then
  766.     dont_escape+=$reserved
  767.   fi
  768.   # $mark must be last because of the "-"
  769.   if [[ -z $opts[(r)-m] ]]; then
  770.     dont_escape+=$mark
  771.   fi
  772.   dont_escape+="]"
  773.  
  774.   # Implemented to use a single printf call and avoid subshells in the loop,
  775.   # for performance (primarily on Windows).
  776.   local url_str=""
  777.   for (( i = 1; i <= ${#str}; ++i )); do
  778.     byte="$str[i]"
  779.     if [[ "$byte" =~ "$dont_escape" ]]; then
  780.       url_str+="$byte"
  781.     else
  782.       if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
  783.         url_str+="+"
  784.       else
  785.         ord=$(( [##16] #byte ))
  786.         url_str+="%$ord"
  787.       fi
  788.     fi
  789.   done
  790.   echo -E "$url_str"
  791. }
  792.  
  793. # URL-decode a string
  794. #
  795. # Decodes a RFC 2396 URL-encoded (%-escaped) string.
  796. # This decodes the '+' and '%' escapes in the input string, and leaves
  797. # other characters unchanged. Does not enforce that the input is a
  798. # valid URL-encoded string. This is a convenience to allow callers to
  799. # pass in a full URL or similar strings and decode them for human
  800. # presentation.
  801. #
  802. # Outputs the encoded string on stdout.
  803. # Returns nonzero if encoding failed.
  804. #
  805. # Usage:
  806. #   omz_urldecode <urlstring>  - prints decoded string followed by a newline
  807. function omz_urldecode {
  808.   emulate -L zsh
  809.   local encoded_url=$1
  810.  
  811.   # Work bytewise, since URLs escape UTF-8 octets
  812.   local caller_encoding=$langinfo[CODESET]
  813.   local LC_ALL=C
  814.   export LC_ALL
  815.  
  816.   # Change + back to ' '
  817.   local tmp=${encoded_url:gs/+/ /}
  818.   # Protect other escapes to pass through the printf unchanged
  819.   tmp=${tmp:gs/\\/\\\\/}
  820.   # Handle %-escapes by turning them into `\xXX` printf escapes
  821.   tmp=${tmp:gs/%/\\x/}
  822.   local decoded
  823.   eval "decoded=\$'$tmp'"
  824.  
  825.   # Now we have a UTF-8 encoded string in the variable. We need to re-encode
  826.   # it if caller is in a non-UTF-8 locale.
  827.   local safe_encodings
  828.   safe_encodings=(UTF-8 utf8 US-ASCII)
  829.   if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
  830.     decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
  831.     if [[ $? != 0 ]]; then
  832.       echo "Error converting string from UTF-8 to $caller_encoding" >&2
  833.       return 1
  834.     fi
  835.   fi
  836.  
  837.   echo -E "$decoded"
  838. }
  839. # Outputs current branch info in prompt format
  840. function git_prompt_info() {
  841.   local ref
  842.   if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
  843.     ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
  844.     ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
  845.     echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
  846.   fi
  847. }
  848.  
  849. # Checks if working tree is dirty
  850. function parse_git_dirty() {
  851.   local STATUS=''
  852.   local -a FLAGS
  853.   FLAGS=('--porcelain')
  854.   if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]; then
  855.     if [[ $POST_1_7_2_GIT -gt 0 ]]; then
  856.       FLAGS+='--ignore-submodules=dirty'
  857.     fi
  858.     if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then
  859.       FLAGS+='--untracked-files=no'
  860.     fi
  861.     STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
  862.   fi
  863.   if [[ -n $STATUS ]]; then
  864.     echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
  865.   else
  866.     echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
  867.   fi
  868. }
  869.  
  870. # Gets the difference between the local and remote branches
  871. function git_remote_status() {
  872.     local remote ahead behind git_remote_status git_remote_status_detailed
  873.     remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
  874.     if [[ -n ${remote} ]]; then
  875.         ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
  876.         behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
  877.  
  878.         if [[ $ahead -eq 0 ]] && [[ $behind -eq 0 ]]; then
  879.             git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
  880.         elif [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]; then
  881.             git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
  882.             git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
  883.         elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]; then
  884.             git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
  885.             git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  886.         elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]; then
  887.             git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
  888.             git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
  889.         fi
  890.  
  891.         if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
  892.             git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
  893.         fi
  894.  
  895.         echo $git_remote_status
  896.     fi
  897. }
  898.  
  899. # Outputs the name of the current branch
  900. # Usage example: git pull origin $(git_current_branch)
  901. # Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
  902. # it's not a symbolic ref, but in a Git repo.
  903. function git_current_branch() {
  904.   local ref
  905.   ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
  906.   local ret=$?
  907.   if [[ $ret != 0 ]]; then
  908.     [[ $ret == 128 ]] && return  # no git repo.
  909.     ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
  910.   fi
  911.   echo ${ref#refs/heads/}
  912. }
  913.  
  914.  
  915. # Gets the number of commits ahead from remote
  916. function git_commits_ahead() {
  917.   if command git rev-parse --git-dir &>/dev/null; then
  918.     local commits="$(git rev-list --count @{upstream}..HEAD 2>/dev/null)"
  919.     if [[ -n "$commits" && "$commits" != 0 ]]; then
  920.       echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
  921.     fi
  922.   fi
  923. }
  924.  
  925. # Gets the number of commits behind remote
  926. function git_commits_behind() {
  927.   if command git rev-parse --git-dir &>/dev/null; then
  928.     local commits="$(git rev-list --count HEAD..@{upstream} 2>/dev/null)"
  929.     if [[ -n "$commits" && "$commits" != 0 ]]; then
  930.       echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
  931.     fi
  932.   fi
  933. }
  934.  
  935. # Outputs if current branch is ahead of remote
  936. function git_prompt_ahead() {
  937.   if [[ -n "$(command git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
  938.     echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
  939.   fi
  940. }
  941.  
  942. # Outputs if current branch is behind remote
  943. function git_prompt_behind() {
  944.   if [[ -n "$(command git rev-list HEAD..origin/$(git_current_branch) 2> /dev/null)" ]]; then
  945.     echo "$ZSH_THEME_GIT_PROMPT_BEHIND"
  946.   fi
  947. }
  948.  
  949. # Outputs if current branch exists on remote or not
  950. function git_prompt_remote() {
  951.   if [[ -n "$(command git show-ref origin/$(git_current_branch) 2> /dev/null)" ]]; then
  952.     echo "$ZSH_THEME_GIT_PROMPT_REMOTE_EXISTS"
  953.   else
  954.     echo "$ZSH_THEME_GIT_PROMPT_REMOTE_MISSING"
  955.   fi
  956. }
  957.  
  958. # Formats prompt string for current git commit short SHA
  959. function git_prompt_short_sha() {
  960.   local SHA
  961.   SHA=$(command git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  962. }
  963.  
  964. # Formats prompt string for current git commit long SHA
  965. function git_prompt_long_sha() {
  966.   local SHA
  967.   SHA=$(command git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
  968. }
  969.  
  970. # Get the status of the working tree
  971. function git_prompt_status() {
  972.   local INDEX STATUS
  973.   INDEX=$(command git status --porcelain -b 2> /dev/null)
  974.   STATUS=""
  975.   if $(echo "$INDEX" | command grep -E '^\?\? ' &> /dev/null); then
  976.     STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
  977.   fi
  978.   if $(echo "$INDEX" | grep '^A  ' &> /dev/null); then
  979.     STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  980.   elif $(echo "$INDEX" | grep '^M  ' &> /dev/null); then
  981.     STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  982.   elif $(echo "$INDEX" | grep '^MM ' &> /dev/null); then
  983.     STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
  984.   fi
  985.   if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
  986.     STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  987.   elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
  988.     STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  989.   elif $(echo "$INDEX" | grep '^MM ' &> /dev/null); then
  990.     STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  991.   elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
  992.     STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
  993.   fi
  994.   if $(echo "$INDEX" | grep '^R  ' &> /dev/null); then
  995.     STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
  996.   fi
  997.   if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
  998.     STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  999.   elif $(echo "$INDEX" | grep '^D  ' &> /dev/null); then
  1000.     STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  1001.   elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
  1002.     STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
  1003.   fi
  1004.   if $(command git rev-parse --verify refs/stash >/dev/null 2>&1); then
  1005.     STATUS="$ZSH_THEME_GIT_PROMPT_STASHED$STATUS"
  1006.   fi
  1007.   if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
  1008.     STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
  1009.   fi
  1010.   if $(echo "$INDEX" | grep '^## [^ ]\+ .*ahead' &> /dev/null); then
  1011.     STATUS="$ZSH_THEME_GIT_PROMPT_AHEAD$STATUS"
  1012.   fi
  1013.   if $(echo "$INDEX" | grep '^## [^ ]\+ .*behind' &> /dev/null); then
  1014.     STATUS="$ZSH_THEME_GIT_PROMPT_BEHIND$STATUS"
  1015.   fi
  1016.   if $(echo "$INDEX" | grep '^## [^ ]\+ .*diverged' &> /dev/null); then
  1017.     STATUS="$ZSH_THEME_GIT_PROMPT_DIVERGED$STATUS"
  1018.   fi
  1019.   echo $STATUS
  1020. }
  1021.  
  1022. # Compares the provided version of git to the version installed and on path
  1023. # Outputs -1, 0, or 1 if the installed version is less than, equal to, or
  1024. # greater than the input version, respectively.
  1025. function git_compare_version() {
  1026.   local INPUT_GIT_VERSION INSTALLED_GIT_VERSION
  1027.   INPUT_GIT_VERSION=(${(s/./)1})
  1028.   INSTALLED_GIT_VERSION=($(command git --version 2>/dev/null))
  1029.   INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]})
  1030.  
  1031.   for i in {1..3}; do
  1032.     if [[ $INSTALLED_GIT_VERSION[$i] -gt $INPUT_GIT_VERSION[$i] ]]; then
  1033.       echo 1
  1034.       return 0
  1035.     fi
  1036.     if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
  1037.       echo -1
  1038.       return 0
  1039.     fi
  1040.   done
  1041.   echo 0
  1042. }
  1043.  
  1044. # Outputs the name of the current user
  1045. # Usage example: $(git_current_user_name)
  1046. function git_current_user_name() {
  1047.   command git config user.name 2>/dev/null
  1048. }
  1049.  
  1050. # Outputs the email of the current user
  1051. # Usage example: $(git_current_user_email)
  1052. function git_current_user_email() {
  1053.   command git config user.email 2>/dev/null
  1054. }
  1055.  
  1056. # This is unlikely to change so make it all statically assigned
  1057. POST_1_7_2_GIT=$(git_compare_version "1.7.2")
  1058. # Clean up the namespace slightly by removing the checker function
  1059. unfunction git_compare_version
  1060. # is x grep argument available?
  1061. grep-flag-available() {
  1062.     echo | grep $1 "" >/dev/null 2>&1
  1063. }
  1064.  
  1065. GREP_OPTIONS=""
  1066.  
  1067. # color grep results
  1068. if grep-flag-available --color=auto; then
  1069.     GREP_OPTIONS+=" --color=auto"
  1070. fi
  1071.  
  1072. # ignore VCS folders (if the necessary grep flags are available)
  1073. VCS_FOLDERS="{.bzr,CVS,.git,.hg,.svn}"
  1074.  
  1075. if grep-flag-available --exclude-dir=.cvs; then
  1076.     GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS"
  1077. elif grep-flag-available --exclude=.cvs; then
  1078.     GREP_OPTIONS+=" --exclude=$VCS_FOLDERS"
  1079. fi
  1080.  
  1081. # export grep settings
  1082. alias grep="grep $GREP_OPTIONS"
  1083.  
  1084. # clean up
  1085. unset GREP_OPTIONS
  1086. unset VCS_FOLDERS
  1087. unfunction grep-flag-available
  1088. ## History wrapper
  1089. function omz_history {
  1090.   # Delete the history file if `-c' argument provided.
  1091.   # This won't affect the `history' command output until the next login.
  1092.   zparseopts -E c=clear l=list
  1093.  
  1094.   if [[ -n "$clear" ]]; then
  1095.     # if -c provided, clobber the history file
  1096.     echo -n >| "$HISTFILE"
  1097.     echo >&2 History file deleted. Reload the session to see its effects.
  1098.   elif [[ -n "$list" ]]; then
  1099.     # if -l provided, run as if calling `fc' directly
  1100.     builtin fc "$@"
  1101.   else
  1102.     # otherwise, call `fc -l 1` to show all available
  1103.     # history (and pass additional parameters)
  1104.     builtin fc "$@" -l 1
  1105.   fi
  1106. }
  1107.  
  1108. # Timestamp format
  1109. case $HIST_STAMPS in
  1110.   "mm/dd/yyyy") alias history='omz_history -f' ;;
  1111.   "dd.mm.yyyy") alias history='omz_history -E' ;;
  1112.   "yyyy-mm-dd") alias history='omz_history -i' ;;
  1113.   "") alias history='omz_history' ;;
  1114.   *) alias history="omz_history -t '$HIST_STAMPS'" ;;
  1115. esac
  1116.  
  1117. ## History file configuration
  1118. [ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history"
  1119. HISTSIZE=50000
  1120. SAVEHIST=10000
  1121.  
  1122. ## History command configuration
  1123. setopt extended_history       # record timestamp of command in HISTFILE
  1124. setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
  1125. setopt hist_ignore_dups       # ignore duplicated commands history list
  1126. setopt hist_ignore_space      # ignore commands that start with space
  1127. setopt hist_verify            # show command with history expansion to user before running it
  1128. setopt inc_append_history     # add commands to HISTFILE in order of execution
  1129. setopt share_history          # share command history data
  1130. # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html
  1131. # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Builtins
  1132. # http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Standard-Widgets
  1133.  
  1134. # Make sure that the terminal is in application mode when zle is active, since
  1135. # only then values from $terminfo are valid
  1136. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  1137.   function zle-line-init() {
  1138.     echoti smkx
  1139.   }
  1140.   function zle-line-finish() {
  1141.     echoti rmkx
  1142.   }
  1143.   zle -N zle-line-init
  1144.   zle -N zle-line-finish
  1145. fi
  1146.  
  1147. bindkey -e                                            # Use emacs key bindings
  1148.  
  1149. bindkey '\ew' kill-region                             # [Esc-w] - Kill from the cursor to the mark
  1150. bindkey -s '\el' 'ls\n'                               # [Esc-l] - run command: ls
  1151. bindkey '^r' history-incremental-search-backward      # [Ctrl-r] - Search backward incrementally for a specified string. The string may begin with ^ to anchor the search to the beginning of the line.
  1152. if [[ "${terminfo[kpp]}" != "" ]]; then
  1153.   bindkey "${terminfo[kpp]}" up-line-or-history       # [PageUp] - Up a line of history
  1154. fi
  1155. if [[ "${terminfo[knp]}" != "" ]]; then
  1156.   bindkey "${terminfo[knp]}" down-line-or-history     # [PageDown] - Down a line of history
  1157. fi
  1158.  
  1159. # start typing + [Up-Arrow] - fuzzy find history forward
  1160. if [[ "${terminfo[kcuu1]}" != "" ]]; then
  1161.   autoload -U up-line-or-beginning-search
  1162.   zle -N up-line-or-beginning-search
  1163.   bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
  1164. fi
  1165. # start typing + [Down-Arrow] - fuzzy find history backward
  1166. if [[ "${terminfo[kcud1]}" != "" ]]; then
  1167.   autoload -U down-line-or-beginning-search
  1168.   zle -N down-line-or-beginning-search
  1169.   bindkey "${terminfo[kcud1]}" down-line-or-beginning-search
  1170. fi
  1171.  
  1172. if [[ "${terminfo[khome]}" != "" ]]; then
  1173.   bindkey "${terminfo[khome]}" beginning-of-line      # [Home] - Go to beginning of line
  1174. fi
  1175. if [[ "${terminfo[kend]}" != "" ]]; then
  1176.   bindkey "${terminfo[kend]}"  end-of-line            # [End] - Go to end of line
  1177. fi
  1178.  
  1179. bindkey ' ' magic-space                               # [Space] - do history expansion
  1180.  
  1181. bindkey '^[[1;5C' forward-word                        # [Ctrl-RightArrow] - move forward one word
  1182. bindkey '^[[1;5D' backward-word                       # [Ctrl-LeftArrow] - move backward one word
  1183.  
  1184. if [[ "${terminfo[kcbt]}" != "" ]]; then
  1185.   bindkey "${terminfo[kcbt]}" reverse-menu-complete   # [Shift-Tab] - move through the completion menu backwards
  1186. fi
  1187.  
  1188. bindkey '^?' backward-delete-char                     # [Backspace] - delete backward
  1189. if [[ "${terminfo[kdch1]}" != "" ]]; then
  1190.   bindkey "${terminfo[kdch1]}" delete-char            # [Delete] - delete forward
  1191. else
  1192.   bindkey "^[[3~" delete-char
  1193.   bindkey "^[3;5~" delete-char
  1194.   bindkey "\e[3~" delete-char
  1195. fi
  1196.  
  1197. # Edit the current command line in $EDITOR
  1198. autoload -U edit-command-line
  1199. zle -N edit-command-line
  1200. bindkey '\C-x\C-e' edit-command-line
  1201.  
  1202. # file rename magick
  1203. bindkey "^[m" copy-prev-shell-word
  1204.  
  1205. # consider emacs keybindings:
  1206.  
  1207. #bindkey -e  ## emacs key bindings
  1208. #
  1209. #bindkey '^[[A' up-line-or-search
  1210. #bindkey '^[[B' down-line-or-search
  1211. #bindkey '^[^[[C' emacs-forward-word
  1212. #bindkey '^[^[[D' emacs-backward-word
  1213. #
  1214. #bindkey -s '^X^Z' '%-^M'
  1215. #bindkey '^[e' expand-cmd-path
  1216. #bindkey '^[^I' reverse-menu-complete
  1217. #bindkey '^X^N' accept-and-infer-next-history
  1218. #bindkey '^W' kill-region
  1219. #bindkey '^I' complete-word
  1220. ## Fix weird sequence that rxvt produces
  1221. #bindkey -s '^[[Z' '\t'
  1222. #
  1223. ## Load smart urls if available
  1224. # bracketed-paste-magic is known buggy in zsh 5.1.1 (only), so skip it there; see #4434
  1225. autoload -Uz is-at-least
  1226. if [[ $ZSH_VERSION != 5.1.1 ]]; then
  1227.   for d in $fpath; do
  1228.     if [[ -e "$d/url-quote-magic" ]]; then
  1229.         if is-at-least 5.1; then
  1230.             autoload -Uz bracketed-paste-magic
  1231.             zle -N bracketed-paste bracketed-paste-magic
  1232.         fi
  1233.         autoload -Uz url-quote-magic
  1234.         zle -N self-insert url-quote-magic
  1235.       break
  1236.     fi
  1237.   done
  1238. fi
  1239.  
  1240. ## jobs
  1241. setopt long_list_jobs
  1242.  
  1243. ## pager
  1244. env_default PAGER 'less'
  1245. env_default LESS '-R'
  1246.  
  1247. ## super user alias
  1248. alias _='sudo'
  1249. alias please='sudo'
  1250.  
  1251. ## more intelligent acking for ubuntu users
  1252. if which ack-grep &> /dev/null; then
  1253.   alias afind='ack-grep -il'
  1254. else
  1255.   alias afind='ack -il'
  1256. fi
  1257.  
  1258. # only define LC_CTYPE if undefined
  1259. if [[ -z "$LC_CTYPE" && -z "$LC_ALL" ]]; then
  1260.     export LC_CTYPE=${LANG%%:*} # pick the first entry from LANG
  1261. fi
  1262.  
  1263. # recognize comments
  1264. setopt interactivecomments
  1265. # get the node.js version
  1266. function nvm_prompt_info() {
  1267.   [[ -f "$NVM_DIR/nvm.sh" ]] || return
  1268.   local nvm_prompt
  1269.   nvm_prompt=$(node -v 2>/dev/null)
  1270.   [[ "${nvm_prompt}x" == "x" ]] && return
  1271.   nvm_prompt=${nvm_prompt:1}
  1272.   echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}"
  1273. }
  1274. # *_prompt_info functions for usage in your prompt
  1275. #
  1276. # Plugin creators, please add your *_prompt_info function to the list
  1277. # of dummy implementations to help theme creators not receiving errors
  1278. # without the need of implementing conditional clauses.
  1279. #
  1280. # See also lib/bzr.zsh, lib/git.zsh and lib/nvm.zsh for
  1281. # git_prompt_info, bzr_prompt_info and nvm_prompt_info
  1282.  
  1283. # Dummy implementations that return false to prevent command_not_found
  1284. # errors with themes, that implement these functions
  1285. # Real implementations will be used when the respective plugins are loaded
  1286. function chruby_prompt_info hg_prompt_info pyenv_prompt_info \
  1287.   rbenv_prompt_info svn_prompt_info vi_mode_prompt_info \
  1288.   virtualenv_prompt_info jenv_prompt_info {
  1289.   return 1
  1290. }
  1291.  
  1292. # oh-my-zsh supports an rvm prompt by default
  1293. # get the name of the rvm ruby version
  1294. function rvm_prompt_info() {
  1295.   [ -f $HOME/.rvm/bin/rvm-prompt ] || return 1
  1296.   local rvm_prompt
  1297.   rvm_prompt=$($HOME/.rvm/bin/rvm-prompt ${=ZSH_THEME_RVM_PROMPT_OPTIONS} 2>/dev/null)
  1298.   [[ "${rvm_prompt}x" == "x" ]] && return 1
  1299.   echo "${ZSH_THEME_RVM_PROMPT_PREFIX:=(}${rvm_prompt}${ZSH_THEME_RVM_PROMPT_SUFFIX:=)}"
  1300. }
  1301.  
  1302. # use this to enable users to see their ruby version, no matter which
  1303. # version management system they use
  1304. function ruby_prompt_info() {
  1305.   echo $(rvm_prompt_info || rbenv_prompt_info || chruby_prompt_info)
  1306. }
  1307. #! /bin/zsh
  1308. # A script to make using 256 colors in zsh less painful.
  1309. # P.C. Shyamshankar <sykora@lucentbeing.com>
  1310. # Copied from https://github.com/sykora/etc/blob/master/zsh/functions/spectrum/
  1311.  
  1312. typeset -AHg FX FG BG
  1313.  
  1314. FX=(
  1315.     reset     "%{%}"
  1316.     bold      "%{%}" no-bold      "%{%}"
  1317.     italic    "%{%}" no-italic    "%{%}"
  1318.     underline "%{%}" no-underline "%{%}"
  1319.     blink     "%{%}" no-blink     "%{%}"
  1320.     reverse   "%{%}" no-reverse   "%{%}"
  1321. )
  1322.  
  1323. for color in {000..255}; do
  1324.     FG[$color]="%{[38;5;${color}m%}"
  1325.     BG[$color]="%{[48;5;${color}m%}"
  1326. done
  1327.  
  1328.  
  1329. ZSH_SPECTRUM_TEXT=${ZSH_SPECTRUM_TEXT:-Arma virumque cano Troiae qui primus ab oris}
  1330.  
  1331. # Show all 256 colors with color number
  1332. function spectrum_ls() {
  1333.   for code in {000..255}; do
  1334.     print -P -- "$code: %{$FG[$code]%}$ZSH_SPECTRUM_TEXT%{$reset_color%}"
  1335.   done
  1336. }
  1337.  
  1338. # Show all 256 colors where the background is set to specific color
  1339. function spectrum_bls() {
  1340.   for code in {000..255}; do
  1341.     print -P -- "$code: %{$BG[$code]%}$ZSH_SPECTRUM_TEXT%{$reset_color%}"
  1342.   done
  1343. }
  1344. # Set terminal window and tab/icon title
  1345. #
  1346. # usage: title short_tab_title [long_window_title]
  1347. #
  1348. # See: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss3.1
  1349. # Fully supports screen, iterm, and probably most modern xterm and rxvt
  1350. # (In screen, only short_tab_title is used)
  1351. # Limited support for Apple Terminal (Terminal can't set window and tab separately)
  1352. function title {
  1353.   emulate -L zsh
  1354.   setopt prompt_subst
  1355.  
  1356.   [[ "$EMACS" == *term* ]] && return
  1357.  
  1358.   # if $2 is unset use $1 as default
  1359.   # if it is set and empty, leave it as is
  1360.   : ${2=$1}
  1361.  
  1362.   case "$TERM" in
  1363.     cygwin|xterm*|putty*|rxvt*|ansi)
  1364.       print -Pn "\e]2;$2:q\a" # set window name
  1365.       print -Pn "\e]1;$1:q\a" # set tab name
  1366.       ;;
  1367.     screen*)
  1368.       print -Pn "\ek$1:q\e\\" # set screen hardstatus
  1369.      ;;
  1370.    *)
  1371.      if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
  1372.        print -Pn "\e]2;$2:q\a" # set window name
  1373.        print -Pn "\e]1;$1:q\a" # set tab name
  1374.      else
  1375.        # Try to use terminfo to set the title
  1376.        # If the feature is available set title
  1377.        if [[ -n "$terminfo[fsl]" ]] && [[ -n "$terminfo[tsl]" ]]; then
  1378.       echoti tsl
  1379.       print -Pn "$1"
  1380.       echoti fsl
  1381.     fi
  1382.      fi
  1383.      ;;
  1384.  esac
  1385. }
  1386.  
  1387. ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
  1388. ZSH_THEME_TERM_TITLE_IDLE="%n@%m: %~"
  1389. # Avoid duplication of directory in terminals with independent dir display
  1390. if [[ "$TERM_PROGRAM" == Apple_Terminal ]]; then
  1391.  ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
  1392. fi
  1393.  
  1394. # Runs before showing the prompt
  1395. function omz_termsupport_precmd {
  1396.  emulate -L zsh
  1397.  
  1398.  if [[ "$DISABLE_AUTO_TITLE" == true ]]; then
  1399.    return
  1400.  fi
  1401.  
  1402.  title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
  1403. }
  1404.  
  1405. # Runs before executing the command
  1406. function omz_termsupport_preexec {
  1407.  emulate -L zsh
  1408.  setopt extended_glob
  1409.  
  1410.  if [[ "$DISABLE_AUTO_TITLE" == true ]]; then
  1411.    return
  1412.  fi
  1413.  
  1414.  # cmd name only, or if this is sudo or ssh, the next cmd
  1415.  local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
  1416.  local LINE="${2:gs/%/%%}"
  1417.  
  1418.  title '$CMD' '%100>...>$LINE%<<'
  1419. }
  1420.  
  1421. precmd_functions+=(omz_termsupport_precmd)
  1422. preexec_functions+=(omz_termsupport_preexec)
  1423.  
  1424.  
  1425. # Keep Apple Terminal.app's current working directory updated
  1426. # Based on this answer: https://superuser.com/a/315029
  1427. # With extra fixes to handle multibyte chars and non-UTF-8 locales
  1428.  
  1429. if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
  1430.  # Emits the control sequence to notify Terminal.app of the cwd
  1431.  # Identifies the directory using a file: URI scheme, including
  1432.  # the host name to disambiguate local vs. remote paths.
  1433.  function update_terminalapp_cwd() {
  1434.    emulate -L zsh
  1435.  
  1436.    # Percent-encode the pathname.
  1437.    local URL_PATH="$(omz_urlencode -P $PWD)"
  1438.    [[ $? != 0 ]] && return 1
  1439.  
  1440.    # Undocumented Terminal.app-specific control sequence
  1441.    printf '\e]7;%s\a' "file://$HOST$URL_PATH"
  1442.  }
  1443.  
  1444.  # Use a precmd hook instead of a chpwd hook to avoid contaminating output
  1445.  precmd_functions+=(update_terminalapp_cwd)
  1446.  # Run once to get initial cwd set
  1447.  update_terminalapp_cwd
  1448. fi
  1449. # ls colors
  1450. autoload -U colors && colors
  1451.  
  1452. # Enable ls colors
  1453. export LSCOLORS="Gxfxcxdxbxegedabagacad"
  1454.  
  1455. # TODO organise this chaotic logic
  1456.  
  1457. if [[ "$DISABLE_LS_COLORS" != "true" ]]; then
  1458.  # Find the option for using colors in ls, depending on the version
  1459.  if [[ "$OSTYPE" == netbsd* ]]; then
  1460.    # On NetBSD, test if "gls" (GNU ls) is installed (this one supports colors);
  1461.    # otherwise, leave ls as is, because NetBSD's ls doesn't support -G
  1462.    gls --color -d . &>/dev/null && alias ls='gls --color=tty'
  1463.  elif [[ "$OSTYPE" == openbsd* ]]; then
  1464.    # On OpenBSD, "gls" (ls from GNU coreutils) and "colorls" (ls from base,
  1465.    # with color and multibyte support) are available from ports.  "colorls"
  1466.    # will be installed on purpose and can't be pulled in by installing
  1467.    # coreutils, so prefer it to "gls".
  1468.    gls --color -d . &>/dev/null && alias ls='gls --color=tty'
  1469.    colorls -G -d . &>/dev/null && alias ls='colorls -G'
  1470.  elif [[ "$OSTYPE" == darwin* ]]; then
  1471.    # this is a good alias, it works by default just using $LSCOLORS
  1472.    ls -G . &>/dev/null && alias ls='ls -G'
  1473.  
  1474.    # only use coreutils ls if there is a dircolors customization present ($LS_COLORS or .dircolors file)
  1475.    # otherwise, gls will use the default color scheme which is ugly af
  1476.    [[ -n "$LS_COLORS" || -f "$HOME/.dircolors" ]] && gls --color -d . &>/dev/null && alias ls='gls --color=tty'
  1477.  else
  1478.    # For GNU ls, we use the default ls color theme. They can later be overwritten by themes.
  1479.    if [[ -z "$LS_COLORS" ]]; then
  1480.      (( $+commands[dircolors] )) && eval "$(dircolors -b)"
  1481.    fi
  1482.  
  1483.    ls --color -d . &>/dev/null && alias ls='ls --color=tty' || { ls -G . &>/dev/null && alias ls='ls -G' }
  1484.  
  1485.    # Take advantage of $LS_COLORS for completion as well.
  1486.    zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
  1487.  fi
  1488. fi
  1489.  
  1490. setopt auto_cd
  1491. setopt multios
  1492. setopt prompt_subst
  1493.  
  1494. [[ -n "$WINDOW" ]] && SCREEN_NO="%B$WINDOW%b " || SCREEN_NO=""
  1495.  
  1496. # Apply theming defaults
  1497. PS1="%n@%m:%~%# "
  1498.  
  1499. # git theming default: Variables for theming the git info prompt
  1500. ZSH_THEME_GIT_PROMPT_PREFIX="git:("         # Prefix at the very beginning of the prompt, before the branch name
  1501. ZSH_THEME_GIT_PROMPT_SUFFIX=")"             # At the very end of the prompt
  1502. ZSH_THEME_GIT_PROMPT_DIRTY="*"              # Text to display if the branch is dirty
  1503. ZSH_THEME_GIT_PROMPT_CLEAN=""               # Text to display if the branch is clean
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement