]> git.phdru.name Git - dotfiles.git/blob - .bashrc
.shellrc: Move bash-only code to `.bashrc`
[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 ]; then
72         _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
73         for _compf in /etc/bash_completion.d/*; do
74             . $_compf
75         done
76     elif [ -d /usr/local/etc/bash_completion.d ]; then
77         _BASH_COMPLETIONS_DIR=/usr/local/etc/bash_completion.d
78         for _compf in /usr/local/etc/bash_completion.d/*; do
79             . $_compf
80         done
81     else
82         echo "Unknown OS type, cannot source bash_completion" >&2
83     fi
84 fi
85
86 has_completion() {
87     [ -n "$_BASH_COMPLETIONS_DIR" -a -d "$_BASH_COMPLETIONS_DIR" -a -r "$_BASH_COMPLETIONS_DIR/$1" ] ||
88     complete -p $1 >/dev/null 2>&1
89 }
90
91 for cmd in builtin cgmem_nice command dbus-launch exec \
92         killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg \
93         run-hosts su sudo time whence whereis which xargs; do
94     ! has_completion $cmd && complete -o default -A command $cmd
95 done
96
97 for cmd in dig host mtr nslookup nc netcat nmap p ping ping6 \
98         ssh2 s2 socat telnet t tt \
99         tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
100         whois wd wget wget-download wget-m wget-wrapper ww; do
101     ! has_completion $cmd && complete -A hostname $cmd
102 done
103
104 for cmd in distribute ftp lftp r rsync \
105         s scp ssh smbclient tcpdump tshark wireshark; do
106     ! has_completion $cmd && complete -o default -A hostname $cmd
107 done
108
109 for cmd in bg fg jobs; do
110     ! has_completion $cmd && complete -A job $cmd
111 done
112
113 complete -A job j wait
114
115 for cmd in unset; do
116     ! has_completion $cmd && complete -A function -A variable $cmd
117 done
118
119 if [ -d "$HOME"/.bash_completion.d ]; then
120     for _compf in "$HOME"/.bash_completion.d/*; do
121         . $_compf
122     done
123 fi
124
125 delegate_completion() {
126     local prog programs
127     prog=$1
128     ! has_completion $prog && return
129     shift
130     programs="$@"
131     eval "_${prog}_completion_loader() {
132         _completion_loader $prog
133         if [ x"$DELEGATE_NOSPACE" = x1 ]; then
134             complete -o nospace -F _$prog $programs
135         else
136             complete -F _$prog $programs
137         fi
138         unset _${prog}_completion_loader
139         return 124
140     }"
141     complete -F _${prog}_completion_loader $programs
142 }
143
144 delegate_completion make m
145 delegate_completion ping p
146 delegate_completion python python2.7 python3.4 python3.5 python3.6 python3.7 python3.8 python3.9 python3.10 python3.11
147 DELEGATE_NOSPACE=1 delegate_completion rsync r rsync_cgmn rsync_cgmn_recode rsync_cgmn_recode2
148 delegate_completion ssh s
149 delegate_completion wget wget-m wget-wrapper ww
150
151 unset _BASH_COMPLETIONS_DIR has_completion delegate_completion
152
153 if [ -d "$HOME/lib/config" ]; then
154     complete -W "`cd \"$HOME/lib/config\" && echo *`" include
155 fi
156 complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
157
158 if [ "`type -t short_curdir`" != function -a \
159         -r "$HOME"/admin/prog/bash_prompt ]; then
160     . "$HOME"/admin/prog/bash_prompt
161 fi
162
163 if type -p pip >/dev/null 2>&1 || type -p pip3 >/dev/null 2>&1; then
164     _pip_completion_loader() {
165         eval "`pip completion --bash`"
166         complete -F _pip_completion pip pip2 pip2.7 pip3 pip3.4 pip3.5 pip3.6 pip3.7 pip3.8 pip3.9 pip3.10 pip3.11
167         unset _pip_completion_loader
168         return 124
169     }
170     complete -F _pip_completion_loader pip pip2 pip2.7 pip3 pip3.4 pip3.5 pip3.6 pip3.7 pip3.8 pip3.9 pip3.10 pip3.11
171 fi
172
173 #if which pyenv >/dev/null 2>&1; then
174 #   eval "`pyenv init -`"
175 #   eval "`pyenv virtualenv-init -`"
176 #fi
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
190 if test -x /usr/bin/git >/dev/null 2>&1; then
191     # chdir to a directory
192     cdgitpath() {
193         if [ $# -ne 1 ]; then
194             echo "Usage: cdgitpath path_key" >&2
195             return 1
196         fi
197
198         path_key="$1"
199         if [ -z "$path_key" ]; then
200             echo "Usage: cdgitpath path_key" >&2
201             return 1
202         fi
203
204         path_url="`git config --get --path $path_key`"
205         if echo "$path_url" | grep -q '^file:/'; then
206             path_url="`echo \"$path_url\" | sed 's@^file:/\+@/@'`"
207         fi
208         cd "$path_url"
209     }
210
211     # chdir to a remote's directory (if the remote is on the local FS)
212     cdremote() {
213         if [ $# -gt 1 ]; then
214             echo "Usage: cdremote [remote_name]" >&2
215             return 1
216         fi
217
218         if [ -z "$1" ]; then
219             branch="`git rev-parse --abbrev-ref HEAD`"
220             remote="`git config --get branch.$branch.remote`"
221         else
222             remote="$1"
223         fi
224
225         if [ -n "$remote" ] && git config --get remote.$remote.url |
226                 grep -q '^\(file:/\|/\|\.\./\)'; then # (file:/ or / or ../ at the beginning)
227             cdgitpath remote.$remote.url
228             return
229         fi
230
231         if [ -n "$1" ]; then
232             echo "Cannot find directory for remote $1" >&2
233             echo "Usage: cdremote [remote_name]" >&2
234             return 1
235         fi
236
237         _list_remotes '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
238         if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
239             remote=${GIT_REMOTES[0]}
240             unset GIT_REMOTES
241             cdgitpath remote.$remote.url
242         else
243             unset GIT_REMOTES
244             echo "Cannot find directory for any remote" >&2
245             echo "Usage: cdremote [remote_name]" >&2
246             return 1
247         fi
248     }
249
250     # completion for aliases in global .gitconfig
251
252     # fixup rbi rbia rbiap rbip - do refs name completion
253     _git_fixup() { __gitcomp_nl "$(__git_refs)" ; }
254     _git_rbi() { __gitcomp_nl "$(__git_refs)" ; }
255     _git_rbia() { __gitcomp_nl "$(__git_refs)" ; }
256     _git_rbiap() { __gitcomp_nl "$(__git_refs)" ; }
257     _git_rbip() { __gitcomp_nl "$(__git_refs)" ; }
258     #
259     # push-to-all-remotes - do branch name completion
260     _git_push_to_all_remotes() { __gitcomp_nl "$(__git_heads)" ; }
261
262     _cdgitpath_complete() {
263         local cur="${COMP_WORDS[COMP_CWORD]}"
264         COMPREPLY=(`compgen -W "$(__git_config_get_set_variables)" -- "$cur"`)
265     }
266
267     _git_open() {
268         _cdgitpath_complete
269     }
270
271     complete -F _cdgitpath_complete cdgitpath git-open
272
273     # list remotes with URLs matching a regexp
274     _list_remotes() {
275         if [ $# -ne 1 ]; then
276             echo "Usage: _list_remotes remote_regexp" >&2
277             return 1
278         fi
279         declare -ag GIT_REMOTES=()
280         local remote
281         for remote in `git remote`; do
282             if git config --get remote.$remote.url | grep -q "$1"; then
283                 GIT_REMOTES+=($remote)
284             fi
285         done
286     }
287
288     # completion for cdremote and git-open-remote - list remotes with a pattern
289     _list_remotes_completion() {
290         local cur="${COMP_WORDS[COMP_CWORD]}"
291         _list_remotes "$1"
292         COMPREPLY=(`compgen -W "${GIT_REMOTES[*]}" -- "$cur"`)
293         unset GIT_REMOTES
294     }
295
296     # completion for cdremote - list remotes with directories as URLs
297     _cdremote_complete() {
298         _list_remotes_completion '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
299         if [ ${#COMPREPLY[*]} -eq 0 ]; then
300             _list_remotes_completion .
301         fi
302     }
303
304     complete -F _cdremote_complete cdremote
305
306     # completion for git-open-remote - list remotes with http(s) URLs
307     _git_open_remote() {
308         _list_remotes_completion '^http\(s\)\?://'
309         if [ ${#COMPREPLY[*]} -eq 0 ]; then
310             _list_remotes_completion .
311         fi
312     }
313
314     complete -F _git_open_remote git-open-remote
315 fi
316
317
318 if [ "$SHLVL" -eq 1 -a "`type -t X`" = function ] && which startx >/dev/null 2>&1; then
319     # From https://stackoverflow.com/a/18839557
320
321     copy_function() {
322         test -n "$(declare -f "$1")" || return
323         eval "${_/$1/$2}"
324     }
325
326     rename_function() {
327         copy_function "$@" || return
328         unset -f "$1"
329     }
330
331     rename_function X _non_bash_X
332
333     X() { history -a; _non_bash_X; history -r; }
334 fi