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