bashrc
#!/bin/bash
# ~/.bashrc
#
#
# shosei
# Shell Init
[[ $PATH = *local/bin* ]] || PATH=$PATH:/usr/local/bin
[[ $- = *i* ]] || return
source bashlib
# by OS
if [[ $MACHTYPE = *darwin* ]]; then
# MAC ----------------#
qview() {
files=("$@"); i=0
while true; do
file=${files[i]}
qlmanage -p "$file" & pid=$!
read -sn1 key
kill $pid || key=q
wait $pid
case $key in
q) return ;;
p) let i-- ;;
*) let i++ ;;
esac
(( i < ${#files[@]} )) || break
(( i < 0 )) && i=0
done
} 2>/dev/null
qthumb() {
qlmanage -t "$@" & pid=$!
read -sn1
kill $pid; wait $pid
} 2>/dev/null
fi
# Applications
alias cal="cal -m -3"
alias git="nice git"
alias gsh="git stash"
alias gst="git status --short --branch"
alias gsu="git submodule update --recursive --merge"
alias gdf="git diff"
alias gdt="git difftool"
alias glo="git log"
alias gps="git push"
alias gpl="git pull"
alias gco="git checkout"
alias gci="git commit"
alias gad="git add"
alias grm="git rm"
alias gmv="git mv"
alias gtg="git tag"
alias gbr="git branch"
alias gs="git svn"
# System
s() {
fc -s "$1=$2"
}
exists wdiff && \
alias wdiff="wdiff -a"
exists less && \
alias more="less" || \
alias less="more"
alias kbg='bg; kill $!; fg'
exists ltrace && \
alias trace="ltrace -C -f -n 4 -S"
exists pcregrep && \
alias pcregrep="pcregrep --color=auto"
alias grep="grep -I --color=auto"
alias egrep="grep -E"
alias fgrep="grep -F"
alias pg="pgrep -l"
if exists pstree; then
p() {
if pstree -A >/dev/null 2>&1; then
pstree -Aahlpu
else
[[ -t 1 ]] \
&& pstree -w -g2 \
|| pstree -w -g1 | recode -fg IBM850..UTF8
fi
}
else
if ps auxf >/dev/null 2>&1; then
p() { ps auxf; }
else
p() { ps aux; }
fi
fi
alias pp="p | less"
top -u -h >/dev/null 2>&1 && \
alias top="top -S -u -stats pid,ppid,user,cpu,time,threads,state,rprvt,vprvt,faults,command"
# fink.env
test -r /sw/bin/init.sh && . /sw/bin/init.sh
# applications.startup
exists todo && todo
mysqlstart() {
sudo bash -c 'nice /opt/local/bin/mysqld_safe5 "$@" &' -- "$@"
}
mysqlstop() {
/opt/local/bin/mysqladmin5 -u root -p shutdown "$@"
}
# Filesystem
alias noproxy="http_proxy= HTTP_PROXY= https_proxy= HTTPS_PROXY= ftp_proxy= FTP_PROXY="
alias rs="nice rsync --archive --no-owner --sparse --hard-links --partial --progress"
alias rsz="rs --compress-level=9 --skip-compress=gz/zip/z/rpm/deb/iso/bz2/t[gb]z/7z/mp[34]/mov/avi/ogg/jpg/jpeg/rar/gif/png/dat"
alias port="sudo nice port"
alias mvn="nice mvn"
alias cp="nice cp -v"
alias mv="nice mv -v"
alias tree="tree -F --dirsfirst"
if ls --color >/dev/null 2>/dev/null; then
alias ls="ls -bFk --color=auto"
else
alias ls="ls -bFkG"
fi
alias ll="ls -lh"
alias l=" ll -a"
alias df="nice df -h"
# Network
alias n="netstat -np tcp"
alias mtr="mtr -t"
alias nmap="nmap -v -v -T5"
alias nmapp="nmap -P0 -A --osscan_limit"
alias pktstat="sudo pktstat -tBFT"
# shell history
shopt -s histappend
HISTCONTROL=ignoreboth
HISTSIZE=
HISTFILE=~/.bash.log
HISTFILESIZE=
HISTTIMEFORMAT='%F %T%t'
# theme.shell
shopt -s extglob
shopt -qs globstar
shopt -qs checkwinsize
shopt -qs hostcomplete
shopt -qs no_empty_cmd_completion
stty stop undef
stty -echoctl
#[[ -f /etc/bash_completion ]] && \
# source /etc/bash_completion
[[ -f ~/.share/git-svn-extensions/source.sh ]] && \
source ~/.share/git-svn-extensions/source.sh
# xterm/rxvt/screen.Terminal title
case "$TERM" in
?term*|rxvt*|gnome*|interix)
PROMPT_COMMAND='history -a; echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"' ;;
screen*)
PROMPT_COMMAND='history -a; echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\033\\"' ;;
esac
# prompt.PS1
PS1='\h\[$blue\] $(git-repo-status)\W \[$red\]${?/#0/\[$green\]}\$\[$reset\] '
if (( EUID )); then
PS1='\[$reset$bold$green\]\u@'$PS1
else
PS1='\[$reset$bold$red\]'$PS1
fi
forcePS1ToFront() {
origExitCode=$?
read r c < <(cloc)
(( c > 1 )) && printf '\n' >&2
return $origExitCode
}
# Force the prompt on a new line if the cursor isn't at the beginning of the line.
# PS1='$(forcePS1ToFront)'$PS1
# PS1='\[$reset$bold$green\]\u@\h\[$blue\] \W \[$green\]$(: "${?#0}"; printf "\[%s\]" "${_:+$save$red\r$_ $load}")\$\[$reset\] '
# shell.colors
if [[ -f /etc/DIR_COLORS.env ]]
then source "/etc/DIR_COLORS.env"
elif [[ -f /etc/DIR_COLORS ]] && exists dircolors
then eval "$(dircolors -b "/etc/DIR_COLORS")"
fi
if [[ -f ~/.dir_colors.env ]]
then source ~/.dir_colors.env
elif [[ -f ~/.dir_colors ]] && exists dircolors
then eval "$(dircolors -b ~/.dir_colors)"
fi
# X Resources.
#[ "$DISPLAY" -a -f "$HOME/.Xdefaults" ] && \
# exists xrdb && xrdb "$HOME/.Xdefaults"
# completion.shell
complete_getpw() {
local successes candidates
source ~/.getpw
for url in "${!urls[@]}"; do
[[ $url = $2* ]] || continue
successes=${urls["$url"]}
if (( successes )); then
COMPREPLY+=("$url")
fi
done
}
complete_pw() {
local successes candidates
source ~/.pw.conf
for host in "${!uses[@]}"; do
[[ $host = $2* ]] || continue
use=${uses["$host"]}
if (( use > 0 )); then
COMPREPLY+=("$host")
fi
done
}
complete -F complete_getpw getpw
complete -F complete_pw pw
complete -A command sudo
# functions
i() {
bash --rcfile <(printf '%s\n' "$(<~/.bashrc)" "HISTFILE='$HISTFILE.i'" "PS1='\[$yellow\]~\[$reset\]$PS1'" "cd; clear"); clear
}
d() {
if exists colordiff; then
colordiff -ur "$@"
elif exists diff; then
diff -ur "$@"
elif exists comm; then
comm -3 "$1" "$2"
fi | less
}
# f pattern [find-args]
f() {
local root pattern=$1; shift
find "${@:-.}" -iname "*$pattern*"
}
rerun() {
local h history histories dialogMenu=() startIndex
# Load in history entries (ignoring the last)
IFS=$'\n' read -r -d '' -a histories < <(history | tail -n "${1:-10}")
unset histories[${#histories[@]}-1]
# Build a dialog(1) menu and show it to ask for the starting index.
for h in "${!histories[@]}"; do dialogMenu+=( "$h" "${histories[h]}" ); done
startIndex=$(dialog --stdout --menu "Bash History" 0 0 0 "${dialogMenu[@]}")
# Run all history commands starting from the starting index.
for history in "${histories[@]:startIndex}"; do
eval "${history#*$'\t'}" || break
done
}
sw() {
local bak="$1~"
while [[ -e $bak ]]
do bak+='~'; done
mv -v "$1" "$bak"
mv -v "$2" "$1"
mv -v "$bak" "$2"
}
ppg() {
pat=$1; shift
p | grep -i "$@" "$pat"
}
cwatch() {
while sleep .5; do
o="$("$@")"
clear && echo "$o"
done
}
mvns() {
export PATH=/usr/local/share/soylatte16-amd64-1.0.3/bin:${PATH}
export JAVA_HOME=/usr/local/share/soylatte16-amd64-1.0.3
mvn "$@"
}
mvnroot() {
local p=$PWD c=${1:-1}
until p=${p%/*}; [[ -e "$p/pom.xml" ]] && (( --c <= 0 )); do :; done
echo "${p}${red}${PWD#$p}${reset}"
cd "$p"
}
gf() {
git-forest -a --sha "${@:---all}" | less
}
gd() {
git describe --always --dirty
}
gdm() {
emit "GIT Daemon starting with base path: $(shorten "$PWD")"
git daemon --base-path=. "$@" &
}
git-redo-move() {
(( $# == 2 )) || { emit -r "Expected two parameters; [source] [destination]."; return; }
[[ -e $2 ]] || { emit -r "$2 doesn't exist, can't redo ."; return; }
[[ ! -e $1 ]] || { emit -r "$1 exists, don't want to overwrite, redo move aborted."; return; }
mkdir -p "${1%/*}" && \mv "$2" "$1" && git mv "$1" "$2" && rmdir -p "${1%/*}"
}
git-repo-status() {
local exit=$? cwd=$PWD repo= tag
while [[ $cwd ]]; do
[[ -d $cwd/.git ]] && { repo=git; break; }
cwd=${cwd%/*}
done
case $repo in
git)
if ! tag=$(git describe --tags --exact-match 2>&-); then
tag=$(git describe --tags 2>&-)
tag=${tag%-*} # Chop off hash.
fi
printf '[%s%s%s] \n' "${cwd##*/}" "${tag:+:}" "$tag"
;;
esac
return $exit
}
portget() {
(( $# )) || { emit -r "$0 [revision] [category/portname]"; return; }
[[ -e "${2#*/}" ]] && { ask -Ny! 'Target exists, delete?' && rm -rf "${2#*/}" || return; }
svn co -r "$1" http://svn.macports.org/repository/macports/trunk/dports/"$2"
cd "${2#*/}"
}
# functions.networking
exists lft && \
lft() {
sudo lft -S "$@" | tail -n +3 | column -t
}
svnup() {
local cRev=$(svn info | awk '/^Revision:/ { print $2 }')
[[ $cRev ]] || { emit -r "Not in a repository."; return 1; }
emit "Looking for updates to r$cRev"
svn up
local nRev=$(svn info | awk '/^Revision:/ { print $2 }')
[[ $nRev = $cRev ]] && {
emit "Nothing to update."
return
}
echo
emit "Changelog $cRev -> $nRev:"
svn log -r$((cRev+1)):$nRev | while IFS= read -r line; do
[[ $line ]] || continue
[[ $line != *[^-]* ]] && { begin=1; continue; }
if (( begin )); then
read rev _ user _ d t _ <<< "$line"
echo
echo "$bold$green$user$reset - r$bold${rev#r}$reset: "$'\t'"($bold$d$reset - $bold$t$reset)"
begin=0
else
[[ $line = *:* ]] \
&& echo $'\t'"$reset- $bold$blue${line%%:*}$reset:${line#*:}" \
|| echo $'\t'"$reset- $line"
fi
done
echo
emit "To view the code diff of these updates; execute: svn diff -r$cRev:$nRev $(quote "$@")"
}
# functions.filesystem
cc() {
[[ $@ =~ ^\ *(.*)\ +([^\ ]+)\ *$ ]] && \
tar -Sc ${BASH_REMATCH[1]} | \
tar --preserve -xvC ${BASH_REMATCH[2]}
}
# functions.eval
calc() { python -c "import math; print $*"; }
c() {
local out="${TMPDIR:-/tmp}/c.$$" strict=1
trap 'rm -f "$out"' RETURN
[[ $1 = -l ]] && { strict=; shift; }
local code="
#include <unistd.h>
#include <stdio.h>
#include <math.h>
int main(int argc, const char* argv[]) {
$1;
return 0;
}"
shift
if ! gcc -x c -o "$out" -Wall ${strict:+-Werror} - <<< "$code"; then
emit -r "Compilation failed:"
cat -n <<< "$code"
return 255
else
chmod +x "$out" && "$out" "$@"
fi
}