Skip to content

Commit 4044f41

Browse files
committed
add top-history
1 parent 61f727f commit 4044f41

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

plugins/available/history-search.plugin.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'search history using the prefix already entered'
34

plugins/available/history-substring-search.plugin.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'search history using the substring already entered'
34

plugins/available/history.plugin.bash

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'improve history handling with sane defaults'
34

@@ -9,3 +10,30 @@ export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups}
910

1011
# resize history to 100x the default (500)
1112
export HISTSIZE=${HISTSIZE:-50000}
13+
14+
top-history() {
15+
about 'print the name and count of the most commonly run tools'
16+
17+
if [[ -n $HISTTIMEFORMAT ]]; then
18+
# To parse history we need a predictable format, which HISTTIMEFORMAT
19+
# gets in the way of. So we unset it and set a trap to guarantee the
20+
# user's environment returns to normal even if the pipeline below fails.
21+
# shellcheck disable=SC2064
22+
trap "export HISTTIMEFORMAT='$HISTTIMEFORMAT'" RETURN
23+
unset HISTTIMEFORMAT
24+
fi
25+
26+
history \
27+
| awk '{
28+
a[$2]++
29+
}END{
30+
for(i in a)
31+
printf("%s\t%s\n", a[i], i)
32+
}' \
33+
| sort --reverse --numeric-sort \
34+
| head \
35+
| column \
36+
--table \
37+
--table-columns 'Command Count,Command Name' \
38+
--output-separator ' | '
39+
}

0 commit comments

Comments
 (0)