選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

598 行
15 KiB

  1. #!/bin/bash
  2. # If not running interactively, don't do anything
  3. [[ $- != *i* ]] && return
  4. ##########################################
  5. null(){
  6. "$@" >/dev/null 2>&1
  7. }
  8. __try_exec(){
  9. type $1 >/dev/null 2>&1 && "$@"
  10. }
  11. ##########################
  12. # system type
  13. alias ismsys=false
  14. alias iscygwin=false
  15. alias iswindows=false
  16. alias isdarwin=false
  17. alias islinux=false
  18. alias with_coreutils=false # for mac
  19. case `uname` in
  20. (MINGW*) alias ismsys=true ;;
  21. (CYGWIN*) alias iscygwin=true ;;
  22. (Darwin*) alias isdarwin=true ;;
  23. (Linux*) alias islinux=true ;;
  24. esac
  25. null ls --version && alias with_coreutils=true
  26. ( ismsys || iscygwin ) && alias iswindows=true
  27. #################################
  28. export PS1 # PS1 is defined later
  29. # PROMPT_COMMAND=prompt_function
  30. if false iswindows
  31. then
  32. export PAGER='tr -d \\r | less'
  33. else
  34. export PAGER="less"
  35. fi
  36. export LESS="-iRMXF"
  37. if null type vim
  38. then
  39. export EDITOR=vim
  40. else
  41. export EDITOR=vi
  42. fi
  43. export LANG=ja_JP.UTF-8
  44. export LC_MESSAGES=C
  45. # export CDPATH=".:~"
  46. export VISUAL="$EDITOR"
  47. export GIT_PAGER="$PAGER"
  48. export GIT_EDITOR="$EDITOR"
  49. echo "$TERM" | grep '^screen' >/dev/null 2>&1 || export TERM_ORIG=$TERM
  50. test "$TERM" = screen && test "$TERM_ORIG" = xterm-256color && TERM=screen-256color
  51. test -z "$TMP" && export TMP=/tmp/${USER}-tmp
  52. mkdir -p "$TMP"
  53. ! iswindows && null type stty && {
  54. stty stop undef # unbind C-s to stop displaying output
  55. # stty erase '^h'
  56. }
  57. if iswindows; then
  58. # export PS1=" \[\e[32m\]\u@\H \[\e[33m\]\w\[\e[0m\] \d \t\n\s \# \j \$ "
  59. # export PS1=" [\[\e[33m\]\w\[\e[0m\]]\n\[\e[32m\]\u@\H\[\e[0m\] \d \t \s.\v\nhist:\# jobs:\j \$ "
  60. export USER=$USERNAME
  61. fi
  62. #######################
  63. iswindows && alias tty="echo cmd.exe"
  64. type fortune >/dev/null 2>&1 && {
  65. echo
  66. fortune
  67. echo
  68. fortune -o
  69. echo
  70. }
  71. uname -a
  72. echo TERM $TERM $(tput colors) colors connected to $(tty), running $BASH $BASH_VERSION
  73. echo
  74. ###################################
  75. # some aliases and functions
  76. ( ! with_coreutils && isdarwin ) || test "$TERM" == dumb || _coloroption=" --color=auto"
  77. ( ! with_coreutils && isdarwin ) || iswindows || _timeoption=" --time-style=long-iso"
  78. ( ! with_coreutils && isdarwin ) || _hideoption=" --hide=[A-Z]*" # do not use
  79. _timeformat_iso="%Y-%m-%dT%H:%M:%S%z"
  80. _timeformat_rfc2822="%a, %d %b %Y %T %z"
  81. alias ls="ls -hCF${_coloroption}${_timeoption}"
  82. # export GREP_OPTIONS=""
  83. alias grep="grep -n${_coloroption}"
  84. iswindows && alias grep="grep -n"
  85. # alias ll="ls -l"
  86. # alias la="ls -A"
  87. # alias lla="ls -Al"
  88. # alias less=""
  89. alias em="emacs -nw"
  90. null type vim && alias vi=vim
  91. alias pstree="LANG=C pstree"
  92. alias cp="cp -v"
  93. alias mv="mv -v"
  94. alias psaux="ps auxww"
  95. alias q=exit
  96. alias e3=e3em
  97. alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  98. alias po=popd
  99. alias pu=pushd
  100. alias sudo="sudo " # use aliases through sudo
  101. alias halt="sudo halt"
  102. alias reboot="sudo reboot"
  103. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  104. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  105. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  106. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  107. alias rand="echo \$RANDOM"
  108. alias xunp="file-roller -h"
  109. alias pc="sudo \paco -D"
  110. alias pycalc="python -i -c 'from math import *' "
  111. alias py3=python3
  112. alias py2=python2
  113. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  114. # alias mytime="date +%Y%m%d-%H%M%S"
  115. alias sh="ENV=$HOME/.shrc PS1=\$\ PROMPT_COMMAND="" sh"
  116. # type trash >/dev/null 2>&1 && alias rm=trash
  117. alias mpg123="mpg123 -C -v --title"
  118. export PLAYER="mpg123 -C -v --title"
  119. alias screen="screen -e^z^z"
  120. alias zcd="cd \`zenity --file-selection --directory\`"
  121. alias pad=notepad
  122. null type gedit && alias pad=gedit
  123. null type leafpad && alias pad=leafpad
  124. alias wic=wicd-curses
  125. alias wil="wicd-cli -y -l | head"
  126. #alias wicn="wicd-cli -y -c -n"
  127. wicn(){
  128. if test $# -eq 0
  129. then
  130. local num
  131. wicd-cli -y -l | head
  132. echo -n "input num: "
  133. read num
  134. test -n "$num" && wicd-cli -y -c -n $num
  135. else
  136. wicd-cli -y -c -n $1
  137. fi
  138. }
  139. alias aptin="apt-get install"
  140. alias aptsearch="apt-cache search"
  141. alias aptshow="apt-cache show"
  142. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  143. /usr/share/vim/vim73/macros/less.sh
  144. do
  145. test -f $f && alias vl=$f && break
  146. done
  147. alias yt=yaourt
  148. null type pacman-color && {
  149. alias pacman=pacman-color
  150. export pacman_program=pacman-color # used by pacmatic
  151. export PACMAN=pacman-color # used by yaourt
  152. }
  153. null type pacmatic && {
  154. alias pacman="pacmatic"
  155. export PACMAN="pacmatic"
  156. }
  157. null type apt-get && \
  158. alias aupgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  159. null type port && \
  160. alias pupgrade="sudo port selfupdate && sudo port upgrade outdated && sudo port uninstall leaves"
  161. if iscygwin; then
  162. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  163. alias cygsu="cygstart /cygwinsetup.exe"
  164. alias emacs="CYGWIN=tty emacs -nw"
  165. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  166. fi
  167. alias g=git
  168. if null type _git # enable programmable completion for g
  169. then
  170. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  171. || complete -o default -o nospace -F _git g
  172. fi
  173. input(){
  174. local foo
  175. stty -echo
  176. read foo
  177. stty echo
  178. echo $foo
  179. }
  180. tmux(){
  181. if test $# -eq 0
  182. then
  183. if command tmux has -t main
  184. then
  185. command tmux attach -t main
  186. else
  187. command tmux new -s main
  188. fi
  189. else
  190. command tmux "$@"
  191. fi
  192. }
  193. mcrypt-stream(){
  194. test $# -eq 2 || return 1
  195. case $1 in
  196. en)
  197. mcrypt --key $2 | base64 ;;
  198. de)
  199. base64 -d | mcrypt -d --key $2 ;;
  200. esac
  201. }
  202. gpg-stream(){
  203. test $# -eq 2 || return 1
  204. case $1 in
  205. en)
  206. gpg --passphrase $2 -c --batch |base64 ;;
  207. de)
  208. base64 -d|gpg --passphrase $2 -d --batch ;;
  209. esac
  210. }
  211. alias enst="gpg-stream en"
  212. alias dest="gpg-stream de"
  213. showinfo(){
  214. echo "Japanese letters are 表示可能"
  215. __try_exec diskinfo
  216. ! isdarwin && test -n "${DISPLAY}" && {
  217. __try_exec xrandr | \grep --color=never ^Screen
  218. }
  219. iswindows || __try_exec finger $USER
  220. LANG=C __try_exec id
  221. __try_exec xset q
  222. }
  223. x(){
  224. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  225. #mkdir -p ~/.my/log
  226. # nohup startx >~/.my/log/xorg.log 2>&1 &
  227. # exit
  228. exec startx
  229. else
  230. echo "X cant be started! Maybe another X is already running or something." 1>&2
  231. fi
  232. }
  233. bak(){
  234. for file in "$@"
  235. do
  236. cp -v ${file} ${file}.bak
  237. done
  238. }
  239. di(){
  240. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  241. then
  242. local diffcmd=colordiff
  243. else
  244. local diffcmd=diff
  245. fi
  246. ${diffcmd} -u "$@" | ${PAGER}
  247. }
  248. tb(){
  249. local tb=~/.my/tb
  250. mkdir -p $tb
  251. for file in "$@"
  252. do
  253. mv $file $tb
  254. done
  255. }
  256. mkcd(){
  257. mkdir -p $1
  258. cd $1
  259. }
  260. catclip(){
  261. if iswindows
  262. then
  263. cat /dev/clipboard | tr -d \\r
  264. elif isdarwin
  265. then
  266. pbpaste
  267. else
  268. xclip -o -selection "clipboard"
  269. fi
  270. }
  271. setclip(){
  272. if iswindows
  273. then
  274. if test $# -eq 0
  275. then
  276. sed -e 's/$/\r/' | tee /dev/clipboard
  277. else
  278. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  279. fi
  280. elif isdarwin
  281. then
  282. if test $# -eq 0
  283. then
  284. pbcopy
  285. else
  286. cat "$@" | pbcopy
  287. fi
  288. else
  289. if test $# -eq 0
  290. then
  291. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  292. else
  293. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  294. fi
  295. fi
  296. }
  297. open_file(){
  298. if iswindows
  299. then
  300. cmd.exe //c start "" "$@"
  301. elif isdarwin
  302. then
  303. touch "$@"
  304. open "$@"
  305. elif islinux
  306. then
  307. if null type pcmanfm; then
  308. LC_MESSAGES= pcmanfm "$@"
  309. else
  310. LC_MESSAGES= xdg-open "$@" &
  311. fi
  312. else
  313. cat "$@"
  314. fi
  315. }
  316. o(){
  317. if test $# -eq 0
  318. then
  319. open_file .
  320. else
  321. for f in "$@"
  322. do
  323. open_file "$f"
  324. done
  325. fi
  326. }
  327. convmv-sjis2utf8-test(){
  328. convmv -r -f sjis -t utf8 *
  329. }
  330. convmv-sjis2utf8-notest(){
  331. convmv -r -f sjis -t utf8 * --notest
  332. }
  333. __my_moc_state(){
  334. type mocp >/dev/null 2>&1 || return
  335. test "`mocp -Q %state 2>/dev/null`" == PLAY || return
  336. printf "$1" "`mocp -Q %title 2>/dev/null`"
  337. }
  338. __my_parse_svn_branch() {
  339. local LANG=C
  340. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  341. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  342. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  343. }
  344. __my_svn_ps1(){
  345. if svn status >/dev/null 2>&1
  346. then
  347. local svn_branch=$(__my_parse_svn_branch)
  348. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  349. fi
  350. }
  351. #Change ANSI Colors
  352. _chengecolors(){
  353. echo -e \
  354. "\e]P0000000" \
  355. "\e]P1cd0000" \
  356. "\e]P200cd00" \
  357. "\e]P3cdcd00" \
  358. "\e]P41e90ff" \
  359. "\e]P5cd00cd" \
  360. "\e]P600cdcd" \
  361. "\e]P7353535" \
  362. "\e]P8666666" \
  363. "\e]P9ff9999" \
  364. "\e]Pa99ff99" \
  365. "\e]Pbffff99" \
  366. "\e]Pc9999ff" \
  367. "\e]Pdff99ff" \
  368. "\e]Pe99ffff" \
  369. "\e]Pfffffff"
  370. }
  371. # printf "\e]P7353535" \
  372. _colors(){
  373. echo -e \
  374. "\e[30mBlack" \
  375. "\e[31mRed" \
  376. "\e[32mGreen" \
  377. "\e[33mYellow" \
  378. "\e[34mBlue" \
  379. "\e[35mMagenta" \
  380. "\e[36mCyan" \
  381. "\e[37mWhite"
  382. echo -e \
  383. "\e[30;1mBright Black" \
  384. "\e[31;1mBright Red" \
  385. "\e[32;1mBright Green" \
  386. "\e[33;1mBright Yellow" \
  387. "\e[34;1mBright Blue" \
  388. "\e[35;1mBright Magenta" \
  389. "\e[36;1mBright Cyan" \
  390. "\e[37;1mBright White\n" \
  391. "\e[0m"
  392. }
  393. winln(){
  394. # for windose make link (actually junction)
  395. if test $# -eq 0
  396. then
  397. {
  398. echo "usage: winln TARGET LINK_NAME"
  399. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  400. echo "About other features run 'junction'."
  401. } 1>&2
  402. return 1
  403. else
  404. junction "$2" "$1"
  405. fi
  406. }
  407. __my_battery_status(){
  408. local dir=/sys/class/power_supply/BAT0
  409. if test -d $dir
  410. then
  411. local st=$(cat $dir/status)
  412. local full=$(cat $dir/charge_full)
  413. local now=$(cat $dir/charge_now)
  414. local rate=$(expr $now \* 100 / $full)
  415. printf "$1" "${st}:${rate}%"
  416. fi
  417. }
  418. alias bat='__my_battery_status %s\\n'
  419. ip-address(){
  420. test type ifconfig >/dev/null 2>&1 || return 1
  421. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  422. test -n "$ip" && printf $1 $ip
  423. }
  424. __my_ps1_scale(){
  425. local last=$?
  426. echo "[LC:${LINES}x${COLUMNS}]"
  427. return $last
  428. }
  429. __my_ps1_tmux(){
  430. local last=$?
  431. local tmuxc="$(__try_exec tmux display -p '#S:#I:#W.#P' 2>/dev/null)"
  432. test -n "$TMUX" && echo "[TMUX:$tmuxc]"
  433. return $last
  434. }
  435. __my_ps1_moc(){
  436. local last=$?
  437. __my_moc_state "[MOC:%s]"
  438. return $last
  439. }
  440. test -r /usr/share/git/git-prompt.sh && . /usr/share/git/git-prompt.sh
  441. __my_ps1_git(){
  442. local last=$?
  443. null type __git_ps1 || return $last
  444. __try_exec __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  445. return $last
  446. }
  447. __my_ps1_ipaddr(){
  448. local last=$?
  449. test -z "$DISPLAY" && ! iswindows && ip-address [Addr:%s]
  450. return $last
  451. }
  452. __my_ps1_bttry(){
  453. local last=$?
  454. local bst="${TMP}/batterystatus"
  455. if test -z "$DISPLAY" && ! iswindows
  456. then
  457. test -f $bst && local bstr="$(cat $bst)"
  458. test -n "$bstr" && echo "[Battery:$bstr]"
  459. __my_battery_status %s >$bst &
  460. fi
  461. return $last
  462. }
  463. __my_ps1_dirs(){
  464. dirs | wc -l
  465. }
  466. __my_ps1_jobs(){
  467. jobs | wc -l
  468. }
  469. if test "$TERM" != dumb
  470. then
  471. __my_c1="\[\e[1;31m\]" # color for PWD
  472. __my_c2="\[\e[0;36m\]" # color for user
  473. __my_c3="\[\e[1;30m\]" # color for OLDPWD
  474. if test "`hostname`" = arch-aspireone; then __my_c4="\[\e[1;34m\]"
  475. elif test "`hostname`" = darwin-mba.local; then __my_c4="\[\e[1;33m\]"
  476. elif test "`hostname`" = newkiwi; then __my_c4="\[\e[1;35m\]"
  477. else __my_c4="\[\e[1;32m\]" # color for ::
  478. fi
  479. __my_cdef="\[\e[0m\]"
  480. fi
  481. _PS1="\
  482. ${__my_c4}:: ${__my_cdef}[${__my_c1}\w/${__my_cdef}<${__my_c3}\${OLDPWD}${__my_cdef}]\$(__my_ps1_scale)\$(__my_ps1_tmux)\$(__my_ps1_git)\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\$(__my_ps1_moc)\n\
  483. ${__my_c4}:: ${__my_c2}\u@\H${__my_cdef} \D{%a, %d %b %Y %T %z} ${SHELL} \V\n\
  484. ${__my_c4}:: ${__my_cdef}shlv:${SHLVL} cnum:\# jobs:\j last:\$? \$ "
  485. PS1=$_PS1
  486. __my_set_title(){
  487. title="$(echo $@)"
  488. case $TERM in
  489. (rxvt*|xterm*|aterm|screen*)
  490. test -t 1 &&
  491. test -n "$DISPLAY" &&
  492. test -z "$EMACS" &&
  493. echo -n -e "\033]0;${title}\007"
  494. ;;
  495. esac
  496. }
  497. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD}"
  498. # copied from https://wiki.archlinux.org/index.php/X_resources
  499. invader(){
  500. # ANSI color scheme script featuring Space Invaders
  501. #
  502. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  503. # Modified by lolilolicon
  504. #
  505. f=3 b=4
  506. for j in f b; do
  507. for i in {0..7}; do
  508. printf -v $j$i %b "\e[${!j}${i}m"
  509. done
  510. done
  511. bld=$'\e[1m'
  512. rst=$'\e[0m'
  513. cat << EOF
  514. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  515. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  516. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  517. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  518. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  519. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  520. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  521. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  522. $f7▌$rst
  523. $f7▌$rst
  524. $f7 ▄█▄ $rst
  525. $f7▄█████████▄$rst
  526. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  527. EOF
  528. }
  529. #/etc/lsb-release