]> git.phdru.name Git - dotfiles.git/blob - .shellrc
bash: Separate host from current dir in `PS1` with `:`
[dotfiles.git] / .shellrc
1 #
2 # $HOME/.shellrc
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 if test -n "$BASH_VERSION"; then
19    # append to the history file, don't overwrite it
20    shopt -s histappend
21
22    # check the window size after each command and, if necessary,
23    # update the values of LINES and COLUMNS.
24    shopt -s checkwinsize
25
26    # If set, the pattern "**" used in a pathname expansion context will
27    # match all files and zero or more directories and subdirectories.
28    #shopt -s globstar
29 fi
30
31
32 #if [ -x /usr/bin/dircolors ]; then
33 #   # I don't like these aliases - they work only in command line
34 #   # but not in scripts I run from command line.
35 #   # Color parameters must be passed via environment.
36 #   # So instead I put them in shell scripts in ~/bin.
37 #   alias ls='ls --color=auto'
38 #   alias dir='dir --color=auto'
39 #   alias vdir='vdir --color=auto'
40 #
41 #   alias grep='grep --color=auto'
42 #   alias fgrep='fgrep --color=auto'
43 #   alias egrep='egrep --color=auto'
44 #
45 #   alias dmesg='dmesg --human'
46 #fi
47
48
49 case "$HOME" in
50    /home/*)
51       if [ -L /home ]; then
52          START_DIR="`pwd`" &&
53          cd "$HOME" &&
54          HOME="`pwd`" &&
55          cd "$START_DIR"
56       fi
57    ;;
58 esac
59
60 PS1="$OPS1$SHLVL "
61
62 if test -n "$BASH_VERSION"; then
63    [ "`type -t ls`" = alias ] && unalias ls
64    [ "`type -t ll`" = alias ] && unalias ll
65    [ "`type -t mc`" = alias ] && unalias mc
66
67    back() { cd - "$@"; }
68    functions() { typeset -f "$@"; }
69    j() { jobs; }
70
71    has_completion() { return 0; }
72    if [ -z "$BASH_COMPLETION_COMPAT_DIR" ]; then
73       if [ "$SHELL" = /bin/bash ]; then
74          if [ -d /usr/share/bash-completion -a -f /usr/share/bash-completion/bash_completion ]; then
75             . /usr/share/bash-completion/bash_completion
76             has_completion() { [ -r /usr/share/bash-completion/completions/$1 ]; }
77          elif [ -f /etc/bash_completion ]; then
78             . /etc/bash_completion
79             has_completion() { [ -r /etc/bash_completion.d/$1 ]; }
80          elif [ -d /etc/bash_completion.d ]; then
81             for _compf in /etc/bash_completion.d/*; do
82                . $_compf
83             done
84             has_completion() { [ -r /etc/bash_completion.d/$1 ]; }
85          fi
86       elif [ "$SHELL" = /usr/local/bin/bash ]; then
87          if [ -d /usr/local/etc/bash_completion.d ]; then
88             for _compf in /usr/local/etc/bash_completion.d/*; do
89                . $_compf
90             done
91             has_completion() { [ -r /usr/local/etc/bash_completion.d/$1 ]; }
92          fi
93       else
94          echo "Unknown OS type, cannot source bash_completion" >&2
95       fi
96    fi
97
98    for cmd in builtin cgmem_nice command dbus-launch exec \
99       killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg su sudo \
100       time whence whereis which xargs; do
101          ! has_completion $cmd && complete -o default -A command $cmd
102    done
103
104    for cmd in dig host mtr nslookup nc netcat nmap p ping ping6 socat \
105       telnet t tt \
106       tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
107       whois wd wget wget-download wget-m wget-wrapper ww; do
108          ! has_completion $cmd && complete -A hostname $cmd
109    done
110
111    for cmd in distribute ftp lftp r rsync \
112       s scp ssh smbclient tcpdump tshark wireshark; do
113          ! has_completion $cmd && complete -o default -A hostname $cmd
114    done
115
116    if [ -d "$HOME"/.bash_completion.d ]; then
117       for _compf in "$HOME"/.bash_completion.d/*; do
118          . $_compf
119       done
120    fi
121
122    delegate_completion() {
123       local prog programs
124       prog=$1
125       if has_completion $prog; then
126          shift
127          programs="$@"
128          eval "_${prog}_completion_loader() {
129             _completion_loader $prog
130             complete -o nospace -F _$prog $programs
131             unset _${prog}_completion_loader
132             return 124
133          }"
134          complete -F _${prog}_completion_loader $programs
135       fi
136    }
137
138    delegate_completion make m
139    delegate_completion ping p
140    delegate_completion rsync r rsync_cgmn rsync_cgmn_recode
141    delegate_completion ssh s
142    delegate_completion wget wget-m wget-wrapper ww
143
144    unset has_completion delegate_completion
145
146    complete -A job bg fg j jobs wait
147    complete -A variable -A function unset
148
149    if [ -d "$HOME/lib/config" ]; then
150       complete -W "`cd \"$HOME/lib/config\" && echo *`" include
151    fi
152    complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
153
154    if [ "`type -t short_curdir`" != function -a \
155          -r "$HOME"/admin/prog/bash_prompt ]; then
156       . "$HOME"/admin/prog/bash_prompt
157    fi
158
159    PS1="\`cgmem_which_prompt\`$OPS1$SHLVL "
160
161    #if type -p pip >/dev/null 2>&1; then
162    #   eval "`pip completion --bash`"
163    #   rm -rf /tmp/pip_build_"$USER"
164    #fi
165
166    #if which pyenv >/dev/null 2>&1; then
167    #   eval "`pyenv init -`"
168    #   eval "`pyenv virtualenv-init -`"
169    #fi
170
171    . virtualenvwrapper_lazy.sh 2>/dev/null
172
173    if [ -n "$VIRTUAL_ENV" ] && ! type deactivate >/dev/null 2>&1; then
174       . "$VIRTUAL_ENV/bin/activate"
175    fi
176
177    # Clean up python virtual environment on exit
178    # See https://virtualenvwrapper.readthedocs.io/en/latest/tips.html#clean-up-environments-on-exit
179
180    trap '[ "$VIRTUAL_ENV" ] && deactivate' EXIT
181
182 elif test -n "$KSH_VERSION" -o -n "$FCEDIT"; then
183    back() { cd - "$@"; }
184    j() { jobs; }
185 fi
186
187
188 # clear screen and history, logout
189 chlo() { clear; cd; >$HISTFILE; unset HISTFILE; history -c; logout || exit; }
190
191
192 if test -x /usr/bin/git >/dev/null 2>&1; then
193    # chdir to a directory
194    cdgitpath() {
195       if [ $# -ne 1 ]; then
196          echo "Usage: cdgitpath path_key" >&2
197          return 1
198       fi
199
200       path_key="$1"
201       if [ -z "$path_key" ]; then
202          echo "Usage: cdgitpath path_key" >&2
203          return 1
204       fi
205
206       path_url="`git config --get --path $path_key`"
207       if echo "$path_url" | grep -q '^file:/'; then
208          path_url="`echo \"$path_url\" | sed 's@^file:/\+@/@'`"
209       fi
210       cd "$path_url"
211    }
212
213    # chdir to a remote's directory (if the remote is on the local FS)
214    cdremote() {
215       if [ $# -gt 1 ]; then
216          echo "Usage: cdremote [remote_name]" >&2
217          return 1
218       fi
219
220       if [ -z "$1" ]; then
221          branch="`git rev-parse --abbrev-ref HEAD`"
222          remote="`git config --get branch.$branch.remote`"
223       else
224          remote="$1"
225       fi
226
227       if [ -n "$remote" ] && git config --get remote.$remote.url |
228             grep -q '^\(file:/\|/\|\.\./\)'; then # (file:/ or / or ../ at the beginning)
229          cdgitpath remote.$remote.url
230       else
231
232          if [ -n "$1" ]; then
233             echo "Cannot find directory for remote $1" >&2
234             echo "Usage: cdremote [remote_name]" >&2
235             return 1
236          fi
237
238          if test -n "$BASH_VERSION"; then
239             _list_remotes '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
240             if [ ${#GIT_REMOTES[*]} -eq 1 ]; then
241                remote=${GIT_REMOTES[0]}
242                unset GIT_REMOTES
243                cdgitpath remote.$remote.url
244             else
245                unset GIT_REMOTES
246                echo "Cannot find directory for any remote" >&2
247                echo "Usage: cdremote [remote_name]" >&2
248                return 1
249             fi
250          else
251             echo "Cannot find directory for remote $1" >&2
252             echo "Usage: cdremote [remote_name]" >&2
253             return 1
254          fi
255       fi
256    }
257
258    if test -n "$BASH_VERSION"; then
259       # completion for aliases in global .gitconfig
260
261       # fixup rbi rbia rbiap rbip - do refs name completion
262       _git_fixup() { __gitcomp_nl "$(__git_refs)" ; }
263       _git_rbi() { __gitcomp_nl "$(__git_refs)" ; }
264       _git_rbia() { __gitcomp_nl "$(__git_refs)" ; }
265       _git_rbiap() { __gitcomp_nl "$(__git_refs)" ; }
266       _git_rbip() { __gitcomp_nl "$(__git_refs)" ; }
267       #
268       # push-to-all-remotes - do branch name completion
269       _git_push_to_all_remotes() { __gitcomp_nl "$(__git_heads)" ; }
270
271       _cdgitpath_complete() {
272          local cur="${COMP_WORDS[COMP_CWORD]}"
273          COMPREPLY=(`compgen -W "$(__git_config_get_set_variables)" -- "$cur"`)
274       }
275
276       _git_open() {
277          _cdgitpath_complete
278       }
279
280       complete -F _cdgitpath_complete cdgitpath git-open
281
282       # list remotes with URLs matching a regexp
283       _list_remotes() {
284          if [ $# -ne 1 ]; then
285             echo "Usage: _list_remotes remote_regexp" >&2
286             return 1
287          fi
288          declare -ag GIT_REMOTES=()
289          local remote
290          for remote in `git remote`; do
291             if git config --get remote.$remote.url | grep -q "$1"; then
292                GIT_REMOTES+=($remote)
293             fi
294          done
295       }
296
297       # completion for cdremote and git-open-remote - list remotes with a pattern
298       _list_remotes_completion() {
299          local cur="${COMP_WORDS[COMP_CWORD]}"
300          _list_remotes "$1"
301          COMPREPLY=(`compgen -W "${GIT_REMOTES[*]}" -- "$cur"`)
302          unset GIT_REMOTES
303       }
304
305       # completion for cdremote - list remotes with directories as URLs
306       _cdremote_complete() {
307          _list_remotes_completion '^\(file:/\|/\|\.\./\)' # (file:/ or / or ../ at the beginning)
308          if [ ${#COMPREPLY[*]} -eq 0 ]; then
309             _list_remotes_completion .
310          fi
311       }
312
313       complete -F _cdremote_complete cdremote
314
315       # completion for git-open-remote - list remotes with http(s) URLs
316       _git_open_remote() {
317          _list_remotes_completion '^http\(s\)\?://'
318          if [ ${#COMPREPLY[*]} -eq 0 ]; then
319             _list_remotes_completion .
320          fi
321       }
322
323       complete -F _git_open_remote git-open-remote
324    fi
325 fi
326
327
328 include() {
329    cfg="$1"
330    if [ -f "./$cfg" -a -r "./$cfg" ]; then
331       echo "Reading config file \`$cfg'" 1>&2
332       . "./$cfg"
333    elif [ -f "$HOME/lib/config/$cfg" -a -r "$HOME/lib/config/$cfg" ]; then
334       echo "Reading config file \`$HOME/lib/config/$cfg'" 1>&2
335       . "$HOME/lib/config/$cfg"
336    else
337       echo "Cannot find config file \`$cfg'" 1>&2
338    fi
339 }
340
341
342 mc() {
343    if test -n "$BASH_VERSION"; then
344       MC_SAVE_OPS1="$OPS1"
345       if [ "`type -t short_curdir`" = function ]; then
346          OPS1="\u@\${HOSTNAME::5}:\`short_curdir\` \\$"
347       else
348          OPS1="\u@\h:\W \\$"
349       fi
350    fi
351
352    if [ -n "$SLOWTERM" ]; then
353       MC_SLOW="--slow"
354    fi
355
356    case "$TERM" in
357       screen*) # screen, screen.rxvt
358          MC_XTERM="-x"
359       ;;
360    esac
361
362    #TMPDIR="$HOME"/tmp
363    #export TMPDIR
364
365    MC_FOUND=NO
366
367    for wrapper in \
368          /usr/local/share/mc/bin/mc-wrapper.sh \
369          /usr/local/libexec/mc/mc-wrapper.sh \
370          /usr/share/mc/bin/mc-wrapper.sh; do
371       if [ -r $wrapper ]; then
372          . $wrapper $MC_SLOW $MC_XTERM "$@"
373          rc=$?
374          MC_FOUND=YES
375          break
376       fi
377    done
378
379    if [ $MC_FOUND = NO ]; then
380       for mc in /usr/local/bin/mc /usr/bin/mc; do
381          if [ -x $mc ]; then
382             MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER"
383             if [ ! -d MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER" ]; then
384                MC_TMP_DIR="${TMPDIR-/tmp}"
385             fi
386             MC_PWD_FILE="$MC_TMP_DIR/mc.pwd.$$"
387             mc_type="`mc_type.py $mc`"
388             if [ "$mc_type" = old ]; then
389                $mc -P $MC_SLOW $MC_XTERM "$@" > "$MC_PWD_FILE"
390             elif [ "$mc_type" = new ]; then
391                $mc -P "$MC_PWD_FILE" $MC_SLOW $MC_XTERM "$@"
392             else
393                continue
394             fi
395             rc=$?
396             MC_FOUND=YES
397             cd "`cat \"$MC_PWD_FILE\"`" && /bin/rm "$MC_PWD_FILE"
398             break
399          fi
400       done
401    fi
402
403    if [ $MC_FOUND = NO ]; then
404       echo "Cannot find mc-wrapper.sh or mc" >&2
405       rc=1
406    fi
407
408    [ -n "$MC_SAVE_OPS1" ] && OPS1="$MC_SAVE_OPS1"
409    unset MC_FOUND MC_PWD_FILE MC_SAVE_OPS1 MC_SLOW MC_TMP_DIR MC_XTERM
410
411    return $rc
412 }
413
414
415 mkcd() {
416    if [ $# -ne 1 ]; then
417       echo "Usage: mkcd directory_name" >&2
418    elif [ -d "$1" ]; then
419       cd "$1"
420    elif [ -e "$1" ]; then
421       echo "Error: $1 is a file" >&2
422    else
423       mkdir -p "$1" && cd "$1"
424    fi
425 }
426
427
428 if which tmux >/dev/null 2>&1; then
429    tmux() {
430       case "$TERM" in
431          rxvt)
432             TERM=rxvt-unicode command tmux "$@"
433          ;;
434
435          *)
436             command tmux "$@"
437          ;;
438       esac
439    }
440 fi
441
442
443 if [ "$SHLVL" -eq 1 ] && which startx >/dev/null 2>&1; then
444    X() { startx >> .Xserver.log 2>&1; cyr; rm -f .Xauthority; }
445 fi
446
447 x() { exit; }