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