-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot-bashrc-dot-tmux
126 lines (106 loc) · 3.51 KB
/
dot-bashrc-dot-tmux
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- shell-script -*-
#
# .bashrc.tmux - tmux-related functions and aliases for bash/zsh
#
# This is part of: https://github.com/edsantiago/tmux-session
#
#
# On LOGIN: enable a separate history file for this session
#
_tmux-init-history() {
# Only interested in interactive shells running under tmux
test -z "$PS1" && return
test -z "$TMUX" && return
# Get our current tmux session id (eg devel.bz12345).
# If empty, or if unnamed session (0, 1, 2, etc), do not save history.
tmux_session=$(tmux-session mywindowid)
test -z "$tmux_session" && return
case "$tmux_session" in
[0-9]*) return ;;
esac
histdir=$HOME/.bash_history.d
if [ -n "$ZSH_NAME" ]; then histdir=$HOME/.zsh_history.d; fi
if [ ! -d $histdir ]; then
mkdir $histdir
chmod 0700 $histdir
fi
histfile=$histdir/$tmux_session
# Create the file on demand
if [ ! -f $histfile ]; then
touch $histfile
fi
chmod 0600 $histfile
HISTFILE=$histfile
HISTTIMEFORMAT='%F %T '
HIST_DIRSTACK=$histfile.dirs
# FIXME: chmod 600 $HIST_DIRSTACK and $HIST_DIRSTACK-
# On first pass through, restore dirs
dirstack_reset=
if [ -f $HIST_DIRSTACK ]; then
while read d;do
if [ -z "$dirstack_reset" ]; then
builtin cd "$d"
dirstack_reset=reset
else
builtin pushd "$d" >/dev/null
fi
done < <(tac $HIST_DIRSTACK)
fi
# Restore $OLDPWD, to allow cd -
if [ -f $HIST_DIRSTACK- ]; then
OLDPWD=$(< $HIST_DIRSTACK-)
fi
# Save history after every command. This saves our bacon if we crash.
if [ -n "$PROMPT_COMMAND" ]; then
PROMPT_COMMAND="$PROMPT_COMMAND;history -w"
else
PROMPT_COMMAND="history -w"
fi
}
#
# On LOGOUT: make history file read-only. This signals that this session
# was deliberately ended, not closed due to a crash.
#
_tmux-logout() {
test -z "$tmux_session" && return
# FIXME FIXME FIXME: this was intended to detect if the session had
# been renamed. I don't do that in practice (Feb 2014, after many
# years of using tmux) so I don't see any point in implementing/testing).
#new_tmux_session=$(tmux-session mywindowid)
#test -z "$new_tmux_session" && return
# Unchanged.
#test "$new_tmux_session" = "$tmux_session" && return
if [ "$HISTFILE" = "$histdir/$tmux_session" ]; then
chmod 0400 "$HISTFILE"
fi
}
#
# Directory-changing aliases. Preserve current directory stack.
#
_tmux-save-dir-stack() {
# Save both dirlist and $OLDPWD, so we can 'cd -' upon restore
if [ -n "$HIST_DIRSTACK" ]; then
builtin dirs -p -l >| $HIST_DIRSTACK
echo $OLDPWD >| $HIST_DIRSTACK-
fi
}
P() { builtin pushd "$@"; _tmux-save-dir-stack; }
p() { builtin popd "$@"; _tmux-save-dir-stack; }
cd() { builtin cd "$@"; _tmux-save-dir-stack; }
#
# For creating a new session.
#
tt() { tmux-session new "$@"; }
# tmux-restore is useful when you <prefix>-c to create a new window,
# and immediately want to recover/restore from another session.
# It would be better to use tt, but sometimes we forget.
tmux-restore() {
tmux rename-window "$@" && _tmux-init-history && history -r && _tmux-save-dir-stack
}
# tmux-rename is useful when you want to rename this session but keep
# your work. (tmux-restore clobbers your history and directories).
tmux-rename() {
tmux rename-window "$@"
tmux_session=$(tmux-session mywindowid)
HISTFILE=$histdir/$tmux_session
}