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.

shrc 29 KiB

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