about summary refs log tree commit diff stats
path: root/bash/history.bash
blob: d2bdcebf1ad998b5c66386eff9ae2495af1dd1ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Bash history settings
# I don't export any variables in this file because history settings
# really only apply in an interactive session.

# XDG compliance
HISTFILE="$XDG_DATA_HOME/bash/history"
mkdir -p "$XDG_DATA_HOME/bash"

# Don't truncate history
HISTFILESIZE=-1 # numeric values less than zero => don't truncate
HISTSIZE=100000 # ideally, I wouldn't truncate at all, but after a while shell
# startup might be affected.

# Append the history to HISTFILE.
shopt -s histappend

# History editing with readline
shopt -s histreedit # allow re-editing of a failed history substitution
shopt -s histverify # verify a history expansion before running it

# Save command invocation time to HISTFILE, and format it thusly
HISTTIMEFORMAT="%F %T "

# Erase all duplicates before saving the current line
HISTCONTROL=erasedups
# Don't save some commands to history
# "'HISTIGNORE' subsumes the function of 'HISTCONTROL'.  A pattern of
# '&' is identical to 'ignoredups', and a pattern of '[ ]*' is
# identical to 'ignorespace'." -- info (bash)Bash Variables
HISTIGNORE='&:[ ]*'
# Other commands to ignore
HISTIGNORE="$HISTIGNORE:ls:exit:cd"

# Automatically append to HISTFILE on every command
PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-:}"

## Make a new function from a history item: WIP
# In particular, the arguments of this function could be better imo
histfunc() { # histfunc NAME QUERY
    local name="$1"
    local query="$2"
    local -a cands
    while read -r cmd; do
        cands=( "${cands[@]}" "$cmd" )
    done < <(grep -E "$query" "$HISTFILE" | sort | uniq)
    if (( "${#cands[@]}" == 1 )); then
        funcbody="${cands[0]}"
    else
        select funcbody in "${cands[@]}"; do break; done
    fi
    eval "$name() { $funcbody; }"
}

funcsave() { # funcsave FUNC FILENAME
    (( $# == 2 )) || {
        echo "Wrong number of arguments (need 2)" >&2
        return 1
    }
    cat <<EOF > "$2"
#!/usr/bin/env bash
# saved with 'funcsave'

$(type "$1" | sed 1d)

if [[ "\${BASH_SOURCE[0]}" == "\$0" ]]; then
   [[ "\$DEBUG" ]] && set -x
   "$1" "\$@"
fi
EOF
    chmod +x "$2"
}