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