]> git.phdru.name Git - dotfiles.git/blob - .bashrc
.bashrc: Check the dir is not empty before looping over files
[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 if [ -d "$HOME"/.bash_completion.d -a \
122         -n "`ls -A $HOME/.bash_completion.d`" ]; then
123     for _compf in "$HOME"/.bash_completion.d/*; do
124         . $_compf
125     done
126 fi
127
128 delegate_completion() {
129     local prog programs
130     prog=$1
131     ! has_completion $prog && return
132     shift
133     programs="$@"
134     eval "_${prog}_completion_loader() {
135         _completion_loader $prog
136         if [ x"$DELEGATE_NOSPACE" = x1 ]; then
137             complete -o nospace -F _$prog $programs
138         else
139             complete -F _$prog $programs
140         fi
141         unset _${prog}_completion_loader
142         return 124
143     }"
144     complete -F _${prog}_completion_loader $programs
145 }
146
147 delegate_completion make m
148 delegate_completion ping p
149 delegate_completion python python2.7 python3.4 python3.5 python3.6 python3.7 python3.8 python3.9 python3.10 python3.11 python3.12
150 DELEGATE_NOSPACE=1 delegate_completion rsync r rsync_cgmn rsync_cgmn_recode rsync_cgmn_recode2
151 delegate_completion ssh s
152 delegate_completion wget wget-m wget-wrapper ww
153
154 unset _BASH_COMPLETIONS_DIR has_completion delegate_completion
155
156 if [ -d "$HOME/lib/config" ]; then
157     complete -W "`cd \"$HOME/lib/config\" && echo *`" include
158 fi
159 complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
160
161 if [ "`type -t short_curdir`" != function -a \
162         -r "$HOME"/admin/prog/bash_prompt ]; then
163     . "$HOME"/admin/prog/bash_prompt
164 fi
165
166 if type -p pip >/dev/null 2>&1 || type -p pip3 >/dev/null 2>&1; then
167     _pip_completion_loader() {
168         eval "`pip completion --bash`"
169         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 pip3.12
170         unset _pip_completion_loader
171         return 124
172     }
173     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 pip3.12
174 fi
175
176 if test -d "$HOME"/.pyenv
177 then
178     PYENV_ROOT="$HOME"/.pyenv
179     export PYENV_ROOT
180     PATH="$PYENV_ROOT/bin:$PATH"
181     if which pyenv >/dev/null 2>&1; then
182 #        eval "`pyenv init --path`"
183         eval "`pyenv init -`"
184         eval "`pyenv virtualenv-init -`"
185     fi
186 fi
187
188
189 . virtualenvwrapper_lazy.sh 2>/dev/null
190
191 if [ -n "$VIRTUAL_ENV" ] && ! type deactivate >/dev/null 2>&1; then
192     . "$VIRTUAL_ENV/bin/activate"
193 fi
194
195 # Clean up python virtual environment on exit
196 # See https://virtualenvwrapper.readthedocs.io/en/latest/tips.html#clean-up-environments-on-exit
197
198 trap '[ "$VIRTUAL_ENV" ] && deactivate' EXIT
199
200
201 if test -x /usr/bin/git >/dev/null 2>&1; then
202     # chdir to a remote's directory (if the remote is on the local FS)
203     cdremote() {
204         if [ $# -gt 1 ]; then
205             echo "Usage: cdremote [remote_name]" >&2
206             return 1
207         fi
208
209         if [ -z "$1" ]; then
210             branch="`git rev-parse --abbrev-ref HEAD`"
211             remote="`git config --get branch.$branch.remote`"
212         else
213             remote="$1"
214         fi
215
216         if [ -n "$remote" ] && git config --get remote.$remote.url |
217                 grep -q '^\(file:/\|/\|\.\./\)'; then # (file:/ or / or ../ at the beginning)
218             cdgitpath remote.$remote.url
219             return
220         fi
221
222         if [ -n "$1" ]; then
223             echo "Cannot find directory for remote $1" >&2
224             echo "Usage: cdremote [remote_name]" >&2
225             return 1
226         fi
227
228         _list_remotes '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
229         if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
230             remote=${GIT_REMOTES[0]}
231             unset GIT_REMOTES
232             cdgitpath remote.$remote.url
233         else
234             unset GIT_REMOTES
235             echo "Cannot find directory for any remote" >&2
236             echo "Usage: cdremote [remote_name]" >&2
237             return 1
238         fi
239     }
240
241     # completion for `cd_worktree`
242
243     _cd_worktree_comp() {
244         local cur="${COMP_WORDS[COMP_CWORD]}"
245         COMPREPLY=(`compgen -W "$(
246             git worktree list | awk '{s=$3; gsub("[\\\\[\\\\]]", "", s); print s}'
247         )" -- "$cur"`)
248     }
249
250     _cd_worktree_comp_loader() {
251         _completion_loader git
252         unset _cd_worktree_comp_loader
253         complete -F _cd_worktree_comp cd_worktree
254         return 124
255     }
256
257     complete -F _cd_worktree_comp_loader cd_worktree
258
259     # completion for aliases in global .gitconfig
260
261     # fixup rbi rbia rbiap rbip - do refs name completion
262     _git_fixup() { __gitcomp_nl "$(__git_refs)" ; }
263     _git_rbi() { __gitcomp_nl "$(__git_refs)" ; }
264     _git_rbia() { __gitcomp_nl "$(__git_refs)" ; }
265     _git_rbiap() { __gitcomp_nl "$(__git_refs)" ; }
266     _git_rbip() { __gitcomp_nl "$(__git_refs)" ; }
267     #
268     # push-to-all-remotes - do branch name completion
269     _git_push_to_all_remotes() { __gitcomp_nl "$(__git_heads)" ; }
270
271     _cdgitpath_complete() {
272         local cur="${COMP_WORDS[COMP_CWORD]}"
273         COMPREPLY=(`compgen -W "$(__git_config_get_set_variables)" -- "$cur"`)
274     }
275
276     _git_open() {
277         _cdgitpath_complete
278     }
279
280     complete -F _cdgitpath_complete cdgitpath git-open
281
282     # list remotes with URLs matching a regexp
283     _list_remotes() {
284         if [ $# -ne 1 ]; then
285             echo "Usage: _list_remotes remote_regexp" >&2
286             return 1
287         fi
288         declare -ag GIT_REMOTES=()
289         local remote
290         for remote in `git remote`; do
291             if git config --get remote.$remote.url | grep -q "$1"; then
292                 GIT_REMOTES+=($remote)
293             fi
294         done
295     }
296
297     # completion for cdremote and git-open-remote - list remotes with a pattern
298     _list_remotes_completion() {
299         local cur="${COMP_WORDS[COMP_CWORD]}"
300         _list_remotes "$1"
301         COMPREPLY=(`compgen -W "${GIT_REMOTES[*]}" -- "$cur"`)
302         unset GIT_REMOTES
303     }
304
305     # completion for cdremote - list remotes with directories as URLs
306     _cdremote_complete() {
307         _list_remotes_completion '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
308         if [ ${#COMPREPLY[*]} -eq 0 ]; then
309             _list_remotes_completion .
310         fi
311     }
312
313     complete -F _cdremote_complete cdremote
314
315     # completion for git-open-remote - list remotes with http(s) URLs
316     _git_open_remote() {
317         _list_remotes_completion '^http\(s\)\?://'
318         if [ ${#COMPREPLY[*]} -eq 0 ]; then
319             _list_remotes_completion .
320         fi
321     }
322
323     complete -F _git_open_remote git-open-remote
324 fi
325
326
327 if [ "$SHLVL" -eq 1 -a "`type -t X`" = function ] && which startx >/dev/null 2>&1; then
328     # From https://stackoverflow.com/a/18839557
329
330     copy_function() {
331         test -n "$(declare -f "$1")" || return
332         eval "${_/$1/$2}"
333     }
334
335     rename_function() {
336         copy_function "$@" || return
337         unset -f "$1"
338     }
339
340     rename_function X _non_bash_X
341
342     X() { history -a; _non_bash_X; history -r; }
343 fi
344
345 if test -x /usr/bin/screen >/dev/null 2>&1; then
346     screen_newwin() {
347         history -a
348         screen "$@"
349     }
350 fi