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