]> git.phdru.name Git - dotfiles.git/blob - .shellrc
.profile: copy some code and comments from Debian /etc/skel
[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 # append to the history file, don't overwrite it
16 shopt -s histappend
17
18 # check the window size after each command and, if necessary,
19 # update the values of LINES and COLUMNS.
20 shopt -s checkwinsize
21
22 # If set, the pattern "**" used in a pathname expansion context will
23 # match all files and zero or more directories and subdirectories.
24 #shopt -s globstar
25
26
27 #if [ -x /usr/bin/dircolors ]; then
28    # I don't like these aliases - they work only in command line
29    # but not in scripts I run from command line.
30    # Color parameters must be passed via environment.
31    #alias ls='ls --color=auto'
32    #alias dir='dir --color=auto'
33    #alias vdir='vdir --color=auto'
34
35    #alias grep='grep --color=auto'
36    #alias fgrep='fgrep --color=auto'
37    #alias egrep='egrep --color=auto'
38 #fi
39
40
41 OPS1="$OPS1\\$"
42 PS1="$OPS1 "
43
44
45 case "$HOME" in
46    /home/*)
47       if [ -L /home ]; then
48          START_DIR="`pwd`" &&
49          cd "$HOME" &&
50          HOME="`pwd`" &&
51          cd "$START_DIR"
52       fi
53    ;;
54 esac
55
56 case "$SHELL" in
57 */bash)
58    [ "`type -t ls`" = alias ] && unalias ls
59    [ "`type -t ll`" = alias ] && unalias ll
60    [ "`type -t mc`" = alias ] && unalias mc
61
62    back() { cd - ${1:+"$@"}; }
63    clo() { clear; logout; }
64    eval 'functions() { typeset -f ${1:+"$@"}; }'
65    j() { jobs; }
66
67    if [ -z "$BASH_COMPLETION_COMPAT_DIR" ]; then
68       if [ "$SHELL" = /bin/bash ]; then
69          if [ -f /etc/bash_completion ]; then
70             . /etc/bash_completion
71          elif [ -d /etc/bash_completion.d ]; then
72             . /etc/bash_completion.d/*
73          fi
74       elif [ "$SHELL" = /usr/local/bin/bash ]; then
75          if [ -d /usr/local/etc/bash_completion.d ]; then
76             . /usr/local/etc/bash_completion.d/*
77          fi
78       else
79          echo "Unknown OS type, canot source bash_completion" >&2
80       fi
81    fi
82
83    has_completion() { return 0; }
84    if [ "$SHELL" = /bin/bash ]; then
85       if [ -d /etc/bash_completion.d -o -d /usr/share/bash-completion/completions ]; then
86          has_completion() { [ -r /etc/bash_completion.d/$1 -o -r /usr/share/bash-completion/completions/$1 ]; }
87       fi
88    elif [ "$SHELL" = /usr/local/bin/bash ]; then
89       if [ -d /usr/local/etc/bash_completion.d ]; then
90          has_completion() { [ -r /usr/local/etc/bash_completion.d/$1 ]; }
91       fi
92    fi
93
94    for cmd in builtin cgmem_nice command dbus-launch exec \
95       killall man nice nohup pidof pidOf KillAll pgrep pkill psg pswg su sudo \
96       time whence whereis which xargs; do
97          ! has_completion $cmd && complete -o default -A command $cmd
98    done
99
100    for cmd in dig host nslookup nc netcat nmap p ping ping6 socat \
101       telnet t tt \
102       tcptraceroute tcptraceroute6 tracert tracert6 traceroute traceroute6 \
103       whois wget wget-m wget-wrapper ww; do
104          ! has_completion $cmd && complete -A hostname $cmd
105    done
106
107    for cmd in distribute ftp lftp r rsync \
108       s scp ssh smbclient tcpdump tshark wireshark; do
109          ! has_completion $cmd && complete -o default -A hostname $cmd;
110    done
111    unset has_completion
112
113    complete -A job bg fg j jobs wait
114    complete -A variable unset
115
116    if [ -d "$HOME/lib/config" ]; then
117       complete -W "`cd \"$HOME/lib/config\" && echo *`" include
118    fi
119    complete -W "`echo $BROWSER | sed 's/:/ /g'`" start-browser
120
121    which pip >/dev/null 2>&1 && eval "`pip completion --bash`"
122    ;;
123
124 */ksh)
125    back() { cd - ${1:+"$@"}; }
126    clo() { clear; exit; }
127    j() { jobs; }
128    ;;
129
130 */sh)
131    clo() { clear; exit; }
132    ;;
133 esac
134
135
136 # clear history, clear screen and logout
137 #chlo() { cd; unset HISTFILE; rm -f .sh_history; history -c; clo; }
138
139
140 #Pwd() { pwd | sed "s/.*\/\(.*\)\/\(.*\)\/\(.*\)\/\(.*\)/...\/\2\/\3\/\4/"; }
141 #PWd() { pwd | sed "s/.*\/\(.*\)\/\(.*\)/...\/\2/"; }
142
143
144 psg()   { ps auxw   | grep -i ${1:+"$@"} | grep -v '\(ps auxw\|grep\)'; }
145 pswg()  { ps auxwww | grep -i ${1:+"$@"} | grep -v '\(ps auxw\|grep\)'; }
146
147 # Like pgrep -f
148 pidOf() { ps auxwww | grep -i ${1:+"$@"} | grep -v '\(ps auxw\|grep\|pidOf\|KillAll\)' | awk '{print $2}'; }
149
150 # A kind of pkill/killall
151 KillAll() {
152    if [ -z "$2" ]; then
153       kill `pidOf "$1"`
154    else
155       kill "$1" `pidOf "$2"`
156    fi
157 }
158
159
160 x() { exit; }
161 X() { startx >> .Xserver.log 2>&1; cyr; }
162
163
164 include()
165 {
166    cfg="$1"
167    if [ -f "./$cfg" -a -r "./$cfg" ]; then
168       echo "Reading config file \`$cfg'" 1>&2
169       . "./$cfg"
170    elif [ -f "$HOME/lib/config/$cfg" -a -r "$HOME/lib/config/$cfg" ]; then
171       echo "Reading config file \`$HOME/lib/config/$cfg'" 1>&2
172       . "$HOME/lib/config/$cfg"
173    else
174       echo "Cannot find config file \`$cfg'" 1>&2
175    fi
176 }
177
178
179 mc() {
180    case "$SHELL" in
181       */bash)
182          MC_SAVE_OPS1="$OPS1"
183          OPS1="\u@\h "
184       ;;
185    esac
186
187    if [ -n "$SLOWTERM" ]; then
188       MC_SLOW="--slow"
189    fi
190
191    case "$TERM" in
192       screen*) # screen, screen.rxvt
193          if [ "$TERM" = screen.rxvt ]; then
194             MC_SAVE_TERM="$TERM"
195             TERM=rxvt
196          fi
197          MC_XTERM="-x"
198       ;;
199    esac
200
201    #TMPDIR="$HOME"/tmp
202    #export TMPDIR
203
204    MC_FOUND=NO
205
206    for wrapper in \
207          /usr/local/share/mc/bin/mc-wrapper.sh \
208          /usr/local/libexec/mc/mc-wrapper.sh \
209          /usr/share/mc/bin/mc-wrapper.sh; do
210       if [ -r $wrapper ]; then
211          . $wrapper $MC_SLOW $MC_XTERM "$@"
212          rc=$?
213          MC_FOUND=YES
214          break
215       fi
216    done
217
218    if [ $MC_FOUND = NO ]; then
219       for mc in /usr/local/bin/mc /usr/bin/mc; do
220          if [ -x $mc ]; then
221             MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER"
222             if [ ! -d MC_TMP_DIR="${TMPDIR-/tmp}/mc-$USER" ]; then
223                MC_TMP_DIR="${TMPDIR-/tmp}"
224             fi
225             MC_PWD_FILE="$MC_TMP_DIR/mc.pwd.$$"
226             mc_type="`mc_type.py $mc`"
227             if [ "$mc_type" = old ]; then
228                $mc -P $MC_SLOW $MC_XTERM "$@" > "$MC_PWD_FILE"
229             elif [ "$mc_type" = new ]; then
230                $mc -P "$MC_PWD_FILE" $MC_SLOW $MC_XTERM "$@"
231             else
232                continue
233             fi
234             rc=$?
235             MC_FOUND=YES
236             cd "`cat \"$MC_PWD_FILE\"`" && /bin/rm "$MC_PWD_FILE"
237             break
238          fi
239       done
240    fi
241
242    if [ $MC_FOUND = NO ]; then
243       echo "Cannot find mc-wrapper.sh or mc" >&2
244       rc=1
245    fi
246
247    [ -n "$MC_SAVE_OPS1" ] && OPS1="$MC_SAVE_OPS1"
248    [ -n "$MC_SAVE_TERM" ] && TERM="$MC_SAVE_TERM"
249    unset MC_FOUND MC_PWD_FILE MC_SAVE_OPS1 MC_SAVE_TERM \
250          MC_SLOW MC_TMP_DIR MC_XTERM
251
252    return $rc
253 }
254
255
256 mkcd() {
257    if [ $# -ne 1 ]; then
258       echo "Usage: mkcd directory_name" >&2
259    elif [ -d "$1" ]; then
260       cd "$1"
261    elif [ -e "$1" ]; then
262       echo "Error: $1 is a file" >&2
263    else
264       mkdir -p "$1" && cd "$1"
265    fi
266 }
267
268
269 tmux() {
270    case "$TERM" in
271       rxvt)
272          TERM=rxvt-unicode command tmux "$@"
273       ;;
274
275       *)
276          command tmux "$@"
277       ;;
278    esac
279 }