history.zsh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ## History wrapper
  2. function omz_history {
  3. # parse arguments and remove from $@
  4. local clear list stamp REPLY
  5. zparseopts -E -D c=clear l=list f=stamp E=stamp i=stamp t:=stamp
  6. if [[ -n "$clear" ]]; then
  7. # if -c provided, clobber the history file
  8. # confirm action before deleting history
  9. print -nu2 "This action will irreversibly delete your command history. Are you sure? [y/N] "
  10. builtin read
  11. [[ "$REPLY" = [yY] ]] || return 0
  12. print -nu2 >| "$HISTFILE"
  13. fc -p "$HISTFILE"
  14. print -u2 History file deleted.
  15. elif [[ $# -eq 0 ]]; then
  16. # if no arguments provided, show full history starting from 1
  17. builtin fc $stamp -l 1
  18. else
  19. # otherwise, run `fc -l` with a custom format
  20. builtin fc $stamp -l "$@"
  21. fi
  22. }
  23. # Timestamp format
  24. case ${HIST_STAMPS-} in
  25. "mm/dd/yyyy") alias history='omz_history -f' ;;
  26. "dd.mm.yyyy") alias history='omz_history -E' ;;
  27. "yyyy-mm-dd") alias history='omz_history -i' ;;
  28. "") alias history='omz_history' ;;
  29. *) alias history="omz_history -t '$HIST_STAMPS'" ;;
  30. esac
  31. ## History file configuration
  32. [ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history"
  33. [ "$HISTSIZE" -lt 50000 ] && HISTSIZE=50000
  34. [ "$SAVEHIST" -lt 10000 ] && SAVEHIST=10000
  35. ## History command configuration
  36. setopt extended_history # record timestamp of command in HISTFILE
  37. setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
  38. setopt hist_ignore_dups # ignore duplicated commands history list
  39. setopt hist_ignore_space # ignore commands that start with space
  40. setopt hist_verify # show command with history expansion to user before running it
  41. setopt share_history # share command history data