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