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

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