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