]> git.phdru.name Git - dotfiles.git/blob - .bashrc
.bashrc: Extract git-related functions to a separate file
[dotfiles.git] / .bashrc
1 #
2 # $HOME/.bashrc
3 #
4
5 # ~/.bashrc: executed by bash(1) for non-login shells.
6 # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
7 # for examples
8
9 # Copied from .profile to do minor initialization for non-interactive non-login-shells
10 umask 077
11
12 PATH="$HOME"/bin:"$HOME"/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
13 if [ -n "$DISPLAY" ]; then
14     PATH=$PATH:/usr/games
15 fi
16
17 # Make `ssh host date` to display in 24-hour format
18 LANG=C
19
20 # If not running interactively, don't do anything
21 case $- in
22     *i*) ;;
23       *) return ;;
24 esac
25
26 # Stop if non-interactive shell
27 [ -z "$PS1" ] && return
28
29 . "$HOME"/.shellrc
30
31 if [ "$HISTFILE" = "$HOME/.bash_history" ]; then
32     history -r
33     # This is the default value set by interactive bash
34     # when the global value was unset. Unset it here too.
35     unset HISTFILE
36 fi
37
38 # check the window size after each command and, if necessary,
39 # update the values of LINES and COLUMNS.
40 shopt -s checkwinsize
41
42 # If set, the pattern "**" used in a pathname expansion context will
43 # match all files and zero or more directories and subdirectories.
44 #shopt -s globstar
45
46 # append to the history file, don't overwrite it
47 shopt -s histappend
48
49 [ "`type -t ls`" = alias ] && unalias ls
50 [ "`type -t ll`" = alias ] && unalias ll
51
52 back() { cd - "$@"; }
53 functions() { typeset -f "$@"; }
54 j() { jobs; }
55
56 if [ -r "$HOME"/admin/prog/bash_prompt ]; then
57     . "$HOME"/admin/prog/bash_prompt
58     set_prompt
59     unset set_prompt
60 else
61     PS1="${debian_chroot:+($debian_chroot)}\u@\h:\W \$SHLVL\\$ "
62 fi
63
64 if [ -z "$BASH_COMPLETION_COMPAT_DIR" ]; then
65     if [ -d /usr/share/bash-completion -a -r /usr/share/bash-completion/bash_completion ]; then
66         _BASH_COMPLETIONS_DIR=/usr/share/bash-completion/completions
67         . /usr/share/bash-completion/bash_completion
68     elif [ -r /etc/bash_completion ]; then
69         _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
70         . /etc/bash_completion
71     elif [ -d /etc/bash_completion.d ] &&
72             [ -n "`ls -A /etc/bash_completion.d`" ]; then
73         _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
74         for _compf in /etc/bash_completion.d/*; do
75             . $_compf
76         done
77     elif [ -d /usr/local/etc/bash_completion.d ] &&
78             [ -n "`ls -A /usr/local/etc/bash_completion.d`" ]; then
79         _BASH_COMPLETIONS_DIR=/usr/local/etc/bash_completion.d
80         for _compf in /usr/local/etc/bash_completion.d/*; do
81             . $_compf
82         done
83     else
84         echo "Unknown OS type, cannot source bash_completion" >&2
85     fi
86 fi
87
88 has_completion() {
89     [ -n "$_BASH_COMPLETIONS_DIR" -a -d "$_BASH_COMPLETIONS_DIR" -a -r "$_BASH_COMPLETIONS_DIR/$1" ] ||
90     complete -p $1 >/dev/null 2>&1
91 }
92
93 for cmd in builtin cgmem_nice command dbus-launch exec \
94         killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg \
95         run-hosts su sudo time whence whereis which xargs; do
96     has_completion $cmd || complete -o default -A command $cmd
97 done
98
99 for cmd in dig host mtr nslookup nc netcat nmap p ping ping6 \
100         ssh2 s2 socat telnet t tt \
101         tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
102         whois wd wget wget-download wget-m wget-wrapper ww; do
103     has_completion $cmd || complete -A hostname $cmd
104 done
105
106 for cmd in distribute ftp lftp r rsync \
107         s scp ssh smbclient tcpdump tshark wireshark; do
108     has_completion $cmd || complete -o default -A hostname $cmd
109 done
110
111 for cmd in bg fg jobs; do
112     has_completion $cmd || complete -A job $cmd
113 done
114
115 complete -A job j wait
116
117 for cmd in unset; do
118     has_completion $cmd || complete -A function -A variable $cmd
119 done
120
121 delegate_completion() {
122     local prog programs
123     prog=$1
124     has_completion $prog || return 0
125     shift
126     programs="$@"
127     eval "_${prog}_completion_loader() {
128         _completion_loader $prog
129         if [ x"$DELEGATE_NOSPACE" = x1 ]; then
130             complete -o nospace -F _$prog $programs
131         else
132             complete -F _$prog $programs
133         fi
134         unset _${prog}_completion_loader
135         return 124
136     }"
137     complete -F _${prog}_completion_loader $programs
138 }
139
140 delegate_completion make m
141 delegate_completion ping p
142 DELEGATE_NOSPACE=1 delegate_completion rsync r rsync_cgmn rsync_cgmn_recode rsync_cgmn_recode2
143 delegate_completion ssh s
144 delegate_completion wget wget-m wget-wrapper ww
145
146 if [ -d "$HOME"/.bash_completion.d -a \
147         -n "`ls -A $HOME/.bash_completion.d`" ]; then
148     for _compf in "$HOME"/.bash_completion.d/*; do
149         . $_compf
150     done
151 fi
152
153 unset _BASH_COMPLETIONS_DIR has_completion delegate_completion
154
155 if [ -d "$HOME/lib/config" ]; then
156     complete -W "`cd \"$HOME/lib/config\" && echo *`" include
157 fi
158 complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
159
160 if [ "`type -t short_curdir`" != function -a \
161         -r "$HOME"/admin/prog/bash_prompt ]; then
162     . "$HOME"/admin/prog/bash_prompt
163 fi
164
165 if test -d "$HOME"/.pyenv
166 then
167     PYENV_ROOT="$HOME"/.pyenv
168     export PYENV_ROOT
169     PATH="$PYENV_ROOT/bin:$PATH"
170     if which pyenv >/dev/null 2>&1; then
171 #        eval "`pyenv init --path`"
172         eval "`pyenv init -`"
173         eval "`pyenv virtualenv-init -`"
174     fi
175 fi
176
177
178 . virtualenvwrapper_lazy.sh 2>/dev/null
179
180 if [ -n "$VIRTUAL_ENV" ] && ! type deactivate >/dev/null 2>&1; then
181     . "$VIRTUAL_ENV/bin/activate"
182 fi
183
184 # Clean up python virtual environment on exit
185 # See https://virtualenvwrapper.readthedocs.io/en/latest/tips.html#clean-up-environments-on-exit
186
187 trap '[ "$VIRTUAL_ENV" ] && deactivate' EXIT
188
189 if [ "$SHLVL" -eq 1 -a "`type -t X`" = function ] && which startx >/dev/null 2>&1; then
190     # From https://stackoverflow.com/a/18839557
191
192     copy_function() {
193         test -n "$(declare -f "$1")" || return
194         eval "${_/$1/$2}"
195     }
196
197     rename_function() {
198         copy_function "$@" || return
199         unset -f "$1"
200     }
201
202     rename_function X _non_bash_X
203
204     X() { history -a; _non_bash_X; history -r; }
205 fi
206
207 if test -x /usr/bin/screen >/dev/null 2>&1; then
208     screen_newwin() {
209         history -a
210         screen "$@"
211     }
212 fi