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