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