/
bashrc
335 lines (292 loc) · 8.17 KB
/
bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
##
# Bash or Zsh runtime configuration file
#
# Author: Paul J. Lucas
##
[[ $- == *i* ]] || return # do .bashrc only if interactive
[ -t 0 ] || return # do .bashrc only if connected to a tty
##### Settings ################################################################
if [ -n "$BASH" ]
then
shopt -s cdable_vars
shopt -s histappend
elif [ -n "$ZSH_NAME" ]
then
setopt append_history
setopt auto_cd
setopt no_auto_param_slash
setopt cdable_vars
setopt hist_expire_dups_first
setopt hist_find_no_dups
setopt hist_ignore_all_dups
setopt hist_ignore_dups
setopt hist_reduce_blanks
setopt hist_save_no_dups
setopt inc_append_history
setopt interactive_comments
setopt no_list_beep
setopt no_list_types
setopt +o nomatch
setopt rm_star_silent
#setopt share_history
setopt sh_word_split
##
# Make zsh have the same behavior as bash/ksh93: typing Esc-K (in vi command
# mode) to move back in command-line history puts the cursor at the BEGINNING
# of the line; same for Esc-J, but forward in history.
##
bindkey -M vicmd j vi-down-line-or-history
bindkey -M vicmd k vi-up-line-or-history
##
# Make zsh have the same behavior as bash/ksh93: typing _ (in vi command
# mode) inserts a space (if needed) followed by the last word of the previous
# command.
##
insert-space-last-word() {
zle .vi-add-next # Enter insert mode after current pos.
zle .magic-space # Insert space if necessary.
zle .insert-last-word
}
zle -N insert-space-last-word
bindkey -M vicmd _ insert-space-last-word
##
# Remove the annoying delay required between Esc-/ to start a command history
# search.
##
bindkey -M viins -r "^[/"
zle_highlight+=(paste:none) # disable paste highlighting
fi
set -o ignoreeof # don't exit shell on ^D
set -o vi # use vi editing on command line
set +u # ignore unbound variables
stty echo erase '^?' intr '^C' kill '^X'
##### Functions ###############################################################
##
# Given an executable name and a path, return the component of the path
# containing the executable, if any.
##
find_path() {
local cmd=$1 cmd_path path_comp
echo $2 | tr ':' '\012' | while read path_comp
do
cmd_path="$path_comp/$cmd"
[ -x "$cmd_path" ] && { echo "$cmd_path"; break; }
done
}
##
# Given a string and a max_len, if length > max_len, returns the right-most
# length - max_len of the string prefixed by "..."; otherwise returns the
# string as-is.
##
str_right() {
local str=$1 max_len=$2
local len=${#str}
if (( $len > $max_len ))
then
local pos=$(( $len - $max_len + 3 ))
str="...$(echo "$str" | cut -c${pos}-)"
fi
echo "$str"
}
##### grep alias ##############################################################
LOCAL_PATH=/usr/local/bin:/opt/local/bin
GNU_PATH=$LOCAL_PATH:/usr/gnu/bin:/usr/bin:/bin
# Look for ggrep (GNU grep) first.
grep=$(find_path ggrep $GNU_PATH)
# Failing that, look for a local grep(1) and assume it's preferred.
[[ $grep ]] || grep=$(find_path grep $GNU_PATH)
# Otherwise, default to regular grep(1).
[[ $grep ]] || grep="grep"
# See if grep supports color.
if $grep --color=auto foo ~/.bashrc >/dev/null 2>&1
then
grep="$grep --color=auto"
GREP_COLOR="41" # for older grep
GREP_COLORS="bn=32:cx=36:fn=35:ln=33:mt=41;1:se=36"
export GREP_COLOR GREP_COLORS
fi
[[ $grep == "grep" ]] || alias grep="$grep"
##### ls aliases ##############################################################
# Look for GNU ls(1) first since it's more flexible.
ls=$(find_path gls $GNU_PATH)
# Failing that, look for a local ls(1) and assume it's preferred.
[[ $ls ]] || ls=$(find_path ls $LOCAL_PATH)
# Otherwise, default to regular ls(1).
[[ $ls ]] || ls="ls"
# Probe the options it accepts.
$ls -C >/dev/null 2>&1 && ls="$ls -C"
$ls -F >/dev/null 2>&1 && ls="$ls -F"
$ls --color >/dev/null 2>&1 && ls="$ls --color"
$ls --quoting-style=literal >/dev/null 2>&1 && ls="$ls --quoting-style=literal"
[[ $OS == Darwin ]] && $ls -Ix >/dev/null 2>&1 && ls="$ls -I'Icon'"
ls_nl='
'
if $ls -l --time-style='+%b %e %G$ls_nl%b %e %R' >/dev/null 2>&1
then ls_time="--time-style='+%b %e %G$ls_nl%b %e %R'"
fi
alias lf="$ls"; alias fl=lf
alias ll="$ls -l $ls_time"
##### Prompt ##################################################################
if [ -f ~/bin/set-ps ]
then
PROMPT_COMMAND=". ~/bin/set-ps"
export PROMPT_COMMAND
if [ -n "$ZSH_NAME" ]
then
precmd() {
eval "$PROMPT_COMMAND"
}
fi
fi
##### Convenience aliases #####################################################
alias docker-rm-ac='docker ps --all --quiet | xargs docker rm'
alias docker-rm-ai='docker images --quiet | xargs docker rmi'
alias docker-rm-all='docker system prune --all'
alias fn='find . -name'
alias ftp='\ftp -i'
alias gcc-macros='gcc -E -dM -xc /dev/null'
alias ggrep='cgrep -g'
alias json='python -mjson.tool'
alias mgrep='cgrep -m'
command -v cpus >/dev/null && alias mj="make -j$(( $(cpus) + 1 ))"
alias pr='\pr -F -o8 -l76 -w80'
alias psa='ps -A -opid,ppid,ruser,%cpu,command'
alias psu="ps -u $USER -opid,ppid,%cpu,command"
alias psag='psa | grep'
alias psug='psu | grep'
alias pyc='python -m py_compile'
alias tm="tmux new-session -As $HOSTNAME"
command -v vim >/dev/null && alias vi=vim
[[ $OS == Darwin ]] && {
alias macsleep='pmset sleepnow'
}
##### Typo aliases ############################################################
alias and=ant
alias bsr=bzr
alias ci=vi
alias iv=vi
alias car=cat
alias cta=cat
alias ceho=echo
alias ceoh=echo
alias cheo=echo
alias choe=echo
alias coeh=echo
alias cohe=echo
alias ecoh=echo
alias ehco=echo
alias ehoc=echo
alias eoch=echo
alias eohc=echo
alias oceh=echo
alias oche=echo
alias oech=echo
alias oehc=echo
alias ohce=echo
alias ohec=echo
alias gerp=grep
alias rgep=grep
alias ekam=make
alias keam=make
alias kaem=make
alias kame=make
alias eakm=make
alias aekm=make
alias akem=make
alias akme=make
alias eamk=make
alias aemk=make
alias amek=make
alias amke=make
alias ekma=make
alias kema=make
alias kmea=make
alias kmae=make
alias emka=make
alias meka=make
alias mkea=make
alias mkae=make
alias emak=make
alias meak=make
alias maek=make
alias pdw=pwd
alias wdp=pwd
alias wpd=pwd
##### Function aliases ########################################################
alias gdbbt=pjl_gdbbt
function pjl_gdbbt {
gdb --batch --quiet -ex 'thread apply all bt' -ex quit $*
}
alias ssh=pjl_ssh
function pjl_ssh {
\ssh $*; mycd .
}
##### cd ######################################################################
##
# Remember the old path so that the proper path will be displayed upon exiting
# a subshell back into an editor. "opwd" is intentionally not exported so that
# each bash will remember its own old path.
##
opwd=$PWD; trap "cd '$opwd'" 0
##
# Display current directory in status line of terminal (if it has one).
##
alias cd=mycd
TERM_STATUS_LINE_WIDTH=35
case "$TERM" in
iris-ansi*|sun-cmd|viterm|xterm*)
function mycd {
if [[ "$*" ]]
then \cd "$*" || return
else \cd
fi
dir=$(str_right "$PWD" $TERM_STATUS_LINE_WIDTH)
case "$TERM" in
iris-ansi*)
PSB='\033P1.y'; PSR='\033\\\c' ;;
sun-cmd)
PSB='\033]l'; PSR='\033\\';;
viterm)
PSB='\033L|'; PSR='|\c' ;;
xterm*)
PSB='\033]2;'; PSR='\007' ;;
esac
echo -en "$PSB$dir$PSR"
}
;;
screen*)
function mycd {
if [[ "$*" ]]
then \cd "$*" || return
else \cd
fi
dir=$(str_right "$PWD" $TERM_STATUS_LINE_WIDTH)
if [[ $TMUX ]]
then
tmux set-option -gq set-titles-string "$dir"
else
PSB='\033]2;'; PSR='\007'
echo -en "$PSB$dir$PSR"
fi
}
;;
*)
function mycd {
\cd "$*"
}
;;
esac
mycd .
##### Local bashrc ############################################################
[ -n "$BASH" -a -f ~/.bashrc-local ] && . ~/.bashrc-local
[ -n "$ZSH_NAME" -a -f ~/.zshrc-local ] && . ~/.zshrc-local
##### Miscellaneous ###########################################################
unset -f find_path
unset GNU_PATH
unset grep
unset LOCAL_PATH
unset ls
unset ls_nl
unset ls_time
# vim:set et sw=2 ts=2: