-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc
379 lines (316 loc) · 8.73 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# Bash completion - pretty important!@#
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
set show-all-if-ambiguous on
# {{{ Prompt
setprompt(){
# Capture last return code
local rts=$?
# Get path with tilde for home
if [[ "$PWD" == "$HOME" ]]; then
local dir="~"
elif [[ "${PWD:0:${#HOME}}" == "$HOME" ]]; then
local dir="~${PWD:${#HOME}}"
else
local dir=$PWD
fi
# Truncate path if it's long
if [[ ${#dir} -gt 19 ]]; then
local offset=$((${#dir}-18))
dir="+${dir:$offset:18}"
fi
# Path color indicates host
case "$HOSTNAME" in
"ayu") local dircol="\[\e[1;35m\]"; ;; # Desktop
"misuzu") local dircol="\[\e[1;32m\]"; ;; # Laptop
"izaya") local dircol="\[\e[1;31m\]"; ;; # Server
*) local dircol="\[\e[1;37m\]"; ;; # Other
esac
# Marker char indicates root or user
[[ $UID -eq 0 ]] && local marker='#' || local marker='$'
# Marker color indicates successful execution
[[ $rts -eq 0 ]] && local colormarker="\[\e[1;37m\]$marker" \
|| local colormarker="\[\e[1;31m\]$marker"
# Set PS1
PS1="${dircol}${dir} ${colormarker}\[\e[0;37m\] "
# Append history to saved file
history -a
}
PROMPT_COMMAND="setprompt &> /dev/null"
# }}}
# {{{ Bash settings
# Applications
export EDITOR='emacs -nw'
export BROWSER='google-chrome'
# History control
export HISTCONTROL="ignoreboth"
export HISTFILESIZE=500000
export HISTIGNORE="cd:..*:no:na:clear:reset:j *:exit:hc:h:-"
# Check for current bash version
if [[ ${BASH_VERSINFO[0]} -ge 4 ]]; then
shopt -s autocd cdspell
shopt -s dirspell globstar
fi
# General options
shopt -s cmdhist nocaseglob
shopt -s histappend extglob
# Complete only directories on cd
complete -d cd
# Don't echo ^C
stty -ctlecho
# Load autojump
if [[ -f /etc/profile.d/autojump.bash ]]; then
. /etc/profile.d/autojump.bash
fi
# Give ls more colors
if [[ -x /bin/dircolors ]]; then
eval $(/bin/dircolors ~/.dircolors)
fi
# }}}
# {{{ Key bindings and macros
case "$-" in *i*)
# Remove annoying fc map
bind -m vi -r v
# Walk through completions with ^N
bind "\C-n: menu-complete"
# Clear screen with ^L
bind "\C-l: clear-screen"
# Add pager pipe with ^T
# Background & ignore with <C-b>
bind '"\C-b":" &> /dev/null &\C-m"'
# Search
bind '"\C-w":" | ack "'
# Reset and clear
bind '"\C-a":"reset ; clear \C-m"'
bind '"\C-k":"cd ; clear \C-m"'
bind '"\C-e":"clear \C-m"'
;; esac;
# }}}
# {{{ General shortcuts
# Ls
alias ls='ls --color=auto -Fh --group-directories-first'
alias ll='ls -lah'
alias no='ls'
alias na='ll'
alias g='gcc -Wall -Wextra -pedantic'
# Devtodo
alias t='todo'
alias td='todo --database ~/.todo.daily'
alias ts='todo --database ~/.todo.schedule'
# Editor
alias e='emacs -nw'
alias ee='sudo emacs -nw'
alias v='vim'
alias vv='sudo vim'
# Tmux
tm() { tmux -2 attach -t $1; }
tmn() { tmux -2 new -s $1 $1; }
# Misc
## mount encrypted filesystems
alias m='encMount'
## start xorg
x(){ builtin cd ~; exec xinit $@; }
## sync music to iriver
syncm() {
sudo rsync -vhru --progress /data/music/Anime/ /mnt/iriver/Music/Anime/;
}
# }}}
# {{{ Git shortcuts
alias a='git add'
alias d='git diff'
alias p='git push origin master'
alias pu='git pull origin master'
alias gpo='git push origin'
alias gpuo='git pull origin'
alias gp='git push'
alias gpu='git pull'
# Commit everything or specified path
c() {
if [[ "$1" == "-i" ]]; then
shift; git commit -s --interactive $@
else
if [[ -n "$@" ]]; then
git commit -s $@
else
git commit -s -a
fi;
fi;
}
# Git show relevant status
sa() {
git status | ack -B 999 --no-color "Untracked"
}
# }}}
# {{{ Directory navigation
# General
alias h='builtin cd'
hc() { builtin cd; clear; }
mcd() { mkdir -p "$1" && eval cd "$1"; }
# Directory up
..() { cd "../$@"; }
..2() { cd "../../$@"; }
..3() { cd "../../../$@"; }
..4() { cd "../../../../$@"; }
..5() { cd "../../../../../$@"; }
# Auto-Ls after CD
cd() { if [[ -n "$1" ]]; then builtin cd "$1" && ls;
else builtin cd && ls; fi; }
,cd() { [[ -n "$1" ]] && builtin cd "$1" || builtin cd; }
ca() { ,cd "$1"; ls -la; }
cn() { ,cd "$1"; ls -a; }
# Directory stack
di() { dirs -v; }
po() { if [[ -n "$1" ]]; then popd "$1" 1>/dev/null && ls;
else popd 1>/dev/null && ls; fi; }
ph() { pushd "$1" 1>/dev/null && ls; }
alias p+='ph +1'
alias p2='ph +2'
alias p3='ph +3'
alias p4='ph +4'
alias -- p-='ph -0'
alias -- p-1='ph -1'
alias -- p-2='ph -2'
alias -- p-3='ph -3'
alias -- p-4='ph -4'
alias -- -='cd -'
# }}}
# {{{ Fallback applications
# Fallback to grep if ack is not found
if [[ ! -x ~/bin/ack ]]; then
alias ack="grep -i --color=always"
else
alias ack="command ack -i"
fi
# Unpack programs
if [[ -x '/usr/bin/aunpack' ]]; then
alias un='aunpack'
else
alias un='tar xvf'
fi
# }}}
# {{{ Daemons
rc.d() { [[ -d /etc/rc.d ]] && sudo /etc/rc.d/$@;
[[ -d /etc/init.d ]] && sud /etc/init.d/$@; }
dr() { for d in $@; do rc.d $d restart; done; }
ds() { for d in $@; do rc.d $d start; done; }
dt() { for d in $@; do rc.d $d stop; done; }
# }}}
# Make a git package, then show the log
gi() {
gitdir=$1; shift
from=$(git --git-dir=src/$gitdir/.git rev-parse HEAD)
makepkg -fi $@
git --git-dir=src/$gitdir/.git log --stat $from..HEAD
}
## Map function for bash.
map() {
local command i rep
if [ $# -lt 2 ] || [[ ! "$@" =~ :[[:space:]] ]];then
echo "Invalid syntax." >&2; return 1
fi
until [[ $1 =~ : ]]; do
command="$command $1"; shift
done
command="$command ${1%:}"; shift
for i in "$@"; do
if [[ $command =~ \{\} ]]; then
rep="${command//\{\}/\"$i\"}"
eval "${rep//\\/\\\\}"
else
eval "${command//\\/\\\\} \"${i//\\/\\\\}\""
fi
done
}
## wrapper for tvtime
tvtime() {
sudo modprobe -a saa7134_alsa saa7134
while [[ ! -e /dev/video0 ]]; do sleep 0.5; done;
sudo /usr/bin/tvtime
cp ~/.tvtime/tvtime.xml.orig ~/.tvtime/tvtime.xml
sudo modprobe -r saa7134_alsa
}
# }}}
extract () {
until [[ -z "$1" ]]; do
echo Extracting $1 ...
if [[ -f "$1" ]] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.tar.lzma) tar --lzma xf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "Don't know how to extract '$1'" ;;
esac
else
echo "'$1' is not a valid file"
fi
shift
done
}
archive () {
FILE=$1
case $FILE in
*.tar.bz2) shift && tar cjf $FILE $* ;;
*.tar.gz) shift && tar czf $FILE $* ;;
*.tar.lzma) shift && tar --lzma cf $FILE $* ;;
*.tgz) shift && tar czf $FILE $* ;;
*.zip) shift && zip $FILE $* ;;
*.rar) shift && rar $FILE $* ;;
esac
}
# recursively 'fix' dir/file perm
fix() {
for dir in "$@"; do
find "$dir" -type d -exec chmod 755 {} \;
find "$dir" -type f -exec chmod 644 {} \;
done
}
# go to google for a definition
define() {
which lynx &>/dev/null || return 1
local lang=$(echo $LANG | cut -d '_' -f 1)
local charset=$(echo $LANG | cut -d '.' -f 2)
lynx -accept_all_cookies -dump -hiddenlinks=ignore -nonumbers -assume_charset="$charset" -display_charset="$charset" "http://www.google.com/search?hl=$lang&q=define%3A+$1&btnG=Google+Search" | grep -m 5 -C 2 -A 5 -w "*" > /tmp/define
if [ ! -s /tmp/define ]; then
echo -e "No definition found.\n"
else
echo -e "$(grep -v Search /tmp/define | sed "s/$1/\\\e[1;32m&\\\e[0m/g")\n"
fi
rm -f /tmp/define
}
# simple spellchecker, uses /usr/share/dict/words
spellcheck() {
[ -f /usr/share/dict/words ] || return 1
for word in "$@"; do
if grep -Fqx "$word" /usr/share/dict/words; then
echo -e "\e[1;32m$word\e[0m" # green
else
echo -e "\e[1;31m$word\e[0m" # red
fi
done
}
# omp load
ompload() {
curl -# -F file1=@"$1" http://omploader.org/upload|awk '/Info:|File:|Thumbnail:|BBCode:/ {
gsub(/<[^<]*?\/?>/,"");
$1 = $1;
print
}';
}
# Less Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;38;5;74m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[38;5;246m' # begin standout-mode - info box
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline