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
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
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
12 years ago
13 years ago
12 years ago
13 years ago
12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 e3=e3em
  80. alias sudo="sudo " # use aliases through sudo
  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. if iswindows; then
  213. alias _open_file='cmd.exe //c start ""'
  214. elif isdarwin; then
  215. alias _open_file=open
  216. elif islinux; then
  217. if null type pcmanfm; then
  218. alias _open_file="LC_MESSAGES= pcmanfm"
  219. else
  220. alias _open_file="LC_MESSAGES= xdg-open"
  221. fi
  222. else
  223. alias _open_file=cat
  224. fi
  225. o(){
  226. if test $# -eq 0
  227. then
  228. _open_file . &
  229. else
  230. for f in "$@"
  231. do
  232. if test -d $f
  233. then
  234. _open_file $f &
  235. else
  236. _open_file $f
  237. fi
  238. done
  239. fi
  240. }
  241. convmv-sjis2utf8-test(){
  242. convmv -r -f sjis -t utf8 *
  243. }
  244. convmv-sjis2utf8-notest(){
  245. convmv -r -f sjis -t utf8 * --notest
  246. }
  247. _mygitconfig(){
  248. git config --global user.name '10sr'
  249. git config --global user.email '8slashes+git@gmail.com'
  250. git config --global core.autocrlf false
  251. git config --global color.ui auto
  252. git config --global status.relativePaths false
  253. git config --global status.showUntrackedFiles normal
  254. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=short"
  255. git config --global alias.st "status -s"
  256. git config --global alias.b "branch"
  257. git config --global alias.ci "commit --verbose"
  258. git config --global alias.co "checkout"
  259. git config --global alias.cim "commit --verbose -m"
  260. git config --global alias.di "diff --color"
  261. git config --global alias.me "merge --no-ff --stat -v"
  262. git config --global alias.ls "ls-files -v --full-name"
  263. git config --global alias.sl "!sl"
  264. # git config --global alias.my-ls "ls-files | xargs ls"
  265. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  266. git config --global alias.addi "add -i"
  267. if iswindows; then
  268. git config --global core.fileMode false
  269. fi
  270. }
  271. __my_parse_svn_branch() {
  272. local LANG=C
  273. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  274. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  275. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  276. }
  277. __my_svn_ps1(){
  278. if svn status >/dev/null 2>&1
  279. then
  280. local svn_branch=$(__my_parse_svn_branch)
  281. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  282. fi
  283. }
  284. #Change ANSI Colors
  285. _chengecolors(){
  286. echo -e \
  287. "\e]P0000000" \
  288. "\e]P1cd0000" \
  289. "\e]P200cd00" \
  290. "\e]P3cdcd00" \
  291. "\e]P41e90ff" \
  292. "\e]P5cd00cd" \
  293. "\e]P600cdcd" \
  294. "\e]P7353535" \
  295. "\e]P8666666" \
  296. "\e]P9ff9999" \
  297. "\e]Pa99ff99" \
  298. "\e]Pbffff99" \
  299. "\e]Pc9999ff" \
  300. "\e]Pdff99ff" \
  301. "\e]Pe99ffff" \
  302. "\e]Pfffffff"
  303. }
  304. # printf "\e]P7353535" \
  305. _colors(){
  306. echo -e \
  307. "\e[30mBlack" \
  308. "\e[31mRed" \
  309. "\e[32mGreen" \
  310. "\e[33mYellow" \
  311. "\e[34mBlue" \
  312. "\e[35mMagenta" \
  313. "\e[36mCyan" \
  314. "\e[37mWhite"
  315. echo -e \
  316. "\e[30;1mBright Black" \
  317. "\e[31;1mBright Red" \
  318. "\e[32;1mBright Green" \
  319. "\e[33;1mBright Yellow" \
  320. "\e[34;1mBright Blue" \
  321. "\e[35;1mBright Magenta" \
  322. "\e[36;1mBright Cyan" \
  323. "\e[37;1mBright White\n" \
  324. "\e[0m"
  325. }
  326. # http://www.frexx.de/xterm-256-notes/data/colortable16.sh
  327. _install_script(){
  328. mkdir -p $HOMO/bin/
  329. for f in "$@"
  330. do
  331. bn=$(basename "$f")
  332. type ${bn} >/dev/null 2>&1 || wget "$f" -P "$HOME/bin/"
  333. chmod u+x "$HOME/bin/${bn}"
  334. done
  335. }
  336. winln(){
  337. # for windose make link (actually junction)
  338. if test $# -eq 0
  339. then
  340. {
  341. echo "usage: winln TARGET LINK_NAME"
  342. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  343. echo "About other features run 'junction'."
  344. } 1>&2
  345. return 1
  346. else
  347. junction "$2" "$1"
  348. fi
  349. }
  350. battery-status(){
  351. local dir=/sys/class/power_supply/BAT0
  352. if test -d $dir
  353. then
  354. local st=$(cat $dir/status)
  355. local full=$(cat $dir/charge_full)
  356. local now=$(cat $dir/charge_now)
  357. local rate=$(expr $now \* 100 / $full)
  358. printf "$1" "${st}:${rate}%"
  359. fi
  360. }
  361. alias bat='battery-status %s\\n'
  362. battery-status2(){
  363. local dir=/sys/class/power_supply/BAT0
  364. . $dir/uevent
  365. local rate=$(expr $POWER_SUPPLY_CHARGE_NOW \* 100 / $POWER_SUPPLY_CHARGE_FULL)
  366. echo ${POWER_SUPPLY_STATUS}:${rate}%
  367. }
  368. ip-address(){
  369. local ip=$(LANG=C ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}')
  370. test -n "$ip" && printf $1 $ip
  371. }
  372. __my_prompt_function(){ # used by PS1
  373. # remove __try_exec from function
  374. local lastreturn=$?
  375. if test "${TERM}" == dumb
  376. then
  377. local c1=
  378. local c2=
  379. local c3=
  380. local cdef=
  381. else
  382. local c1="\e[33m"
  383. local c2="\e[36m"
  384. local c3="\e[37m"
  385. local cdef="\e[0m"
  386. fi
  387. if iswindows
  388. then
  389. local pwd=$PWD
  390. local oldpwd=$OLDPWD
  391. git branch >/dev/null 2>&1 && local git="[GIT]"
  392. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  393. else
  394. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  395. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  396. local jobnum=$(jobs | wc -l)
  397. local git=$(__try_exec __git_ps1 [GIT:%s])
  398. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  399. fi
  400. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  401. if test -z "$DISPLAY"
  402. then
  403. local ip=$(ip-address [Addr:%s])
  404. local bst="/tmp/${USER}-tmp/batterystatus"
  405. test -f $bst && local battery="[Battery:$(sed -e 's`%`%%`g' $bst)]"
  406. battery-status %s >$bst &
  407. fi
  408. local tty=$(__try_exec tty | sed -e 's:/dev/::')
  409. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  410. printf " [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  411. printf "${c2}${USER}@${HOSTNAME}${cdef} ${tty} ${date} ${BASH} ${BASH_VERSION}\n"
  412. printf "shlv:${SHLVL} jobs:${jobnum} last:${lastreturn} "
  413. }
  414. # from https://wiki.archlinux.org/index.php/X_resources
  415. invader(){
  416. # ANSI color scheme script featuring Space Invaders
  417. #
  418. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  419. # Modified by lolilolicon
  420. #
  421. f=3 b=4
  422. for j in f b; do
  423. for i in {0..7}; do
  424. printf -v $j$i %b "\e[${!j}${i}m"
  425. done
  426. done
  427. bld=$'\e[1m'
  428. rst=$'\e[0m'
  429. cat << EOF
  430. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  431. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  432. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  433. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  434. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  435. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  436. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  437. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  438. $f7▌$rst
  439. $f7▌$rst
  440. $f7 ▄█▄ $rst
  441. $f7▄█████████▄$rst
  442. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  443. EOF
  444. }