]> git.phdru.name Git - dotfiles.git/blob - .shellrc
.shellrc: test if completion already exists
[dotfiles.git] / .shellrc
1 #
2 # $HOME/.shellrc
3 #
4
5 OPS1="$OPS1\\$"
6 PS1="$OPS1 "
7
8
9 case "$SHELL" in
10 */bash)
11    [ "`type -t ls`" = alias ] && unalias ls
12    [ "`type -t ll`" = alias ] && unalias ll
13    [ "`type -t mc`" = alias ] && unalias mc
14
15    back() { cd - ${1:+"$@"}; }
16    clo() { clear; logout; }
17    eval 'functions() { typeset -f ${1:+"$@"}; }'
18    j() { jobs; }
19
20    if [ -z "$BASH_COMPLETION_COMPAT_DIR" ]; then
21       if [ "$SHELL" = /bin/bash ]; then
22          if [ -f /etc/bash_completion ]; then
23             . /etc/bash_completion
24          elif [ -d /etc/bash_completion.d ]; then
25             . /etc/bash_completion.d/*
26          fi
27       elif [ "$SHELL" = /usr/local/bin/bash ]; then
28          if [ -d /usr/local/etc/bash_completion.d ]; then
29             . /usr/local/etc/bash_completion.d/*
30          fi
31       else
32          echo "Unknown OS type, canot source bash_completion" >&2
33       fi
34    fi
35
36    has_completion() { return 0; }
37    if [ "$SHELL" = /bin/bash ]; then
38       if [ -d /etc/bash_completion.d -o -d /usr/share/bash-completion/completions ]; then
39          has_completion() { [ -r /etc/bash_completion.d/$1 -o -r /usr/share/bash-completion/completions/$1 ]; }
40       fi
41    elif [ "$SHELL" = /usr/local/bin/bash ]; then
42       if [ -d /usr/local/etc/bash_completion.d ]; then
43          has_completion() { [ -r /usr/local/etc/bash_completion.d/$1 ]; }
44       fi
45    fi
46
47    for cmd in builtin cgmem_nice command dbus-launch exec \
48       killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg su sudo \
49       time whence whereis which xargs; do
50          ! has_completion $cmd && complete -A command -o default $cmd
51    done
52
53    for cmd in dig host nslookup nc netcat nmap p ping ping6 socat \
54       telnet t tt \
55       tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
56       whois wget wget-m wget-wrapper ww; do
57          ! has_completion $cmd && complete -A hostname $cmd
58    done
59
60    for cmd in distribute ftp lftp r rsync \
61       s scp ssh smbclient tcpdump tshark wireshark; do
62          ! has_completion $cmd && complete -A hostname -o default $cmd;
63    done
64
65    complete -A job bg fg j jobs wait
66    complete -A variable unset
67
68    if [ -d "$HOME/lib/config" ]; then
69       complete -W "`cd \"$HOME/lib/config\" && echo *`" include
70    fi
71    complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
72    ;;
73
74 */ksh)
75    back() { cd - ${1:+"$@"}; }
76    clo() { clear; exit; }
77    j() { jobs; }
78    ;;
79
80 */sh)
81    clo() { clear; exit; }
82    ;;
83 esac
84
85
86 # clear history, clear screen and logout
87 #chlo() { cd; unset HISTFILE; rm -f .*history*; clo; }
88
89
90 #Pwd() { pwd | sed "s/.*\/\(.*\)\/\(.*\)\/\(.*\)\/\(.*\)/...\/\2\/\3\/\4/"; }
91 #PWd() { pwd | sed "s/.*\/\(.*\)\/\(.*\)/...\/\2/"; }
92
93
94 psg()   { ps auxw   | grep -i ${1:+"$@"} | grep -v '\(ps auxw\|grep\)'; }
95 pswg()  { ps auxwww | grep -i ${1:+"$@"} | grep -v '\(ps auxw\|grep\)'; }
96
97 # Like pgrep -f
98 pidOf() { ps auxwww | grep -i ${1:+"$@"} | grep -v '\(ps auxw\|grep\|pidOf\|KillAll\)' | awk '{print $2}'; }
99
100 # A kind of pkill/killall
101 KillAll() {
102    if [ -z "$2" ]; then
103       kill `pidOf "$1"`
104    else
105       kill "$1" `pidOf "$2"`
106    fi
107 }
108
109
110 x() { exit; }
111 X() { startx >> .Xserver.log 2>&1; cyr; }
112
113
114 include()
115 {
116    cfg="$1"
117    if [ -f "./$cfg" -a -r "./$cfg" ]; then
118       echo "Reading config file \`$cfg'" 1>&2
119       . "./$cfg"
120    elif [ -f "$HOME/lib/config/$cfg" -a -r "$HOME/lib/config/$cfg" ]; then
121       echo "Reading config file \`$HOME/lib/config/$cfg'" 1>&2
122       . "$HOME/lib/config/$cfg"
123    else
124       echo "Cannot find config file \`$cfg'" 1>&2
125    fi
126 }
127
128
129 mc() {
130    if [ -n "$SLOWTERM" ]; then
131       MC_SLOW="--slow"
132    fi
133
134    case "$TERM" in
135       screen*) # screen, screen.rxvt
136          if [ "$TERM" = screen.rxvt ]; then
137             MC_SAVE_TERM="$TERM"
138             TERM=rxvt
139          fi
140          MC_XTERM="-x"
141       ;;
142    esac
143
144    #TMPDIR="$HOME"/tmp
145    #export TMPDIR
146
147    MC_FOUND=NO
148
149    for wrapper in \
150          /usr/local/share/mc/bin/mc-wrapper.sh \
151          /usr/local/libexec/mc/mc-wrapper.sh \
152          /usr/share/mc/bin/mc-wrapper.sh; do
153       if [ -r $wrapper ]; then
154          . $wrapper $MC_SLOW $MC_XTERM "$@"
155          rc=$?
156          MC_FOUND=YES
157          break
158       fi
159    done
160
161    if [ $MC_FOUND = NO ]; then
162       for mc in /usr/local/bin/mc /usr/bin/mc; do
163          if [ -x $mc ]; then
164             MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER"
165             if [ ! -d MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER" ]; then
166                MC_TMP_DIR="${TMPDIR-/tmp}"
167             fi
168             MC_PWD_FILE="$MC_TMP_DIR/mc.pwd.$$"
169             mc_type="`mc_type.py $mc`"
170             if [ "$mc_type" = old ]; then
171                $mc -P $MC_SLOW $MC_XTERM "$@" > "$MC_PWD_FILE"
172             elif [ "$mc_type" = new ]; then
173                $mc -P "$MC_PWD_FILE" $MC_SLOW $MC_XTERM "$@"
174             else
175                continue
176             fi
177             rc=$?
178             MC_FOUND=YES
179             cd "`cat \"$MC_PWD_FILE\"`" && /bin/rm "$MC_PWD_FILE"
180             break
181          fi
182       done
183    fi
184
185    if [ $MC_FOUND = NO ]; then
186       echo "Cannot find mc-wrapper.sh or mc" >&2
187       rc=1
188    fi
189
190    [ -n "$MC_SAVE_TERM" ] && TERM="$MC_SAVE_TERM"
191    unset MC_FOUND MC_PWD_FILE MC_SAVE_TERM MC_SLOW MC_TMP_DIR MC_XTERM
192    return $rc
193 }
194
195
196 tmux() {
197    case "$TERM" in
198       rxvt)
199          TMUX_SAVE_TERM="$TERM"
200          TERM=rxvt-unicode
201       ;;
202    esac
203
204    command tmux "$@"
205    rc=$?
206
207    [ -n "$TMUX_SAVE_TERM" ] && TERM="$TMUX_SAVE_TERM"
208    unset TMUX_SAVE_TERM
209    return $rc
210 }