-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_bash_scripts
69 lines (55 loc) · 1.88 KB
/
dot_bash_scripts
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
#!/bin/bash
# Quick HTTPBIN server for request inspection
httpbin() {
docker run -it --rm --name httpbin -p "${1:-8080}:80" kennethreitz/httpbin
}
# Open any file via xdg-open in the background
function open() {
nohup xdg-open "$*" > /dev/null 2>&1 &
}
# Synchronize history between bash sessions
#
# Make history from other terminals available to the current one. However,
# don't mix all histories together - make sure that *all* commands from the
# current session are on top of its history, so that pressing up arrow will
# give you most recent command from this session, not from any session.
#
# Since history is saved on each prompt, this additionally protects it from
# terminal crashes.
#
# on every prompt, save new history to dedicated file and recreate full history
# by reading all files, always keeping history from current session on top.
update_history () {
history -a ${HISTFILE}.$$
history -c
history -r # load common history file
# load histories of other sessions
for f in `ls ${HISTFILE}.[0-9]* 2>/dev/null | grep -v "${HISTFILE}.$$\$"`; do
history -r $f
done
history -r "${HISTFILE}.$$" # load current session history
}
if [[ "$PROMPT_COMMAND" != *update_history* ]]; then
export PROMPT_COMMAND="update_history; $PROMPT_COMMAND"
fi
# merge session history into main history file on bash exit
merge_session_history () {
if [ -e ${HISTFILE}.$$ ]; then
cat ${HISTFILE}.$$ >> $HISTFILE
rm ${HISTFILE}.$$
fi
}
trap merge_session_history EXIT
# detect leftover files from crashed sessions and merge them back
active_shells=$(pgrep `ps -p $$ -o comm=`)
grep_pattern=`for pid in $active_shells; do echo -n "-e \.${pid}\$ "; done`
orphaned_files=`ls $HISTFILE.[0-9]* 2>/dev/null | grep -v $grep_pattern`
if [ -n "$orphaned_files" ]; then
echo Merging orphaned history files:
for f in $orphaned_files; do
echo " `basename $f`"
cat $f >> $HISTFILE
rm $f
done
echo "done."
fi