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