-
Notifications
You must be signed in to change notification settings - Fork 0
/
phist.sh
executable file
·57 lines (47 loc) · 1.94 KB
/
phist.sh
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
# Make command history persistent.
# This file should be executed by sourcing it, e.g.:
# source phist.sh
# Code from:
# http://eli.thegreenplace.net/2013/06/11/keeping-persistent-history-in-bash
bash_persistent_history()
{
# Extract date and the actual command from the latest entry
# in the Bash history.
# The date is supposed to consist of two fields (date and time)
# separated by one space character.
[[
$(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
# Ensure the command is different from the command last written.
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
then
# Write the date and the command to the persistent history
# file and remember which command was written.
echo "$date_part" "|" "$command_part" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
# Run the function log_bash_persistent_history when the Bash prompt
# is shown.
PROMPT_COMMAND="bash_persistent_history"
# Make each Bash session remember a decent number of commands.
HISTSIZE=500
# Configure Bash to not store command history for each session.
HISTFILESIZE=0
# Don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options.
HISTCONTROL=ignorespace:ignoredups
# Format dates using the ISO 8601 standard.
# Note that the regex expression in the function bash_persistent_history()
# will need to be updated if the date format is changed to anything that
# does not contain two fields separated by one space character.
HISTTIMEFORMAT="%Y-%m-%d %T "
# Setup aliases
# Search for a command in the persistent history file.
alias phgrep='cat ~/.persistent_history|grep'
# Search the history belonging to the current session.
alias hgrep='history|grep'
# See the latest executed commands stored in the persistent history file.
alias phist='tail -n 500 ~/.persistent_history'