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