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