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