]> git.phdru.name Git - dotfiles.git/blob - .bashrc
14eeaec3b31bc792485a9d2d22d7137ce7c70704
[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
14 # Make `ssh host date` to display in 24-hour format
15 LANG=C
16
17 # If not running interactively, don't do anything
18 case $- in
19     *i*) ;;
20       *) return ;;
21 esac
22
23 # Stop if non-interactive shell
24 [ -z "$PS1" ] && return
25
26 . "$HOME"/.shellrc
27
28 if [ "$HISTFILE" = "$HOME/.bash_history" ]; then
29     # This is the default value set by interactive bash
30     # when the global value was unset. Unset it here too.
31     unset HISTFILE
32 fi
33
34 # append to the history file, don't overwrite it
35 shopt -s histappend
36
37 # check the window size after each command and, if necessary,
38 # update the values of LINES and COLUMNS.
39 shopt -s checkwinsize
40
41 # If set, the pattern "**" used in a pathname expansion context will
42 # match all files and zero or more directories and subdirectories.
43 #shopt -s globstar
44
45 [ "`type -t ls`" = alias ] && unalias ls
46 [ "`type -t ll`" = alias ] && unalias ll
47
48 back() { cd - "$@"; }
49 functions() { typeset -f "$@"; }
50 j() { jobs; }
51
52 if [ -z "$BASH_COMPLETION_COMPAT_DIR" ]; then
53     if [ -d /usr/share/bash-completion -a -r /usr/share/bash-completion/bash_completion ]; then
54         _BASH_COMPLETIONS_DIR=/usr/share/bash-completion/completions
55         . /usr/share/bash-completion/bash_completion
56     elif [ -r /etc/bash_completion ]; then
57         _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
58         . /etc/bash_completion
59     elif [ -d /etc/bash_completion.d ]; then
60         _BASH_COMPLETIONS_DIR=/etc/bash_completion.d
61         for _compf in /etc/bash_completion.d/*; do
62             . $_compf
63         done
64     elif [ -d /usr/local/etc/bash_completion.d ]; then
65         _BASH_COMPLETIONS_DIR=/usr/local/etc/bash_completion.d
66         for _compf in /usr/local/etc/bash_completion.d/*; do
67             . $_compf
68         done
69     else
70         echo "Unknown OS type, cannot source bash_completion" >&2
71     fi
72 fi
73
74 has_completion() {
75     [ -n "$_BASH_COMPLETIONS_DIR" -a -d "$_BASH_COMPLETIONS_DIR" -a -r "$_BASH_COMPLETIONS_DIR/$1" ] ||
76     complete -p $1 >/dev/null 2>&1
77 }
78
79 for cmd in builtin cgmem_nice command dbus-launch exec \
80         killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg \
81         run-hosts su sudo time whence whereis which xargs; do
82     ! has_completion $cmd && complete -o default -A command $cmd
83 done
84
85 for cmd in dig host mtr nslookup nc netcat nmap p ping ping6 \
86         ssh2 s2 socat telnet t tt \
87         tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
88         whois wd wget wget-download wget-m wget-wrapper ww; do
89     ! has_completion $cmd && complete -A hostname $cmd
90 done
91
92 for cmd in distribute ftp lftp r rsync \
93         s scp ssh smbclient tcpdump tshark wireshark; do
94     ! has_completion $cmd && complete -o default -A hostname $cmd
95 done
96
97 for cmd in bg fg jobs; do
98     ! has_completion $cmd && complete -A job $cmd
99 done
100
101 complete -A job j wait
102
103 for cmd in unset; do
104     ! has_completion $cmd && complete -A function -A variable $cmd
105 done
106
107 if [ -d "$HOME"/.bash_completion.d ]; then
108     for _compf in "$HOME"/.bash_completion.d/*; do
109         . $_compf
110     done
111 fi
112
113 delegate_completion() {
114     local prog programs
115     prog=$1
116     ! has_completion $prog && return
117     shift
118     programs="$@"
119     eval "_${prog}_completion_loader() {
120         _completion_loader $prog
121         if [ x"$DELEGARE_NOSPACE" = x1 ]; then
122             complete -o nospace -F _$prog $programs
123         else
124             complete -F _$prog $programs
125         fi
126         unset _${prog}_completion_loader
127         return 124
128     }"
129     complete -F _${prog}_completion_loader $programs
130 }
131
132 delegate_completion make m
133 delegate_completion ping p
134 delegate_completion python python2.7 python3.4 python3.5 python3.6 python3.7 python3.8 python3.9 python3.10
135 DELEGARE_NOSPACE=1 delegate_completion rsync r rsync_cgmn rsync_cgmn_recode rsync_cgmn_recode2
136 delegate_completion ssh s
137 delegate_completion wget wget-m wget-wrapper ww
138
139 unset _BASH_COMPLETIONS_DIR has_completion delegate_completion
140
141 if [ -d "$HOME/lib/config" ]; then
142     complete -W "`cd \"$HOME/lib/config\" && echo *`" include
143 fi
144 complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
145
146 if [ "`type -t short_curdir`" != function -a \
147         -r "$HOME"/admin/prog/bash_prompt ]; then
148     . "$HOME"/admin/prog/bash_prompt
149 fi
150
151 if type -p pip >/dev/null 2>&1 || type -p pip3 >/dev/null 2>&1; then
152     _pip_completion_loader() {
153         eval "`pip$PY_VER completion --bash`"
154         complete -F _pip_completion pip pip2 pip2.7 pip3 pip3.4 pip3.5 pip3.6 pip3.7 pip3.8 pip3.9 pip3.10
155         unset _pip_completion_loader
156         return 124
157     }
158     complete -F _pip_completion_loader pip pip2 pip2.7 pip3 pip3.4 pip3.5 pip3.6 pip3.7 pip3.8 pip3.9 pip3.10
159 fi
160
161 #if which pyenv >/dev/null 2>&1; then
162 #   eval "`pyenv init -`"
163 #   eval "`pyenv virtualenv-init -`"
164 #fi
165
166 . virtualenvwrapper_lazy.sh 2>/dev/null
167
168 if [ -n "$VIRTUAL_ENV" ] && ! type deactivate >/dev/null 2>&1; then
169     . "$VIRTUAL_ENV/bin/activate"
170 fi
171
172 # Clean up python virtual environment on exit
173 # See https://virtualenvwrapper.readthedocs.io/en/latest/tips.html#clean-up-environments-on-exit
174
175 trap '[ "$VIRTUAL_ENV" ] && deactivate' EXIT
176
177
178 if test -x /usr/bin/git >/dev/null 2>&1; then
179     # chdir to a directory
180     cdgitpath() {
181         if [ $# -ne 1 ]; then
182             echo "Usage: cdgitpath path_key" >&2
183             return 1
184         fi
185
186         path_key="$1"
187         if [ -z "$path_key" ]; then
188             echo "Usage: cdgitpath path_key" >&2
189             return 1
190         fi
191
192         path_url="`git config --get --path $path_key`"
193         if echo "$path_url" | grep -q '^file:/'; then
194             path_url="`echo \"$path_url\" | sed 's@^file:/\+@/@'`"
195         fi
196         cd "$path_url"
197     }
198
199     # chdir to a remote's directory (if the remote is on the local FS)
200     cdremote() {
201         if [ $# -gt 1 ]; then
202             echo "Usage: cdremote [remote_name]" >&2
203             return 1
204         fi
205
206         if [ -z "$1" ]; then
207             branch="`git rev-parse --abbrev-ref HEAD`"
208             remote="`git config --get branch.$branch.remote`"
209         else
210             remote="$1"
211         fi
212
213         if [ -n "$remote" ] && git config --get remote.$remote.url |
214                 grep -q '^\(file:/\|/\|\.\./\)'; then # (file:/ or / or ../ at the beginning)
215             cdgitpath remote.$remote.url
216             return
217         fi
218
219         if [ -n "$1" ]; then
220             echo "Cannot find directory for remote $1" >&2
221             echo "Usage: cdremote [remote_name]" >&2
222             return 1
223         fi
224
225         _list_remotes '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
226         if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
227             remote=${GIT_REMOTES[0]}
228             unset GIT_REMOTES
229             cdgitpath remote.$remote.url
230         else
231             unset GIT_REMOTES
232             echo "Cannot find directory for any remote" >&2
233             echo "Usage: cdremote [remote_name]" >&2
234             return 1
235         fi
236     }
237
238     # completion for aliases in global .gitconfig
239
240     # fixup rbi rbia rbiap rbip - do refs name completion
241     _git_fixup() { __gitcomp_nl "$(__git_refs)" ; }
242     _git_rbi() { __gitcomp_nl "$(__git_refs)" ; }
243     _git_rbia() { __gitcomp_nl "$(__git_refs)" ; }
244     _git_rbiap() { __gitcomp_nl "$(__git_refs)" ; }
245     _git_rbip() { __gitcomp_nl "$(__git_refs)" ; }
246     #
247     # push-to-all-remotes - do branch name completion
248     _git_push_to_all_remotes() { __gitcomp_nl "$(__git_heads)" ; }
249
250     _cdgitpath_complete() {
251         local cur="${COMP_WORDS[COMP_CWORD]}"
252         COMPREPLY=(`compgen -W "$(__git_config_get_set_variables)" -- "$cur"`)
253     }
254
255     _git_open() {
256         _cdgitpath_complete
257     }
258
259     complete -F _cdgitpath_complete cdgitpath git-open
260
261     # list remotes with URLs matching a regexp
262     _list_remotes() {
263         if [ $# -ne 1 ]; then
264             echo "Usage: _list_remotes remote_regexp" >&2
265             return 1
266         fi
267         declare -ag GIT_REMOTES=()
268         local remote
269         for remote in `git remote`; do
270             if git config --get remote.$remote.url | grep -q "$1"; then
271                 GIT_REMOTES+=($remote)
272             fi
273         done
274     }
275
276     # completion for cdremote and git-open-remote - list remotes with a pattern
277     _list_remotes_completion() {
278         local cur="${COMP_WORDS[COMP_CWORD]}"
279         _list_remotes "$1"
280         COMPREPLY=(`compgen -W "${GIT_REMOTES[*]}" -- "$cur"`)
281         unset GIT_REMOTES
282     }
283
284     # completion for cdremote - list remotes with directories as URLs
285     _cdremote_complete() {
286         _list_remotes_completion '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
287         if [ ${#COMPREPLY[*]} -eq 0 ]; then
288             _list_remotes_completion .
289         fi
290     }
291
292     complete -F _cdremote_complete cdremote
293
294     # completion for git-open-remote - list remotes with http(s) URLs
295     _git_open_remote() {
296         _list_remotes_completion '^http\(s\)\?://'
297         if [ ${#COMPREPLY[*]} -eq 0 ]; then
298             _list_remotes_completion .
299         fi
300     }
301
302     complete -F _git_open_remote git-open-remote
303 fi
304
305
306 if [ "$SHLVL" -eq 1 -a "`type -t X`" = function ] && which startx >/dev/null 2>&1; then
307     # From https://stackoverflow.com/a/18839557
308
309     copy_function() {
310         test -n "$(declare -f "$1")" || return
311         eval "${_/$1/$2}"
312     }
313
314     rename_function() {
315         copy_function "$@" || return
316         unset -f "$1"
317     }
318
319     rename_function X _non_bash_X
320
321     X() { history -a; _non_bash_X; history -r; }
322 fi