]> git.phdru.name Git - dotfiles.git/blob - .shellrc
.shellrc: always pass options -FRX to less
[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 include()
126 {
127    cfg="$1"
128    if [ -f "./$cfg" -a -r "./$cfg" ]; then
129       echo "Reading config file \`$cfg'" 1>&2
130       . "./$cfg"
131    elif [ -f "$HOME/lib/config/$cfg" -a -r "$HOME/lib/config/$cfg" ]; then
132       echo "Reading config file \`$HOME/lib/config/$cfg'" 1>&2
133       . "$HOME/lib/config/$cfg"
134    else
135       echo "Cannot find config file \`$cfg'" 1>&2
136    fi
137 }
138
139
140 mc() {
141    if [ -n "$SLOWTERM" ]; then
142       MC_SLOW="--slow"
143    fi
144
145    case "$TERM" in
146       screen*) # screen, screen.rxvt
147          if [ "$TERM" = screen.rxvt ]; then
148             MC_SAVE_TERM="$TERM"
149             TERM=rxvt
150          fi
151          MC_XTERM="-x"
152       ;;
153    esac
154
155    #TMPDIR="$HOME"/tmp
156    #export TMPDIR
157
158    MC_FOUND=NO
159
160    for wrapper in \
161          /usr/local/share/mc/bin/mc-wrapper.sh \
162          /usr/local/libexec/mc/mc-wrapper.sh \
163          /usr/share/mc/bin/mc-wrapper.sh; do
164       if [ -r $wrapper ]; then
165          . $wrapper $MC_SLOW $MC_XTERM "$@"
166          rc=$?
167          MC_FOUND=YES
168          break
169       fi
170    done
171
172    if [ $MC_FOUND = NO ]; then
173       for mc in /usr/local/bin/mc /usr/bin/mc; do
174          if [ -x $mc ]; then
175             MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER"
176             if [ ! -d MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER" ]; then
177                MC_TMP_DIR="${TMPDIR-/tmp}"
178             fi
179             MC_PWD_FILE="$MC_TMP_DIR/mc.pwd.$$"
180             mc_type="`mc_type.py $mc`"
181             if [ "$mc_type" = old ]; then
182                $mc -P $MC_SLOW $MC_XTERM "$@" > "$MC_PWD_FILE"
183             elif [ "$mc_type" = new ]; then
184                $mc -P "$MC_PWD_FILE" $MC_SLOW $MC_XTERM "$@"
185             else
186                continue
187             fi
188             rc=$?
189             MC_FOUND=YES
190             cd "`cat \"$MC_PWD_FILE\"`" && /bin/rm "$MC_PWD_FILE"
191             break
192          fi
193       done
194    fi
195
196    if [ $MC_FOUND = NO ]; then
197       echo "Cannot find mc-wrapper.sh or mc" >&2
198       rc=1
199    fi
200
201    [ -n "$MC_SAVE_TERM" ] && TERM="$MC_SAVE_TERM"
202    unset MC_FOUND MC_PWD_FILE MC_SAVE_TERM MC_SLOW MC_TMP_DIR MC_XTERM
203    return $rc
204 }
205
206
207 mkcd() {
208    if [ $# -ne 1 ]; then
209       echo "Usage: mkcd directory_name" >&2
210    elif [ -d "$1" ]; then
211       cd "$1"
212    elif [ -e "$1" ]; then
213       echo "Error: $1 is a file" >&2
214    else
215       mkdir -p "$1" && cd "$1"
216    fi
217 }
218
219
220 tmux() {
221    case "$TERM" in
222       rxvt)
223          TERM=rxvt-unicode command tmux "$@"
224       ;;
225
226       *)
227          command tmux "$@"
228       ;;
229    esac
230 }