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 20 KiB

13 years ago
13 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 years ago
11 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
12 years ago
12 years ago
12 years ago
11 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
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 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
11 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
11 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
11 years ago
12 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 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
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
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
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
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
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
13 years ago
12 years ago
13 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
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
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
11 years ago
12 years ago
11 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. #!/bin/bash
  2. # TODO: use tput
  3. # If not running interactively, don't do anything
  4. [[ $- != *i* ]] && return
  5. ##########################################
  6. null(){
  7. "$@" >/dev/null 2>&1
  8. }
  9. __try_exec(){
  10. type $1 >/dev/null 2>&1 && "$@"
  11. }
  12. ##########################
  13. # system type
  14. alias ismsys=false
  15. alias iscygwin=false
  16. alias iswindows=false
  17. alias isdarwin=false
  18. alias islinux=false
  19. alias with_coreutils=false # for mac
  20. case `uname` in
  21. (MINGW*) alias ismsys=true ;;
  22. (CYGWIN*) alias iscygwin=true ;;
  23. (Darwin*) alias isdarwin=true ;;
  24. (Linux*) alias islinux=true ;;
  25. esac
  26. null ls --version && alias with_coreutils=true
  27. ( ismsys || iscygwin ) && alias iswindows=true
  28. alias inbash=false
  29. alias inzsh=false
  30. if test -n "$BASH_VERSION"
  31. then
  32. alias inbash=true
  33. elif test -n "$ZSH_VERSION"
  34. then
  35. alias inzsh=true
  36. fi
  37. #################################
  38. __search_string(){
  39. # __search_string str1 str2
  40. # return 0 if str2 is found in str1
  41. expr "$1" : ".*$2.*" >/dev/null
  42. }
  43. if ! __search_string "$PATH" "$HOME/.local/bin"
  44. then
  45. PATH="${PATH}:${HOME}/.local/bin:$HOME/.local/lib/gems/bin"
  46. fi
  47. if ismsys && ! __search_string "$PATH" /c/mingw/bin
  48. then
  49. PATH="$PATH:/c/mingw/bin:/c/mingw/msys/1.0/bin"
  50. fi
  51. # # it is not so good
  52. # # http://archive.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/shared-libraries.html
  53. # # http://superuser.com/questions/324613/installing-a-library-locally-in-home-directory-but-program-doesnt-recognize-it
  54. # without this ENV i cannot run tmux. another way is to use --disable-shared
  55. # when building tmux
  56. if ! __search_string "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  57. then
  58. LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/.local/lib"
  59. fi
  60. ##################################
  61. export LANG=ja_JP.UTF-8
  62. export LC_MESSAGES=C
  63. export TERMCAP="${TERMCAP}:vb="
  64. ismsys && export HOSTNAME
  65. export ENV=~/.shrc
  66. if false iswindows
  67. then
  68. export PAGER='tr -d \\r | less'
  69. else
  70. export PAGER="less"
  71. fi
  72. export LESS="-iRMX"
  73. _src_hilite_lp_path="`which src-hilite-lesspipe.sh 2>/dev/null`"
  74. if test -n "$_src_hilite_lp_path"
  75. then
  76. export LESSOPEN="| $_src_hilite_lp_path %s"
  77. fi
  78. if null type vim
  79. then
  80. export EDITOR=vim
  81. else
  82. export EDITOR=vi
  83. fi
  84. # export CDPATH=".:~"
  85. export VISUAL="$EDITOR"
  86. export GIT_PAGER="less -F"
  87. export GIT_EDITOR="$EDITOR"
  88. export GIT_MERGE_AUTOEDIT=no
  89. if test -n "$TMUX" && \
  90. echo $TERM | grep screen >/dev/null 2>&1 && \
  91. tmux display -p '#{client_termname}' | grep 256color >/dev/null 2>&1
  92. then
  93. TERM=screen-256color
  94. fi
  95. if test -z "$TMP"
  96. then
  97. if test -n "$TMPDIR"
  98. then
  99. export TMP=$TMPDIR
  100. elif test -n "$TEMP"
  101. then
  102. export TMP="$TEMP"
  103. else
  104. export TMP=/tmp
  105. fi
  106. fi
  107. export TMP="${TMP}/${USER}-tmp"
  108. export TEMP="$TMP"
  109. mkdir -p "$TMP"
  110. ! iswindows && null type stty && {
  111. stty stop undef # unbind C-s to stop displaying output
  112. # stty erase '^h'
  113. }
  114. if iswindows; then
  115. export USER=$USERNAME
  116. fi
  117. _tmux_prefs(){
  118. null type tmux || return 1
  119. tmux set -g mode-keys vi
  120. }
  121. if test -d ~/dbx
  122. then
  123. export CHIT_PATH="$HOME/dbx/.chit"
  124. fi
  125. #######################
  126. iswindows && alias tty="echo cmd.exe"
  127. type fortune >/dev/null 2>&1 && {
  128. fortune
  129. echo
  130. fortune -o
  131. echo
  132. }
  133. uname -a
  134. echo TERM $TERM $(tput colors) colors connected to $(tty), \
  135. running $BASH $BASH_VERSION
  136. if test -n "$TMUX"
  137. then
  138. tmux display -p 'Using tmux #S:#I:#W.#P, client is #{client_termname}' \
  139. 2>/dev/null
  140. echo
  141. fi
  142. ###################################
  143. # some aliases and functions
  144. ( ! with_coreutils && isdarwin ) || test "$TERM" = dumb || \
  145. _coloroption=" --color=auto"
  146. ( ! with_coreutils && isdarwin ) || iswindows || \
  147. _timeoption=" --time-style=long-iso"
  148. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  149. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  150. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  151. _timeformat_num="%Y%m%d%H%M%S"
  152. alias datenum="date +$_timeformat_num"
  153. alias ls="ls -hCF${_coloroption}${_timeoption}"
  154. if ! with_coreutils
  155. then
  156. export LSCOLORS=gxfxcxdxbxegedabagacad
  157. alias ls="ls -G"
  158. fi
  159. # export GREP_OPTIONS=""
  160. alias gr="grep -n --color=always"
  161. iswindows && alias grep="grep -n"
  162. # alias ll="ls -l"
  163. # alias la="ls -A"
  164. # alias lla="ls -Al"
  165. alias less="less -F"
  166. null type emacs && alias em="emacs -nw"
  167. null type vim && alias vi=vim
  168. alias pstree="LANG=C pstree"
  169. alias cp="cp -v"
  170. alias mv="mv -v"
  171. alias rm="rm -v"
  172. alias psaux="ps auxww"
  173. alias q=exit
  174. null type e3em && alias e3=e3em
  175. #alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  176. alias po=popd
  177. alias pu=pushd
  178. null type sudo && alias sudo="sudo " # use aliases through sudo
  179. null type sudoedit && alias sudoe="sudoedit"
  180. null type halt && alias halt="sudo halt"
  181. null type reboot && alias reboot="sudo reboot"
  182. null type dbus-send && {
  183. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  184. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  185. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  186. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  187. }
  188. alias rand="echo \$RANDOM"
  189. null type file-roller && alias xunp="file-roller -h"
  190. null type paco && alias pc="sudo \paco -D"
  191. alias pycalc="python -i -c 'from math import *' "
  192. null type python3 && alias py3=python3
  193. null type python2 && alias py2=python2
  194. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  195. # alias mytime="date +%Y%m%d-%H%M%S"
  196. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  197. # type trash >/dev/null 2>&1 && alias rm=trash
  198. null type mpg123 && alias mpg123="mpg123 -C -v --title"
  199. null type xmms2 && alias xm="xmms2"
  200. #export PLAYER="mpg123 -C -v --title"
  201. null type screen && alias screen="screen -e^z^z"
  202. #alias zcd="cd \`zenity --file-selection --directory\`"
  203. null type gtags && alias gtags="gtags --verbose"
  204. null type htags && alias htags="htags --xhtml --symbol --line-number \
  205. --frame --alphabet --verbose"
  206. null type aunpack && alias au=aunpack
  207. null type lv && alias lv="lv|less"
  208. isdarwin && alias updatedb="LC_ALL=C updatedb"
  209. # do not use locate installed by macports
  210. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  211. # pad
  212. alias pad=notepad
  213. null type gedit && alias pad=gedit
  214. null type leafpad && alias pad=leafpad
  215. isdarwin && alias pad="open -e"
  216. null type wicd-curses && alias wic=wicd-curses
  217. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  218. #alias wicn="wicd-cli -y -c -n"
  219. wicn(){
  220. if test $# -eq 0
  221. then
  222. local num
  223. wicd-cli -y -l | head
  224. echo -n "input num: "
  225. read num
  226. test -n "$num" && wicd-cli -y -c -n $num
  227. else
  228. wicd-cli -y -c -n $1
  229. fi
  230. }
  231. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  232. /usr/share/vim/vim73/macros/less.sh \
  233. /usr/share/vim/vim72/macros/less.sh
  234. do
  235. test -f $f && alias vl=$f && break
  236. done
  237. null type yaourt && alias yt=yaourt
  238. null type cower && alias cower="cower --color=auto"
  239. null type pacmatic && {
  240. alias pacman="pacmatic"
  241. export PACMAN="pacmatic"
  242. }
  243. _pacman_update_mirrorlist_with_reflector(){
  244. ml=/etc/pacman.d/mirrorlist
  245. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  246. if test -z "$cmd"
  247. then
  248. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  249. fi
  250. echo "Running $cmd ..." 1>&2
  251. sudo $cmd
  252. }
  253. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  254. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  255. null type apt-get && {
  256. alias aupgrade="sudo apt-get autoremove --yes && \
  257. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  258. alias aptin="apt-get install"
  259. alias aptsearch="apt-cache search"
  260. alias aptshow="apt-cache show"
  261. }
  262. null type port && {
  263. alias port="port -v"
  264. alias pupgrade="sudo port -v selfupdate && \
  265. { sudo port -v upgrade outdated; }"
  266. }
  267. if iscygwin; then
  268. null type windate || \
  269. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  270. # alias cygsu="cygstart /cygwinsetup.exe"
  271. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  272. fi
  273. g(){
  274. if test $# -eq 0 && null type git-info
  275. then
  276. git info
  277. else
  278. git "$@"
  279. fi
  280. }
  281. if null type _git && inbash
  282. then
  283. # enable programmable completion for g
  284. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  285. || complete -o default -o nospace -F _git g
  286. fi
  287. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  288. null type gitmemo && alias m=gitmemo
  289. null type gitmemo && alias m=gitmemo
  290. alias setup.py="sudo python3 setup.py install --record files.txt"
  291. ssh(){
  292. __my_set_screen_title ssh
  293. command ssh "$@"
  294. }
  295. clk(){
  296. local tformat="%Y/%m/%d %H:%M:%S %z"
  297. cal
  298. REPLY=
  299. printf "\\r`date "+${tformat}"`"
  300. read -t 1
  301. while test $? -ne 0
  302. do
  303. printf "\\r`date "+${tformat}"`"
  304. read -t 1
  305. done
  306. }
  307. s(){
  308. if test $# -eq 0
  309. then
  310. echo "No search word given." 1>&2
  311. return 1
  312. fi
  313. if git rev-parse --git-dir >/dev/null 2>&1
  314. then
  315. git grep -n "$@"
  316. elif which ag >/dev/null 2>&1
  317. then
  318. ag --pager="$PAGER" "$@"
  319. elif which ack >/dev/null 2>&1
  320. then
  321. ack --pager="$PAGER" "$@"
  322. else
  323. grep -nH --exclude='.svn/*' --exclude='.git/*' "$@" -r . | $PAGER
  324. # echo "No search command found. Use grep." 2>&1
  325. # return 127
  326. fi
  327. }
  328. man(){
  329. env \
  330. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  331. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  332. LESS_TERMCAP_me=$(printf "\e[0m") \
  333. LESS_TERMCAP_se=$(printf "\e[0m") \
  334. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  335. LESS_TERMCAP_ue=$(printf "\e[0m") \
  336. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  337. man "$@"
  338. }
  339. scr(){
  340. test -n "$1" && pf="${1}-"
  341. local _tformat="%Y%m%d-%H%M%S%z"
  342. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  343. SCRIPT=${_file} script ${_file} "$@"
  344. }
  345. netwait(){
  346. while ! ping -c 1 -t 1 example.com
  347. do
  348. true
  349. done
  350. echo network works.
  351. }
  352. cd(){
  353. if test $# -eq 0
  354. then
  355. pushd ~/ >/dev/null
  356. elif test "$1" = -
  357. then
  358. local pwd="$PWD"
  359. command cd "$OLDPWD"
  360. pushd -n "$pwd" >/dev/null # stack last dir
  361. elif ! test -d "$1"
  362. then
  363. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  364. return 1
  365. else
  366. pushd "$1" >/dev/null
  367. fi
  368. __dirs_rm_dup "$PWD"
  369. echo "$PWD"
  370. }
  371. __dirs_rm_dup(){
  372. for d in "$@"
  373. do
  374. local next="$(__realpath --strip "$d")"
  375. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  376. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  377. do
  378. popd +$l -n >/dev/null
  379. done
  380. done
  381. }
  382. __realpath(){
  383. if type realpath >/dev/null 2>&1
  384. then
  385. command realpath "$@"
  386. else
  387. while ! test -d $1
  388. do
  389. shift
  390. done
  391. local d="$OLDPWD"
  392. command cd "$1"
  393. echo "$PWD"
  394. command cd "$d"
  395. fi
  396. }
  397. dh(){
  398. if test $# -eq 0
  399. then
  400. dirs -v -l
  401. else
  402. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  403. cd "$dir"
  404. fi
  405. }
  406. input(){
  407. local foo
  408. stty -echo
  409. read foo
  410. stty echo
  411. echo $foo
  412. }
  413. tmux(){
  414. if test $# -eq 0
  415. then
  416. (cd ~; command tmux start;)
  417. if command tmux has -t main
  418. then
  419. command tmux attach -t main
  420. else
  421. (cd ~; command tmux new -s main;)
  422. fi
  423. else
  424. command tmux "$@"
  425. fi
  426. }
  427. mcrypt-stream(){
  428. test $# -eq 2 || return 1
  429. case $1 in
  430. en)
  431. mcrypt --key $2 | base64 ;;
  432. de)
  433. base64 -d | mcrypt -d --key $2 ;;
  434. esac
  435. }
  436. gpg-stream(){
  437. test $# -eq 2 || return 1
  438. case $1 in
  439. en)
  440. gpg --passphrase $2 -c --batch |base64 ;;
  441. de)
  442. base64 -d|gpg --passphrase $2 -d --batch ;;
  443. esac
  444. }
  445. dgpg(){
  446. if test "$1" = help || test -z "$2"
  447. then
  448. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  449. return
  450. fi
  451. local srcs="$2"
  452. local dsts="$3"
  453. test -z "$dsts" && dsts="${srcs}.out"
  454. local pw
  455. echo -n "dgpg pw: "
  456. read -s pw
  457. echo ""
  458. test -z "$pw" && return 1
  459. for f in *${srcs}
  460. do
  461. local d="$(basename "$f" "${srcs}")${dsts}"
  462. echo -n "Processing $f to $d..."
  463. if test -d "$f"
  464. then
  465. echo "`printf 'failed (%s is directory)' $f`"
  466. elif test -f "$d"
  467. then
  468. echo "`printf 'failed (%s is already exists)' $d`"
  469. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  470. then
  471. echo "done"
  472. else
  473. echo "failed"
  474. test -f "$d" && rm "$d"
  475. fi
  476. done
  477. }
  478. alias enst="gpg-stream en"
  479. alias dest="gpg-stream de"
  480. showinfo(){
  481. echo "Japanese letters are 表示可能"
  482. __try_exec diskinfo
  483. ! isdarwin && test -n "${DISPLAY}" && {
  484. __try_exec xrandr | \grep --color=never ^Screen
  485. }
  486. iswindows || __try_exec finger $USER
  487. LANG=C __try_exec id
  488. __try_exec xset q
  489. }
  490. x(){
  491. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  492. #mkdir -p ~/.my/log
  493. # nohup startx >~/.my/log/xorg.log 2>&1 &
  494. # exit
  495. exec startx
  496. else
  497. echo "X cant be started! Another X is already running?" 1>&2
  498. fi
  499. }
  500. bak(){
  501. for file in "$@"
  502. do
  503. cp -v ${file} ${file}.bak
  504. done
  505. }
  506. di(){
  507. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  508. then
  509. local diffcmd=colordiff
  510. else
  511. local diffcmd=diff
  512. fi
  513. ${diffcmd} -u "$@" | ${PAGER}
  514. }
  515. tb(){
  516. local tb="$HOME/.my/tb"
  517. mkdir -p "$tb"
  518. for file in "$@"
  519. do
  520. mv -t "$tb" "$file"
  521. done
  522. }
  523. mkcd(){
  524. if test -d "$1"
  525. then
  526. echo "Dir \"$1\" already exists."
  527. else
  528. mkdir -p "$1"
  529. echo "Dir \"$1\" created."
  530. fi
  531. cd "$1"
  532. }
  533. if test -n "$TMUX" && null type reattach-to-user-namespace
  534. then
  535. alias pbpaste="reattach-to-user-namespace pbpaste"
  536. alias pbcopy="reattach-to-user-namespace pbcopy"
  537. fi
  538. catclip(){
  539. if iswindows
  540. then
  541. cat /dev/clipboard | tr -d \\r
  542. elif isdarwin
  543. then
  544. pbpaste
  545. else
  546. xclip -o -selection "clipboard"
  547. fi
  548. }
  549. setclip(){
  550. if test $# -eq 0
  551. then
  552. exec 3<&0
  553. else
  554. exec 3<<__EOF__
  555. `cat "$@"`
  556. __EOF__
  557. fi
  558. if iswindows
  559. then
  560. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  561. elif isdarwin
  562. then
  563. pbcopy 0<&3
  564. else
  565. 0<&3 xclip -i -f -selection "primary" | \
  566. xclip -i -f -selection "clipboard"
  567. fi
  568. exec 3<&-
  569. }
  570. open_file(){
  571. if iswindows
  572. then
  573. cmd.exe //c start "" "$@"
  574. elif isdarwin
  575. then
  576. touch "$@"
  577. open "$@"
  578. elif islinux
  579. then
  580. touch "$@"
  581. if null type pcmanfm; then
  582. LC_MESSAGES= pcmanfm "$@"
  583. else
  584. LC_MESSAGES= xdg-open "$@" &
  585. fi
  586. else
  587. cat "$@"
  588. fi
  589. }
  590. o(){
  591. if test $# -eq 0
  592. then
  593. open_file .
  594. else
  595. for f in "$@"
  596. do
  597. open_file "$(realpath "$f")"
  598. done
  599. fi
  600. }
  601. convmv-sjis2utf8-test(){
  602. convmv -r -f sjis -t utf8 *
  603. }
  604. convmv-sjis2utf8-notest(){
  605. convmv -r -f sjis -t utf8 * --notest
  606. }
  607. winln(){
  608. # for windose make link (actually junction)
  609. if test $# -eq 0
  610. then
  611. {
  612. echo "usage: winln TARGET LINK_NAME"
  613. echo "Create a link to TARGET with the name LINK_NAME \
  614. (that is, TARGET must already exist)."
  615. echo "About other features run 'junction'."
  616. } 1>&2
  617. return 1
  618. else
  619. junction "$2" "$1"
  620. fi
  621. }
  622. __my_moc_state(){
  623. type mocp >/dev/null 2>&1 || return
  624. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  625. printf "$1" "`mocp -Q %title 2>/dev/null`"
  626. }
  627. __my_parse_svn_branch() {
  628. local LANG=C
  629. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  630. local svn_repository_root=$(svn info 2>/dev/null | \
  631. sed -ne 's#^Repository Root: ##p')
  632. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  633. awk '{print $1}'
  634. }
  635. __my_svn_ps1(){
  636. if svn status >/dev/null 2>&1
  637. then
  638. local svn_branch=$(__my_parse_svn_branch)
  639. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  640. fi
  641. }
  642. __my_battery_status(){
  643. local dir=/sys/class/power_supply/BAT0
  644. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  645. test -r $dir/charge_now
  646. then
  647. local st=$(cat $dir/status)
  648. local full=$(cat $dir/charge_full)
  649. local now=$(cat $dir/charge_now)
  650. local rate=$(expr $now \* 100 / $full)
  651. printf "$1" "${st}:${rate}%"
  652. fi
  653. }
  654. alias bat='__my_battery_status %s\\n'
  655. ip-address(){
  656. type ip >/dev/null 2>&1 || return 1
  657. local ip=$(LANG=C ip addr show scope global | \
  658. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  659. test -n "$ip" && printf $1 $ip
  660. }
  661. test -n "$SCRIPT" && __my_ps1_script_str="${__my_c5}SCR${__my_cdef} "
  662. test -n "$SSH_CONNECTION" && __my_ps1_ssh_str="${__my_c5}SSH${__my_cdef} "
  663. __my_ps1_scale(){
  664. local last=$?
  665. printf "${LINES}x${COLUMNS}"
  666. return $last
  667. }
  668. __my_ps1_tmux(){
  669. local last=$?
  670. null type tmux || return $last
  671. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  672. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  673. return $last
  674. }
  675. __my_ps1_moc(){
  676. local last=$?
  677. __my_moc_state "[MOC:%s]"
  678. return $last
  679. }
  680. for f in /usr/share/git/git-prompt.sh \
  681. /opt/local/share/git-core/git-prompt.sh \
  682. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  683. do
  684. test -r $f && . $f && break
  685. done
  686. GIT_PS1_SHOWDIRTYSTATE=t
  687. GIT_PS1_SHOWUPSTREAM=t
  688. __my_ps1_git(){
  689. local last=$?
  690. null type __git_ps1 || return $last
  691. null __gitdir || return $last
  692. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  693. return $last
  694. }
  695. __my_ps1_ipaddr(){
  696. local last=$?
  697. ! iswindows && ip-address [Addr:%s]
  698. return $last
  699. }
  700. __my_ps1_bttry(){
  701. local last=$?
  702. local bst="${TMP}/batterystatus"
  703. if test -z "$DISPLAY" && ! iswindows
  704. then
  705. test -f $bst && local bstr="$(cat $bst)"
  706. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  707. echo "[Battery:$bstr]"
  708. __my_battery_status %s >$bst &
  709. fi
  710. return $last
  711. }
  712. __my_ps1_dirs(){
  713. dirs | wc -l
  714. }
  715. __my_ps1_jobs(){
  716. jobs | wc -l
  717. }
  718. if test "$TERM" != dumb
  719. then
  720. __my_c1="\[\e[0;33m\]" # color for PWD
  721. __my_c2="\[\e[0;36m\]" # color for user
  722. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  723. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  724. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  725. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  726. else __my_c4="\[\e[1;32m\]" # color for ::
  727. fi
  728. __my_c5="\[\e[30;47m\]" # color for SCR
  729. __my_cdef="\[\e[0m\]"
  730. fi
  731. export _LAST_STATUS=$?
  732. __my_export_last_status(){
  733. export _LAST_STATUS=$?
  734. echo $_LAST_STATUS
  735. return $_LAST_STATUS
  736. }
  737. _ps1_bash="\
  738. ${__my_c4}:: ${__my_cdef}[${__my_c2}\u@\H${__my_cdef}:${__my_c1}\w/${__my_cdef}]\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  739. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_script_str}${__my_ps1_ssh_str}\$ "
  740. inbash && PS1=$_ps1_bash
  741. __my_set_screen_title(){
  742. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  743. then
  744. echo -ne "\033k$1\033\\"
  745. fi
  746. }
  747. __my_set_title(){
  748. case $TERM in
  749. (rxvt*|xterm*|aterm|screen*)
  750. title="$(echo $@)"
  751. test -t 1 &&
  752. test -n "$DISPLAY" &&
  753. test -z "$EMACS" &&
  754. echo -n -e "\033]0;${title}\007"
  755. ;;
  756. esac
  757. }
  758. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  759. __my_set_screen_title \$(basename \"\$PWD\")/"