[Home] Zsh logo

zed

Zsh Wizard

Download zed Return to Examples
  1. # zed(): Peter Stephenson <pws@s-a.amtp.liv.ac.uk>
  2. # No other shell could do this.
  3. # Edit small files with the command line editor.
  4. # Use ^X^W to save, ^C to abort.
  5. # Option -f: edit shell functions. (Also if called as fned.)
  6. #
  7. # Completion: use
  8. # compctl -f -x 'w[1,-f]' -F -- zed
  9. local var fun cleanup
  10. # We do not want timeout while we are editing a file
  11. integer TMOUT=0
  12. [[ $1 = -f || $0 = fned ]] && fun=1
  13. [[ $1 = -(|-|f) ]] && shift
  14. [[ -z "$1" ]] && echo 'Usage: "zed filename" or "zed -f function"' && return 1
  15. # catch interrupts
  16. cleanup="$(bindkey -L "^M"; bindkey -L -M emacs "^X^W"; bindkey -aL "ZZ"
  17. echo "trap - INT EXIT"; trap)"
  18. trap "return 130" INT
  19. trap "$cleanup" EXIT
  20. # don't mangle !'s
  21. setopt localoptions nobanghist
  22. bindkey "^M" self-insert-unmeta
  23. # Depending on your stty's, you may be able to use ^J as accept-line, else:
  24. bindkey -M emacs "^X^W" accept-line
  25. bindkey -a "ZZ" accept-line
  26. if ((fun)) then
  27. var="$(functions $1)"
  28. # If function is undefined but autoloadable, load it
  29. if [[ $var = undefined* ]] then
  30. local dir
  31. for dir in $fpath; do
  32. if [[ -f $dir/$1 ]] then
  33. var="$1() {
  34. $(<$dir/$1)
  35. }"
  36. break
  37. fi
  38. done
  39. elif [[ -z $var ]] then
  40. var="$1() {
  41. }"
  42. fi
  43. vared var && eval function "$var"
  44. else
  45. [[ -f $1 ]] && var="$(<$1)"
  46. while vared var
  47. do
  48. (print -r -- "$var" >| $1) && break
  49. echo -n -e '\a'
  50. done
  51. fi
  52. return 0
  53. # End of zed