Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  * Some zsh tips
  ** Zero-pad (preserve zeroes) a variable
  Either
  *** typeset -Z2 FOO
  *** printf %02 $FOO
  ** Do arithmetic
  *** examples
  **** addition
  (( count = $count + 1 ))
  ** Loop through a directory of long filenames with spaces
  while read -r line
  do
    echo "'$line'"
  done < <(print -l *(/))
  ** Shell return codes for piped commands
  echo foo | grep bar | tr z a | cat
  echo ${PIPESTATUS[@]}
  
  0 1 0 0
  ** Pass file contents as an argument
  *** foo "$(<bar)"
  *** The above is more idiomatic than:  foo "$(cat bar)"
  ** Sort files numerically
  *** files=(*); command ${(n)files}
  ** Get the last argument passed to a zsh script
  *** $@[-1] or $@[$#]
  *** you could also use $argv if $@ seems too weird
  ** exec an alias
  *** alias exec='exec '
  *** an alias ending in space causes further alias expansion for the next arg
  ** Start zsh without inhereting any environment variables or sourcing zsh init files
  *** env -i zsh -f
  ** Which shell are you in
  *** echo $0
  *** Check ZSH_VERSION:
         if [ -n "${BASH_VERSION+s}" ]; then
             echo 'this is bash'
         elif [ "${ZSH_VERSION+s}" ]; then
             echo 'this is zsh'
         fi
  ** Find files/dirs between certain dates
  *** First:
  zmodload zsh/stat
  autoload age
  *** For files
  **** print -l *(.e#age 2015/01/01 2016/01/01#)
  *** For directories
  **** print -l *(/e#age 2015/01/01 2016/01/01#)
  ** make ^R work in zsh
  bindkey -M viins "^R" history-incremental-search-backward
  ** Search for lots of files
  Will not run in to argument number limits
  
    zargs -- **/bar -- grep foo
  ** Remove leading and trailing spaces in a variable
  First, make sure to "setopt EXENDED_GLOB" in your script!
  
  Then:
  *** Remove leading spaces
  ${FOO## #}
  *** Remove trailing spaces
  ${FOO%% #}
  ** Edit a variable using your EDITOR
  vared FOO
  ** Find files in directory depth of one
  print -l */foo
  ** List all files except for 'foobar'
  print -l *~foobar
  ** List all plain files except for 'foobar'
     print -l *~foobar(.)
  ** List all files starting with 'foo' except for 'foobar'
     print -l foo*~foobar
  ** List all files starting with "tex" but not starting with "text"
     print -l tex*~*text*
  ** List all files which are not symbolic links
  print -l *(^@)
  ** Load zsh modules
  *** zmodload zsh/attr


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: