You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bashrc 14 KiB

13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
12 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
12 years ago
13 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. #!/bin/bash
  2. ##########################
  3. # system type
  4. alias ismsys=false
  5. alias iscygwin=false
  6. alias iswindows="iscygwin || ismsys"
  7. alias isdarwin=false
  8. alias islinux=false
  9. case `uname` in
  10. (MINGW32*) alias ismsys=true ;;
  11. (CYGWIN*) alias iscygwin=true ;;
  12. (Darwin*) alias isdarwin=true ;;
  13. (Linux*) alias islinux=true ;;
  14. esac
  15. ##########################################
  16. null(){
  17. "$@" >/dev/null 2>&1
  18. }
  19. __try_exec(){
  20. type $1 >/dev/null 2>&1 && "$@"
  21. }
  22. export PS1="\$(__my_prompt_function)\$ "
  23. # PROMPT_COMMAND=prompt_function
  24. if false iswindows
  25. then
  26. export PAGER='tr -d \\r | less'
  27. else
  28. export PAGER="less"
  29. fi
  30. export LESS="-iRMX"
  31. if false null type vim
  32. then
  33. export EDITOR=vim
  34. else
  35. export EDITOR=vi
  36. fi
  37. export LC_MESSAGES=C
  38. export CDPATH=".:~"
  39. export VISUAL="$EDITOR"
  40. export GIT_PAGER="$PAGER"
  41. export GIT_EDITOR="$EDITOR"
  42. null type stty && {
  43. stty stop undef # unbind C-s to stop displaying output
  44. stty erase '^h'
  45. }
  46. if iswindows; then
  47. # export TMP=/tmp
  48. # export TEMP=/tmp
  49. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  50. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  51. export USER=$USERNAME
  52. fi
  53. #######################
  54. uname -a
  55. if [ "${EMACS}" = "t" ]; then # for emacs shell
  56. true export PS1="\u@\H \d \t \w\nemacs shell\$ "
  57. elif echo "$EMACS" | grep term >/dev/null 2>&1; then # for emacs term
  58. echo "Emacs Term"
  59. fi
  60. ###################################
  61. # some aliases and functions
  62. test "$TERM" == dumb || _coloroption=" --color=always"
  63. alias ls="ls -hCF --time-style=long-iso${_coloroption}"
  64. # export GREP_OPTIONS=""
  65. alias grep="grep${_coloroption}"
  66. # alias ll="ls -l"
  67. # alias la="ls -A"
  68. # alias lla="ls -Al"
  69. # alias less=""
  70. alias vl=/usr/share/vim/vimcurrent/macros/less.sh
  71. alias em="emacs -nw"
  72. alias pstree="LANG=C pstree"
  73. alias cp="cp -v"
  74. alias mv="mv -v"
  75. alias psall="ps auxww"
  76. alias q=exit
  77. alias p="$PAGER"
  78. alias c=cat
  79. alias sudo="sudo " # use aliases through sudo
  80. alias e3=e3em
  81. alias halt="sudo halt"
  82. alias reboot="sudo reboot"
  83. alias rand="echo \$RANDOM"
  84. # alias apt-get="sudo apt-get"
  85. alias ut="ssh 6365454829@un001.ecc.u-tokyo.ac.jp"
  86. alias xunp="file-roller -h"
  87. # alias pacome="sudo \paco -D"
  88. alias pcalc="python -i -c 'from math import *' "
  89. alias py3=python3
  90. alias py2=python2
  91. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  92. alias mytime="date +%Y%m%d-%H%M%S"
  93. alias sh="ENV=$HOME/.shrc PS1=\$\ sh"
  94. # type trash >/dev/null 2>&1 && alias rm=trash
  95. alias aptin="apt-get install"
  96. alias aptsearch="apt-cache search"
  97. alias aptshow="apt-cache show"
  98. alias yt=yaourt
  99. null type pacman-color && {
  100. alias pacman=pacman-color
  101. export pacman_program=pacman-color # used by pacmatic
  102. export PACMAN=pacman-color # used by yaourt
  103. }
  104. null type pacmatic && {
  105. alias pacman="pacmatic"
  106. export PACMAN="pacmatic"
  107. }
  108. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  109. alias arch-upgrade="sudo pacman -Syu"
  110. alias port-upgrade="port selfupdate && port sync && port upgrade installed"
  111. if iscygwin; then
  112. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  113. alias cygsu="cygstart /cygwinsetup.exe"
  114. alias emacs="CYGWIN=tty emacs -nw"
  115. alias ls="ls -CFG $(test "$TERM" == dumb || echo --color=auto)"
  116. fi
  117. alias g=git
  118. if null type _git # enable programmable completion for g
  119. then
  120. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  121. || complete -o default -o nospace -F _git g
  122. fi
  123. showinfo(){
  124. echo "Japanese letters are 表示可能"
  125. __try_exec diskinfo
  126. ! isdarwin && test -n "${DISPLAY}" && {
  127. __try_exec xrandr | grep --color=never ^Screen
  128. }
  129. iswindows || __try_exec finger $USER
  130. LANG=C __try_exec id
  131. __try_exec xset q
  132. }
  133. x(){
  134. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  135. #nohup startx # >~/.backup/log/xorg.log 2>&1 &
  136. startx
  137. else
  138. echo "X cant be started! Maybe another X is already running!" 1>&2
  139. fi
  140. }
  141. export __MYGITBAREREP="${HOME}/dbx/.git-bare"
  142. git-make-local-rep(){
  143. test $# -eq 0 && {
  144. echo "specify repository name." 1>&2
  145. return 1
  146. }
  147. dir="${__MYGITBAREREP}/$1.git"
  148. cdir=$PWD
  149. if test -d "$dir"
  150. then
  151. echo "dir $dir already exist!" 1>&2
  152. else
  153. mkdir -p "$dir" && {
  154. cd "$dir" &&
  155. git init --bare --shared=all
  156. }
  157. fi
  158. cd ${cdir}
  159. }
  160. bak(){
  161. for file in "$@"
  162. do
  163. cp -v ${file} ${file}.bak
  164. done
  165. }
  166. di(){
  167. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  168. then
  169. local diffcmd=colordiff
  170. else
  171. local diffcmd=diff
  172. fi
  173. ${diffcmd} -u "$@" | ${PAGER}
  174. }
  175. throw-away(){
  176. mkdir -p ~/.backup/tb
  177. for file in "$@"
  178. do
  179. mv $file ~/.backup/tb
  180. done
  181. }
  182. mkcd(){
  183. mkdir -p $1
  184. cd $1
  185. }
  186. catclip(){
  187. if iswindows
  188. then
  189. cat /dev/clipboard | tr -d \\r
  190. else
  191. xclip -o -selection "clipboard"
  192. fi
  193. }
  194. setclip(){
  195. if iswindows
  196. then
  197. if test $# -eq 0
  198. then
  199. sed -e 's/$/\r/' | tee /dev/clipboard
  200. else
  201. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  202. fi
  203. else
  204. if test $# -eq 0
  205. then
  206. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  207. else
  208. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  209. fi
  210. fi
  211. }
  212. o(){
  213. if [ $# -eq 0 ]
  214. then
  215. local f=.
  216. else
  217. local f="$1"
  218. fi
  219. if iswindows
  220. then
  221. cmd.exe //c start "" "$f"
  222. elif isdarwin
  223. then
  224. open "$f"
  225. elif type pcmanfm >/dev/null 2>&1
  226. then
  227. LC_MESSAGES= pcmanfm "$f"
  228. else
  229. LC_MESSAGES= xdg-open "$f"
  230. fi
  231. }
  232. convmv-sjis2utf8-test(){
  233. convmv -r -f sjis -t utf8 *
  234. }
  235. convmv-sjis2utf8-notest(){
  236. convmv -r -f sjis -t utf8 * --notest
  237. }
  238. _mygitconfig(){
  239. git config --global user.name '10sr'
  240. git config --global user.email '8slashes+git@gmail.com'
  241. git config --global core.autocrlf false
  242. git config --global color.ui auto
  243. git config --global status.relativePaths false
  244. git config --global status.showUntrackedFiles normal
  245. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=short"
  246. git config --global alias.st "status -s"
  247. git config --global alias.b "branch"
  248. git config --global alias.ci "commit --verbose"
  249. git config --global alias.co "checkout"
  250. git config --global alias.cim "commit --verbose -m"
  251. git config --global alias.di "diff --color"
  252. git config --global alias.me "merge --no-ff --stat -v"
  253. git config --global alias.ls "ls-files -v --full-name"
  254. git config --global alias.sl "!sl"
  255. # git config --global alias.my-ls "ls-files | xargs ls"
  256. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  257. git config --global alias.addi "add -i"
  258. if iswindows; then
  259. git config --global core.fileMode false
  260. fi
  261. }
  262. __my_parse_svn_branch() {
  263. local LANG=C
  264. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  265. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  266. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  267. }
  268. __my_svn_ps1(){
  269. if svn status >/dev/null 2>&1
  270. then
  271. local svn_branch=$(__my_parse_svn_branch)
  272. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  273. fi
  274. }
  275. #Change ANSI Colors
  276. _chengecolors(){
  277. echo -e \
  278. "\e]P0000000" \
  279. "\e]P1cd0000" \
  280. "\e]P200cd00" \
  281. "\e]P3cdcd00" \
  282. "\e]P41e90ff" \
  283. "\e]P5cd00cd" \
  284. "\e]P600cdcd" \
  285. "\e]P7353535" \
  286. "\e]P8666666" \
  287. "\e]P9ff9999" \
  288. "\e]Pa99ff99" \
  289. "\e]Pbffff99" \
  290. "\e]Pc9999ff" \
  291. "\e]Pdff99ff" \
  292. "\e]Pe99ffff" \
  293. "\e]Pfffffff"
  294. }
  295. # printf "\e]P7353535" \
  296. _colors(){
  297. echo -e \
  298. "\e[30mBlack" \
  299. "\e[31mRed" \
  300. "\e[32mGreen" \
  301. "\e[33mYellow" \
  302. "\e[34mBlue" \
  303. "\e[35mMagenta" \
  304. "\e[36mCyan" \
  305. "\e[37mWhite"
  306. echo -e \
  307. "\e[30;1mBright Black" \
  308. "\e[31;1mBright Red" \
  309. "\e[32;1mBright Green" \
  310. "\e[33;1mBright Yellow" \
  311. "\e[34;1mBright Blue" \
  312. "\e[35;1mBright Magenta" \
  313. "\e[36;1mBright Cyan" \
  314. "\e[37;1mBright White\n" \
  315. "\e[0m"
  316. }
  317. # http://www.frexx.de/xterm-256-notes/data/colortable16.sh
  318. _install_script(){
  319. mkdir -p $HOMO/bin/
  320. for f in "$@"
  321. do
  322. bn=$(basename "$f")
  323. type ${bn} >/dev/null 2>&1 || wget "$f" -P "$HOME/bin/"
  324. chmod u+x "$HOME/bin/${bn}"
  325. done
  326. }
  327. winln(){
  328. # for windose make link (actually junction)
  329. if test $# -eq 0
  330. then
  331. {
  332. echo "usage: winln TARGET LINK_NAME"
  333. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  334. echo "About other features run 'junction'."
  335. } 1>&2
  336. return 1
  337. else
  338. junction "$2" "$1"
  339. fi
  340. }
  341. battery-status(){
  342. local dir=/sys/class/power_supply/BAT0
  343. if test -d $dir
  344. then
  345. local st=$(cat $dir/status)
  346. local full=$(cat $dir/charge_full)
  347. local now=$(cat $dir/charge_now)
  348. local rate=$(expr $now \* 100 / $full)
  349. printf "$1" "${st}:${rate}%"
  350. fi
  351. }
  352. alias bat='battery-status %s\\n'
  353. battery-status2(){
  354. local dir=/sys/class/power_supply/BAT0
  355. . $dir/uevent
  356. local rate=$(expr $POWER_SUPPLY_CHARGE_NOW \* 100 / $POWER_SUPPLY_CHARGE_FULL)
  357. echo ${POWER_SUPPLY_STATUS}:${rate}%
  358. }
  359. ip-address(){
  360. local ip=$(LANG=C ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}')
  361. test -n "$ip" && printf $1 $ip
  362. }
  363. __my_prompt_function(){ # used by PS1
  364. # remove __try_exec from function
  365. local lastreturn=$?
  366. if test "${TERM}" == dumb
  367. then
  368. local c1=
  369. local c2=
  370. local c3=
  371. local cdef=
  372. else
  373. local c1="\e[33m"
  374. local c2="\e[36m"
  375. local c3="\e[37m"
  376. local cdef="\e[0m"
  377. fi
  378. if iswindows
  379. then
  380. local pwd=$PWD
  381. local oldpwd=$OLDPWD
  382. git branch >/dev/null 2>&1 && local git="[GIT]"
  383. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  384. else
  385. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  386. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  387. local jobnum=$(jobs | wc -l)
  388. local git=$(__try_exec __git_ps1 [GIT:%s])
  389. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  390. fi
  391. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  392. if test -z "$DISPLAY"
  393. then
  394. local ip=$(ip-address [Addr:%s])
  395. local bst="/tmp/${USER}-tmp/batterystatus"
  396. test -f $bst && local battery="[Battery:$(sed -e 's`%`%%`g' $bst)]"
  397. battery-status %s >$bst &
  398. fi
  399. local tty=$(__try_exec tty | sed -e 's:/dev/::')
  400. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  401. printf " [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  402. printf "${c2}${USER}@${HOSTNAME}${cdef} ${tty} ${date} ${BASH} ${BASH_VERSION}\n"
  403. printf "shlv:${SHLVL} jobs:${jobnum} last:${lastreturn} "
  404. }
  405. # from https://wiki.archlinux.org/index.php/X_resources
  406. invader(){
  407. # ANSI color scheme script featuring Space Invaders
  408. #
  409. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  410. # Modified by lolilolicon
  411. #
  412. f=3 b=4
  413. for j in f b; do
  414. for i in {0..7}; do
  415. printf -v $j$i %b "\e[${!j}${i}m"
  416. done
  417. done
  418. bld=$'\e[1m'
  419. rst=$'\e[0m'
  420. cat << EOF
  421. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  422. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  423. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  424. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  425. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  426. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  427. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  428. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  429. $f7▌$rst
  430. $f7▌$rst
  431. $f7 ▄█▄ $rst
  432. $f7▄█████████▄$rst
  433. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  434. EOF
  435. }