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.
 
 
 
 
 
 

559 lines
15 KiB

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