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