]> git.immae.eu Git - perso/Immae/Config/Ansible.git/blob - roles/shell/bash/templates/bashrc.j2
Add bash history per tmux session
[perso/Immae/Config/Ansible.git] / roles / shell / bash / templates / bashrc.j2
1 #
2 # ~/.bashrc
3 #
4
5 [[ -f "$XDG_STATE_HOME/bash/bashrc_temp" ]] && . $XDG_STATE_HOME/bash/bashrc_temp
6
7 if [ -n "$HOME/.local/bin" ]; then
8 PATH="$HOME/.local/bin:$PATH"
9 fi
10
11 # If not running interactively, don't do anything
12 [[ $- != *i* ]] && return
13
14 export GPG_TTY=$(tty)
15 export HISTFILE="$XDG_STATE_HOME/bash/history"
16
17 alias vi=vim
18 alias ls='ls --color=auto -F -T 0'
19 alias tmux='tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf'
20
21 alias grep='grep --color=auto'
22 alias fgrep='fgrep --color=auto'
23 alias egrep='egrep --color=auto'
24
25 alias sudo="sudo "
26 alias mv="mv -i"
27 alias cp="cp -i"
28 alias rm="rm -i"
29 alias psp="pstree -aUp $USER"
30 alias dotdrop="dotdrop --cfg $XDG_CONFIG_HOME/dotdrop/config.yaml"
31
32 [[ -f /usr/share/stgit/completion/stgit-completion.bash ]] && . /usr/share/stgit/completion/stgit-completion.bash
33
34 {% if role.get(profile) == "fretlink" %}
35 ##### Some Fretlink aliases
36 alias getDump='s3cmd -c <(pass show Travail/Fretlink/S3) get '
37
38 function vpn() {
39 TMPFILE=$(mktemp /tmp/openvpn.XXXXXX)
40 pass show Travail/Fretlink/OpenVPN >> $TMPFILE
41 sudo openvpn $TMPFILE
42 rm -f $TMPFILE
43 }
44
45 function dev_prompt() {
46 if [ -n "$FRETLINK_ENV" ]; then
47 echo "{$FRETLINK_ENV} "
48 fi
49 }
50
51 function tmux_dev() {
52 type="${1:-bash}"
53 if [ -n "$TMUX" ]; then
54 m=switch-client
55 else
56 m=attach-session
57 fi
58
59 case "$type" in
60 "bash") start_dir="$HOME";;
61 "puppet") start_dir="/etc/puppetlabs/code"
62 esac
63
64 if tmux has-session -t "$type" 2>/dev/null; then
65 tmux $m -t "$type"
66 else
67 if [ -n "$start_dir" ]; then
68 tmux new-session -d -c $start_dir -s "$type" \; \
69 $m -t "$type" \; \
70 new-window -c $start_dir
71 else
72 tmux new-session -d -c "$HOME/workdir/$type" -s "$type" \; \
73 $m -t "$type" \; \
74 send-keys "../start" C-m \; \
75 rename-window "running" \; \
76 new-window -c "$HOME/workdir/$type"
77 fi
78 fi
79 }
80
81 _tmux_dev() {
82 COMPREPLY=()
83 local cur="${COMP_WORDS[COMP_CWORD]}"
84 prefix="$HOME/workdir/"
85 local IFS=$'\n'
86 local items=($(IFS=' ' compgen -W "bash puppet" -- $cur) $(compgen -d $prefix$cur))
87 for item in ${items[@]}; do
88 [[ $item == $prefix.* ]] && continue
89 COMPREPLY+=("${item#$prefix}")
90 done
91 }
92 complete -o filenames -F _tmux_dev tmux_dev
93
94 if [ -n "$TMUX" ]; then
95 TMUX_SESSION_NAME=$(tmux display-message -p "#S")
96 export HISTFILE="$XDG_STATE_HOME/bash/tmux_${TMUX_SESSION_NAME}_history"
97 fi
98 ##### /Fretlink
99 {% endif %}
100
101 # Non-printable chars need to be wrapped by \[ ... \] otherwise bash
102 # will wrongly count the size of the prompt and glitch when going
103 # through history
104 function git_prompt_color() {
105 git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
106 gitstatus=$?
107 if [ "$gitstatus" -gt 0 ]; then
108 av=$'\033[01;31m'
109 else
110 av=$'\033[01;34m'
111 fi
112 echo "$av"
113 }
114
115 function git_prompt() {
116 gitinfo=$(git branch 2>/dev/null | grep "^*" | cut -c3-);
117 git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null
118 gitstatus=$?
119 gitlsfiles=$(git ls-files . 2>/dev/null | wc -l);
120 if [ "$gitinfo" -a "$gitlsfiles" -gt 0 ]; then
121 echo -e "($gitinfo) "
122 fi
123 }
124
125 function error_code() {
126 ret=$?
127 if [[ $ret != 0 ]]; then
128 echo "$ret "
129 fi
130 }
131
132 function running_jobs() {
133 ret=$(jobs | wc -l)
134 if [[ $ret != 0 ]]; then
135 echo "[$ret] "
136 fi
137 }
138
139 {% if role.get(profile) == "fretlink" %}
140 PS1='\[\033[01;31m\]$(error_code)\[\033[00;00m\]$(running_jobs)\[\033[01;32m\]$(dev_prompt)\[$(git_prompt_color)\]$(git_prompt)\[\033[00;32m\]\u@\h\[\033[94m\] \w \$\[\033[00m\] '
141 {% else %}
142 PS1='\[\033[01;31m\]$(error_code)\[\033[00;00m\]$(running_jobs)\[$(git_prompt_color)\]$(git_prompt)\[\033[00;32m\]\u@\h\[\033[94m\] \w \$\[\033[00m\] '
143 {% endif %}
144
145 PROMPT_COMMAND="[[ -f "$XDG_STATE_HOME/bash/bashrc_temp" ]] && . $XDG_STATE_HOME/bash/bashrc_temp"
146
147 {% if "nodejs" in features.get(profile, []) %}
148 source /usr/share/nvm/init-nvm.sh
149 {% endif %}