ARCHIVE OF ZSH TRICKS AND SHORTCUTS Replicates key digit times. Alt-- Send to both stdout and a file. prog > /dev/stdout > output_file Expand to last arg of command n times ago. !-n$ Expands to the dth argument in last command. !:d Background a remote job even after logout. nohup prog &> /dev/null < /dev/null &! Execute code when a script finishes. my-exit-fn() { # stuff } trap my-exit-fn EXIT Insert text before every array element. files=(*.ext) mutt {-a,$^files} # ---------------------------------------------------------------------------- BINDINGS Esc-h run man page for command Esc-x execute named widget # ---------------------------------------------------------------------------- Use zmv to move files according to a pattern. autoload zmv Rename files by transposing the text on either side of the underscore. zmv -n '(*)_(*).doc' '$2_$1.doc' Rename files by inserting a leading 0. zmv 'page([0-9][0-9][0-9][0-9]).txt' 'page0$1.txt' Lowercase file names. zmv '(*).f5' '${(L)1}.f5' # ---------------------------------------------------------------------------- Make all files below dir entirely readable and all directories readable and executable. chmod a+r dir/**/*(.) chmod a+rx dir/**/*(/) # ---------------------------------------------------------------------------- Replace spaces in filenames with underscores. for i in *\ *; do mv "$i" ${i:gs/ /_/}; done # ---------------------------------------------------------------------------- ID=CS594S02; \ pavuk -mode sync -dont_leave_dir \ -dont_leave_site_enter_dir -user_condition \ =(chmod u+x /dev/fd/1; \ echo '#!/bin/sh'; \ echo 'echo $2 | \ awk "{if (match($0, ''"'$ID'"'')){exit 1;} else {exit 0;} }"') \ http://www.cs.utk.edu/~huangj/CS594S02/Schedule.html # ---------------------------------------------------------------------------- To grab files not matching a pattern, use the caret before the pattern: ls ^*.o To show only non-source files, try: ls *~*.cpp~*.h ls ^(*.cpp|*.h) # ---------------------------------------------------------------------------- Glob Qualifers - following a glob pattern with qualifiers enclosed in parentheses to limit the returned filenames. For example: ls *(/) - view directories Here's a list of some useful qualifiers: / Match directories . Match plain files @ Match symlinks * Match executable plain files ^ Negate following qualifiers on sort by name (for ls, try ls -U so that ls doesn't resort) On in reverse u[username] # ---------------------------------------------------------------------------- To erase a file, run: : > file The colon tells the shell to ignore the result. The rest of the command is just a redirection which erases the contents of file. # ---------------------------------------------------------------------------- To redirect both stderr and stdout, use &>: ls &>/dev/null # ---------------------------------------------------------------------------- This little shell script replaces all symbolic links which point to files with hardlinks. for i in *(@); do \ fullpath=$(perl -e 'use Cwd realpath; print realpath($ARGV[0]);' $i); \ if [[ ! -d $fullpath ]]; then \ echo ln -f $fullpath $i \ fi \ done # ---------------------------------------------------------------------------- myprog | tee file.txt is equivalent to myprog >file.txt >/dev/fd/0