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 23 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
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
11 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
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
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
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
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
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
11 years ago
12 years ago
11 years ago
12 years ago
11 years ago
11 years ago
12 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. __match(){
  39. # __match str1 str2
  40. # return 0 if str2 is found in str1
  41. expr "$1" : ".*$2.*" >/dev/null
  42. }
  43. if ! __match "$PATH" "$HOME/.local/bin"
  44. then
  45. PATH="${PATH}:${HOME}/.local/bin:$HOME/.local/lib/gems/bin"
  46. fi
  47. if ismsys && ! __match "$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 ! __match "$LD_LIBRARY_PATH" "$HOME/.local/lib"
  57. then
  58. export 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 -FS"
  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. __match "$TMP" "${USER}-tmp" >/dev/null || 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="exec \"$SHELL\""
  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. null type rsync && alias rs="rsync --progress --itemize-changes --compress"
  209. isdarwin && alias updatedb="LC_ALL=C updatedb"
  210. # do not use locate installed by macports
  211. isdarwin && test -x /usr/bin/locate && alias locate="/usr/bin/locate"
  212. # pad
  213. alias pad=notepad
  214. null type gedit && alias pad=gedit
  215. null type leafpad && alias pad=leafpad
  216. isdarwin && alias pad="open -e"
  217. null type wicd-curses && alias wic=wicd-curses
  218. null type wicd-cli && alias wil="wicd-cli -y -l | head"
  219. #alias wicn="wicd-cli -y -c -n"
  220. wicn(){
  221. if test $# -eq 0
  222. then
  223. local num
  224. wicd-cli -y -l | head
  225. echo -n "input num: "
  226. read num
  227. test -n "$num" && wicd-cli -y -c -n $num
  228. else
  229. wicd-cli -y -c -n $1
  230. fi
  231. }
  232. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  233. /usr/share/vim/vim73/macros/less.sh \
  234. /usr/share/vim/vim72/macros/less.sh
  235. do
  236. test -f $f && alias vl=$f && break
  237. done
  238. alias pa=pacapt
  239. null type yaourt && alias yt=yaourt
  240. null type cower && alias cower="cower --color=auto"
  241. null type pacmatic && {
  242. alias pacman="pacmatic"
  243. export PACMAN="pacmatic"
  244. }
  245. _pacman_update_mirrorlist_with_reflector(){
  246. ml=/etc/pacman.d/mirrorlist
  247. cmd="$(expr "$(grep -m 1 reflector $ml)" : '# With: *\(.*\)')"
  248. if test -z "$cmd"
  249. then
  250. cmd="reflector --verbose -l 5 --sort rate --save $ml"
  251. fi
  252. echo "Running $cmd ..." 1>&2
  253. sudo $cmd
  254. }
  255. null type reflector && test -f /etc/pacman.d/mirrorlist && \
  256. alias reflect_mirrorlist=_pacman_update_mirrorlist_with_reflector
  257. null type apt-get && {
  258. alias aupgrade="sudo apt-get autoremove --yes && \
  259. sudo apt-get update --yes && sudo apt-get upgrade --yes"
  260. alias aptin="apt-get install"
  261. alias aptsearch="apt-cache search"
  262. alias aptshow="apt-cache show"
  263. }
  264. null type port && {
  265. alias port="port -v"
  266. alias pupgrade="sudo port -v selfupdate && \
  267. { sudo port -v upgrade outdated; }"
  268. }
  269. if iscygwin; then
  270. null type windate || \
  271. alias windate="cmd.exe //c 'echo %DATE%-%TIME%'"
  272. # alias cygsu="cygstart /cygwinsetup.exe"
  273. # alias ls="ls -CFG $(iswindows || test "$TERM" = dumb || echo --color=auto)"
  274. fi
  275. g(){
  276. if test $# -eq 0 && null type git-info
  277. then
  278. git info
  279. else
  280. git "$@"
  281. fi
  282. }
  283. if null type _git && inbash
  284. then
  285. # enable programmable completion for g
  286. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  287. || complete -o default -o nospace -F _git g
  288. fi
  289. git svn --help >/dev/null 2>&1 && alias gsvn="git svn"
  290. null type gitmemo && alias m=gitmemo
  291. null type gitmemo && alias m=gitmemo
  292. alias setup.py="sudo python3 setup.py install --record files.txt"
  293. ssh(){
  294. __my_set_screen_title ssh
  295. command ssh "$@"
  296. }
  297. memo(){
  298. if test -z "$1"
  299. then
  300. $EDITOR memo.txt
  301. else
  302. $EDITOR "$1/memo.txt"
  303. fi
  304. }
  305. clk(){
  306. local tformat="%Y/%m/%d %H:%M:%S %z"
  307. cal
  308. REPLY=
  309. printf "\\r`date "+${tformat}"`"
  310. read -t 1
  311. while test $? -ne 0
  312. do
  313. printf "\\r`date "+${tformat}"`"
  314. read -t 1
  315. done
  316. }
  317. s(){
  318. if git rev-parse --git-dir >/dev/null 2>&1
  319. then
  320. echo ">> git grep -n $@" 1>&2
  321. git grep -n "$@"
  322. elif which ag >/dev/null 2>&1
  323. then
  324. echo ">> ag --pager=\"$PAGER\" $@" 1>&2
  325. ag --pager="$PAGER" "$@"
  326. elif which ack >/dev/null 2>&1
  327. then
  328. echo ">> ack --pager=\"$PAGER\" $@" 1>&2
  329. ack --pager="$PAGER" "$@"
  330. else
  331. echo \
  332. ">> find . " \
  333. "-path '*/.git' -prune -o" \
  334. "-path '*/.svn' -prune -o" \
  335. "-type f -exec grep -nH -e --color=always $@ {} +" 1>&2
  336. if test $# -eq 0
  337. then
  338. echo "No search word given." 1>&2
  339. return 1
  340. fi
  341. find . \
  342. -path '*/.git' -prune -o \
  343. -path '*/.svn' -prune -o \
  344. -type -f -exec grep -nH -e --color=always "$@" {} + \
  345. | $PAGER
  346. fi
  347. }
  348. man(){
  349. env \
  350. LESS_TERMCAP_mb=$(printf "\e[1;35m") \
  351. LESS_TERMCAP_md=$(printf "\e[1;31m") \
  352. LESS_TERMCAP_me=$(printf "\e[0m") \
  353. LESS_TERMCAP_se=$(printf "\e[0m") \
  354. LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
  355. LESS_TERMCAP_ue=$(printf "\e[0m") \
  356. LESS_TERMCAP_us=$(printf "\e[1;32m") \
  357. man "$@"
  358. }
  359. netwait(){
  360. while ! ping -c 1 -t 1 example.com
  361. do
  362. true
  363. done
  364. echo network works.
  365. }
  366. cd(){
  367. if test $# -eq 0
  368. then
  369. pushd ~/ >/dev/null
  370. elif test "$1" = -
  371. then
  372. local pwd="$PWD"
  373. command cd "$OLDPWD"
  374. pushd -n "$pwd" >/dev/null # stack last dir
  375. elif ! test -d "$1"
  376. then
  377. echo `basename ${SHELL}`: cd: "$1": No such file or directory 1>&2
  378. return 1
  379. else
  380. pushd "$1" >/dev/null
  381. fi
  382. __dirs_rm_dup "$PWD"
  383. echo "$PWD"
  384. }
  385. __dirs_rm_dup(){
  386. for d in "$@"
  387. do
  388. local next="$(__realpath --strip "$d")"
  389. for l in $(\dirs -v -l | cut -d $'\n' -f 2- | \
  390. \grep -x " *[0-9]\+ \+${next}" | \grep -o "^ *[0-9]\+ " | tac)
  391. do
  392. popd +$l -n >/dev/null
  393. done
  394. done
  395. }
  396. __realpath(){
  397. if type realpath >/dev/null 2>&1
  398. then
  399. command realpath "$@"
  400. else
  401. while ! test -d $1
  402. do
  403. shift
  404. done
  405. local d="$OLDPWD"
  406. command cd "$1"
  407. echo "$PWD"
  408. command cd "$d"
  409. fi
  410. }
  411. dh(){
  412. if test $# -eq 0
  413. then
  414. dirs -v -l
  415. else
  416. local dir="$(dirs -v -l | \grep "^ *$1 \+" | sed "s/^ *[0-9]* *//g")"
  417. cd "$dir"
  418. fi
  419. }
  420. input(){
  421. local foo
  422. stty -echo
  423. read foo
  424. stty echo
  425. echo $foo
  426. }
  427. # tmux(){
  428. # if test $# -eq 0
  429. # then
  430. # (cd ~; command tmux start;)
  431. # if command tmux has -t main
  432. # then
  433. # command tmux attach -t main
  434. # else
  435. # (cd ~; command tmux new -s main;)
  436. # fi
  437. # else
  438. # command tmux "$@"
  439. # fi
  440. # }
  441. tx(){
  442. if test $# -eq 0
  443. then
  444. tmux ls
  445. echo "tx <session> to attach."
  446. elif tmux has -t "$1"
  447. then
  448. tmux attach -t "$1"
  449. else
  450. tmux new -s "$1"
  451. fi
  452. }
  453. dt(){
  454. # dt [<name>] [<command ...>]
  455. __dtach_dir="${TMP}/dtach"
  456. install -d "${__dtach_dir}"
  457. if test -n "${__MY_DTACH}"
  458. then
  459. echo "Current session: ${__MY_DTACH}"
  460. fi
  461. if test -z "$1"
  462. then
  463. echo "Sessions:"
  464. ls "${__dtach_dir}"
  465. return 0
  466. elif test "$1" = "-h"
  467. then
  468. echo "dt: usage: dt <name> [<command ...>]" 1>&2
  469. return 1
  470. fi
  471. soc_name="${__dtach_dir}/$1"
  472. shift
  473. if test -n "$__MY_DTACH"
  474. then
  475. echo "dtach session cannot be nested." 1>&2
  476. return 1
  477. elif test -S "$soc_name"
  478. then
  479. dtach -a "$soc_name" -e ^^
  480. elif test -e "$soc_name"
  481. then
  482. echo "dt: File named $soc_name already exists."
  483. return 1
  484. elif test -z "$1"
  485. then
  486. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ sh -c "$SHELL"
  487. # echo "dt: Socket named $soc_name not exists and no command specified."
  488. # return 1
  489. else
  490. __MY_DTACH="$soc_name" dtach -c "$soc_name" -e ^^ "$@"
  491. fi
  492. }
  493. scr(){
  494. test -n "$1" && pf="${1}-"
  495. local _tformat="%Y%m%d-%H%M%S%z"
  496. local _file="${HOME}/${pf}`date +${_tformat}`.script"
  497. __MY_SCRIPT=${_file} script ${_file} "$@"
  498. }
  499. dtscr(){
  500. # dtscr <command ...>
  501. if test -z "$1"
  502. then
  503. echo "dtscr: usage: dtscr <command ...>"
  504. return 1
  505. fi
  506. local _cmdstr="`echo $@ | tr ' ' +`"
  507. local _tformat="%Y%m%d-%H%M%S%z"
  508. local _name="${pf}`date +${_tformat}`-${_cmdstr}"
  509. local _scr_file="${HOME}/${_name}.script"
  510. local _dt_dir="${TMP}/dtscr"
  511. install -d "$_dt_dir"
  512. dtach -n "${_dt_dir}/${_name}" script "${_scr_file_}" "$@"
  513. # echo $_name
  514. # echo $_file
  515. }
  516. mcrypt-stream(){
  517. test $# -eq 2 || return 1
  518. case $1 in
  519. en)
  520. mcrypt --key $2 | base64 ;;
  521. de)
  522. base64 -d | mcrypt -d --key $2 ;;
  523. esac
  524. }
  525. gpg-stream(){
  526. test $# -eq 2 || return 1
  527. case $1 in
  528. en)
  529. gpg --passphrase $2 -c --batch |base64 ;;
  530. de)
  531. base64 -d|gpg --passphrase $2 -d --batch ;;
  532. esac
  533. }
  534. dgpg(){
  535. if test "$1" = help || test -z "$2"
  536. then
  537. echo "dgpg: dgpg <en|de> <src-suffix> [<dst-suffix>]" 1>&2
  538. return
  539. fi
  540. local srcs="$2"
  541. local dsts="$3"
  542. test -z "$dsts" && dsts="${srcs}.out"
  543. local pw
  544. echo -n "dgpg pw: "
  545. read -s pw
  546. echo ""
  547. test -z "$pw" && return 1
  548. for f in *${srcs}
  549. do
  550. local d="$(basename "$f" "${srcs}")${dsts}"
  551. echo -n "Processing $f to $d..."
  552. if test -d "$f"
  553. then
  554. echo "`printf 'failed (%s is directory)' $f`"
  555. elif test -f "$d"
  556. then
  557. echo "`printf 'failed (%s is already exists)' $d`"
  558. elif <"$f" gpg-stream $1 $pw >"$d" 2>/dev/null
  559. then
  560. echo "done"
  561. else
  562. echo "failed"
  563. test -f "$d" && rm "$d"
  564. fi
  565. done
  566. }
  567. alias enst="gpg-stream en"
  568. alias dest="gpg-stream de"
  569. showinfo(){
  570. echo "Japanese letters are 表示可能"
  571. __try_exec diskinfo
  572. ! isdarwin && test -n "${DISPLAY}" && {
  573. __try_exec xrandr | \grep --color=never ^Screen
  574. }
  575. iswindows || __try_exec finger $USER
  576. LANG=C __try_exec id
  577. __try_exec xset q
  578. }
  579. x(){
  580. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  581. #mkdir -p ~/.my/log
  582. # nohup startx >~/.my/log/xorg.log 2>&1 &
  583. # exit
  584. exec startx
  585. else
  586. echo "X cant be started! Another X is already running?" 1>&2
  587. fi
  588. }
  589. bak(){
  590. for file in "$@"
  591. do
  592. cp -v ${file} ${file}.bak
  593. done
  594. }
  595. di(){
  596. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  597. then
  598. local diffcmd=colordiff
  599. else
  600. local diffcmd=diff
  601. fi
  602. ${diffcmd} -u "$@" | ${PAGER}
  603. }
  604. tb(){
  605. local tb="$HOME/.my/tb"
  606. mkdir -p "$tb"
  607. for file in "$@"
  608. do
  609. mv -t "$tb" "$file"
  610. done
  611. }
  612. mkcd(){
  613. if test -z "$1"
  614. then
  615. echo "mkcd: usage: mkcd <dir>"
  616. return 1
  617. elif test -d "$1"
  618. then
  619. echo "Dir \"$1\" already exists."
  620. else
  621. mkdir -p "$1"
  622. echo "Dir \"$1\" created."
  623. fi
  624. cd "$1"
  625. }
  626. mkcdd(){
  627. # make and change date directory
  628. _d=`date +%Y%m%d-%H%M%S`
  629. mkcd "$_d"
  630. }
  631. if test -n "$TMUX" && null type reattach-to-user-namespace
  632. then
  633. alias pbpaste="reattach-to-user-namespace pbpaste"
  634. alias pbcopy="reattach-to-user-namespace pbcopy"
  635. fi
  636. catclip(){
  637. if iswindows
  638. then
  639. cat /dev/clipboard | tr -d \\r
  640. elif isdarwin
  641. then
  642. pbpaste
  643. else
  644. xclip -o -selection "clipboard"
  645. fi
  646. }
  647. setclip(){
  648. if test $# -eq 0
  649. then
  650. exec 3<&0
  651. else
  652. exec 3<<__EOF__
  653. `cat "$@"`
  654. __EOF__
  655. fi
  656. if iswindows
  657. then
  658. 0<&3 sed -e 's/$/\r/' | tee /dev/clipboard
  659. elif isdarwin
  660. then
  661. pbcopy 0<&3
  662. else
  663. 0<&3 xclip -i -f -selection "primary" | \
  664. xclip -i -f -selection "clipboard"
  665. fi
  666. exec 3<&-
  667. }
  668. open_file(){
  669. if iswindows
  670. then
  671. cmd.exe //c start "" "$@"
  672. elif isdarwin
  673. then
  674. touch "$@"
  675. open "$@"
  676. elif islinux
  677. then
  678. touch "$@"
  679. if null type pcmanfm; then
  680. LC_MESSAGES= pcmanfm "$@"
  681. else
  682. LC_MESSAGES= xdg-open "$@" &
  683. fi
  684. else
  685. cat "$@"
  686. fi
  687. }
  688. o(){
  689. if test $# -eq 0
  690. then
  691. open_file .
  692. else
  693. for f in "$@"
  694. do
  695. open_file "$(realpath "$f")"
  696. done
  697. fi
  698. }
  699. convmv-sjis2utf8-test(){
  700. convmv -r -f sjis -t utf8 *
  701. }
  702. convmv-sjis2utf8-notest(){
  703. convmv -r -f sjis -t utf8 * --notest
  704. }
  705. winln(){
  706. # for windose make link (actually junction)
  707. if test $# -eq 0
  708. then
  709. {
  710. echo "usage: winln TARGET LINK_NAME"
  711. echo "Create a link to TARGET with the name LINK_NAME \
  712. (that is, TARGET must already exist)."
  713. echo "About other features run 'junction'."
  714. } 1>&2
  715. return 1
  716. else
  717. junction "$2" "$1"
  718. fi
  719. }
  720. #################################################
  721. ## pastebin services
  722. ## https://wiki.archlinux.org/index.php/List_of_Applications/Internet#Pastebin_clients
  723. sprunge(){
  724. # http://sprunge.us
  725. if test -z "$1"
  726. then
  727. curl -F 'sprunge=<-' http://sprunge.us
  728. else
  729. curl http://sprunge.us/$1
  730. fi
  731. }
  732. dpaste(){
  733. # http://dpaste.de
  734. if test -z "$1"
  735. then
  736. curl -F 'content=<-' https://dpaste.de/api/
  737. echo
  738. else
  739. curl https://dpaste.de/$1/raw/
  740. fi
  741. }
  742. ######################################
  743. ## Prompt
  744. __my_moc_state(){
  745. type mocp >/dev/null 2>&1 || return
  746. test "`mocp -Q %state 2>/dev/null`" = PLAY || return
  747. printf "$1" "`mocp -Q %title 2>/dev/null`"
  748. }
  749. __my_parse_svn_branch() {
  750. local LANG=C
  751. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  752. local svn_repository_root=$(svn info 2>/dev/null | \
  753. sed -ne 's#^Repository Root: ##p')
  754. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | \
  755. awk '{print $1}'
  756. }
  757. __my_svn_ps1(){
  758. if svn status >/dev/null 2>&1
  759. then
  760. local svn_branch=$(__my_parse_svn_branch)
  761. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  762. fi
  763. }
  764. __my_battery_status(){
  765. local dir=/sys/class/power_supply/BAT0
  766. if test -d $dir && test -r $dir/status && test -r $dir/charge_full && \
  767. test -r $dir/charge_now
  768. then
  769. local st=$(cat $dir/status)
  770. local full=$(cat $dir/charge_full)
  771. local now=$(cat $dir/charge_now)
  772. local rate=$(expr $now \* 100 / $full)
  773. printf "$1" "${st}:${rate}%"
  774. fi
  775. }
  776. alias bat='__my_battery_status %s\\n'
  777. ip-address(){
  778. type ip >/dev/null 2>&1 || return 1
  779. local ip=$(LANG=C ip addr show scope global | \
  780. \grep --color=never --only-matching 'inet [^ ]*' | cut -d " " -f 2)
  781. test -n "$ip" && printf $1 $ip
  782. }
  783. __my_ps1_str=""
  784. test -n "$__MY_SCRIPT" && __my_ps1_str="${__my_ps1_str}${__my_c5}SCR${__my_cdef} "
  785. test -n "$SSH_CONNECTION" && __my_ps1_str="${__my_ps1_str}${__my_c5}SSH${__my_cdef} "
  786. test -n "$__MY_DTACH" && __my_ps1_str="${__my_ps1_str}${__my_c5}DTACH${__my_cdef} "
  787. __my_ps1_scale(){
  788. local last=$?
  789. if null type stty && ! ismsys
  790. then
  791. stty size | tr -d $'\n' | tr " " x
  792. printf " "
  793. fi
  794. return $last
  795. }
  796. __my_ps1_tmux(){
  797. local last=$?
  798. null type tmux || return $last
  799. local tmuxc="$(tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  800. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  801. return $last
  802. }
  803. __my_ps1_moc(){
  804. local last=$?
  805. __my_moc_state "[MOC:%s]"
  806. return $last
  807. }
  808. for f in /usr/share/git/git-prompt.sh \
  809. /opt/local/share/git-core/git-prompt.sh \
  810. /opt/local/share/doc/git-core/contrib/completion/git-prompt.sh
  811. do
  812. test -r "$f" && . "$f" && break
  813. done
  814. GIT_PS1_SHOWDIRTYSTATE=t
  815. GIT_PS1_SHOWUPSTREAM=t
  816. __my_ps1_git(){
  817. local last=$?
  818. null type __git_ps1 || return $last
  819. null git rev-parse --git-dir >/dev/null 2>&1 || return $last
  820. __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  821. return $last
  822. }
  823. __my_ps1_ipaddr(){
  824. local last=$?
  825. ! iswindows && ip-address [Addr:%s]
  826. return $last
  827. }
  828. __my_ps1_bttry(){
  829. local last=$?
  830. local bst="${TMP}/batterystatus"
  831. if test -z "$DISPLAY" && ! iswindows
  832. then
  833. test -f $bst && local bstr="$(cat $bst)"
  834. test -n "$bstr" && ! echo $bstr | grep 100 >/dev/null 2>&1 && \
  835. echo "[Battery:$bstr]"
  836. __my_battery_status %s >$bst &
  837. fi
  838. return $last
  839. }
  840. __my_ps1_dirs(){
  841. dirs | wc -l
  842. }
  843. __my_ps1_jobs(){
  844. jobs | wc -l
  845. }
  846. if test "$TERM" != dumb
  847. then
  848. __my_c1="\[\e[0;33m\]" # color for PWD
  849. __my_c2="\[\e[0;36m\]" # color for user
  850. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  851. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  852. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;31m\]"
  853. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  854. else __my_c4="\[\e[1;32m\]" # color for ::
  855. fi
  856. __my_c5="\[\e[30;47m\]" # color for SCR
  857. __my_cdef="\[\e[0m\]"
  858. fi
  859. # export _LAST_STATUS=a
  860. # __my_export_last_status(){
  861. # local last=$?
  862. # echo $last
  863. # export _LAST_STATUS=$last
  864. # echo $_LAST_STATUS
  865. # return $last
  866. # }
  867. _ps1_bash="\
  868. ${__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\
  869. ${__my_c4}:: ${__my_cdef}l${SHLVL}n\#j\js\$? $(__my_ps1_scale) \D{%T} ${__my_ps1_str}\$ "
  870. inbash && PS1=$_ps1_bash
  871. __my_set_screen_title(){
  872. if test -n "$TMUX" && test -z "$INSIDE_EMACS"
  873. then
  874. echo -ne "\033k$1\033\\"
  875. fi
  876. }
  877. __my_set_title(){
  878. case $TERM in
  879. (rxvt*|xterm*|aterm|screen*)
  880. title="$(echo $@)"
  881. test -t 1 &&
  882. test -n "$DISPLAY" &&
  883. test -z "$EMACS" &&
  884. echo -n -e "\033]0;${title}\007"
  885. ;;
  886. esac
  887. }
  888. PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\:\${PWD};
  889. __my_set_screen_title \$(basename \"\$PWD\")/"