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.
 
 
 
 
 
 

542 line
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. mcrypt-stream(){
  140. test $# -eq 2 || return 1
  141. case $1 in
  142. en)
  143. mcrypt --key $2 | base64 ;;
  144. de)
  145. base64 -d | mcrypt -d --key $2 ;;
  146. esac
  147. }
  148. gpg-stream(){
  149. test $# -eq 2 || return 1
  150. case $1 in
  151. en)
  152. gpg --passphrase $2 -c --batch |base64 ;;
  153. de)
  154. base64 -d|gpg --passphrase $2 -d --batch ;;
  155. esac
  156. }
  157. alias enst="gpg-stream en"
  158. alias dest="gpg-stream de"
  159. showinfo(){
  160. echo "Japanese letters are 表示可能"
  161. __try_exec diskinfo
  162. ! isdarwin && test -n "${DISPLAY}" && {
  163. __try_exec xrandr | \grep --color=never ^Screen
  164. }
  165. iswindows || __try_exec finger $USER
  166. LANG=C __try_exec id
  167. __try_exec xset q
  168. }
  169. x(){
  170. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  171. #mkdir -p ~/.my/log
  172. # nohup startx >~/.my/log/xorg.log 2>&1 &
  173. # exit
  174. exec startx
  175. else
  176. echo "X cant be started! Maybe another X is already running or something." 1>&2
  177. fi
  178. }
  179. bak(){
  180. for file in "$@"
  181. do
  182. cp -v ${file} ${file}.bak
  183. done
  184. }
  185. di(){
  186. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  187. then
  188. local diffcmd=colordiff
  189. else
  190. local diffcmd=diff
  191. fi
  192. ${diffcmd} -u "$@" | ${PAGER}
  193. }
  194. throw-away(){
  195. mkdir -p ~/.my/tb
  196. for file in "$@"
  197. do
  198. mv $file ~/.my/tb
  199. done
  200. }
  201. mkcd(){
  202. mkdir -p $1
  203. cd $1
  204. }
  205. catclip(){
  206. if iswindows
  207. then
  208. cat /dev/clipboard | tr -d \\r
  209. else
  210. xclip -o -selection "clipboard"
  211. fi
  212. }
  213. setclip(){
  214. if iswindows
  215. then
  216. if test $# -eq 0
  217. then
  218. sed -e 's/$/\r/' | tee /dev/clipboard
  219. else
  220. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  221. fi
  222. else
  223. if test $# -eq 0
  224. then
  225. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  226. else
  227. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  228. fi
  229. fi
  230. }
  231. if iswindows; then
  232. alias _open_file='cmd.exe //c start ""'
  233. elif isdarwin; then
  234. alias _open_file=open
  235. elif islinux; then
  236. if null type pcmanfm; then
  237. alias _open_file="LC_MESSAGES= pcmanfm"
  238. else
  239. alias _open_file="LC_MESSAGES= xdg-open"
  240. fi
  241. else
  242. alias _open_file=cat
  243. fi
  244. o(){
  245. if test $# -eq 0
  246. then
  247. _open_file . >/dev/null 2>&1 &
  248. else
  249. for f in "$@"
  250. do
  251. if test -d $f
  252. then
  253. _open_file $f >/dev/null 2>&1 &
  254. else
  255. _open_file $f >/dev/null 2>&1 &
  256. fi
  257. done
  258. fi
  259. }
  260. convmv-sjis2utf8-test(){
  261. convmv -r -f sjis -t utf8 *
  262. }
  263. convmv-sjis2utf8-notest(){
  264. convmv -r -f sjis -t utf8 * --notest
  265. }
  266. _my_git_config(){
  267. git config --global user.name '10sr'
  268. git config --global user.email '8slashes+git@gmail.com'
  269. git config --global core.autocrlf false
  270. git config --global core.excludesfile '~/.gitignore'
  271. git config --global color.ui auto
  272. git config --global status.relativePaths false
  273. git config --global status.showUntrackedFiles normal
  274. git config --global log.date iso
  275. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=iso"
  276. git config --global alias.st "status -s -b"
  277. git config --global alias.b "branch"
  278. git config --global alias.ci "commit --verbose"
  279. git config --global alias.co "checkout"
  280. git config --global alias.cim "commit --verbose -m"
  281. git config --global alias.di "diff --color"
  282. git config --global alias.me "merge --no-ff --stat -v"
  283. git config --global alias.ls "ls-files -v --full-name"
  284. git config --global alias.sl "!sl"
  285. # git config --global alias.my-ls "ls-files | xargs ls"
  286. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  287. git config --global alias.addi "add -i"
  288. if iswindows; then
  289. git config --global core.fileMode false
  290. fi
  291. }
  292. __my_parse_svn_branch() {
  293. local LANG=C
  294. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  295. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  296. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  297. }
  298. __my_svn_ps1(){
  299. if svn status >/dev/null 2>&1
  300. then
  301. local svn_branch=$(__my_parse_svn_branch)
  302. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  303. fi
  304. }
  305. #Change ANSI Colors
  306. _chengecolors(){
  307. echo -e \
  308. "\e]P0000000" \
  309. "\e]P1cd0000" \
  310. "\e]P200cd00" \
  311. "\e]P3cdcd00" \
  312. "\e]P41e90ff" \
  313. "\e]P5cd00cd" \
  314. "\e]P600cdcd" \
  315. "\e]P7353535" \
  316. "\e]P8666666" \
  317. "\e]P9ff9999" \
  318. "\e]Pa99ff99" \
  319. "\e]Pbffff99" \
  320. "\e]Pc9999ff" \
  321. "\e]Pdff99ff" \
  322. "\e]Pe99ffff" \
  323. "\e]Pfffffff"
  324. }
  325. # printf "\e]P7353535" \
  326. _colors(){
  327. echo -e \
  328. "\e[30mBlack" \
  329. "\e[31mRed" \
  330. "\e[32mGreen" \
  331. "\e[33mYellow" \
  332. "\e[34mBlue" \
  333. "\e[35mMagenta" \
  334. "\e[36mCyan" \
  335. "\e[37mWhite"
  336. echo -e \
  337. "\e[30;1mBright Black" \
  338. "\e[31;1mBright Red" \
  339. "\e[32;1mBright Green" \
  340. "\e[33;1mBright Yellow" \
  341. "\e[34;1mBright Blue" \
  342. "\e[35;1mBright Magenta" \
  343. "\e[36;1mBright Cyan" \
  344. "\e[37;1mBright White\n" \
  345. "\e[0m"
  346. }
  347. _my_install_script(){
  348. mkdir -p "$HOME/bin/"
  349. for f in "$@"
  350. do
  351. bn=$(basename "$f")
  352. type $bn >/dev/null 2>&1 || {
  353. if type wget >/dev/null 2>&1
  354. then
  355. wget "$f" -P "$HOME/bin/" &&
  356. chmod u+x "$HOME/bin/${bn}"
  357. elif type curl >/dev/null 2>&1
  358. then
  359. curl --url "$f" --output "$HOME/bin/${bn}" &&
  360. chmod u+x "$HOME/bin/${bn}"
  361. fi
  362. }
  363. done
  364. }
  365. _my_install_script http://www.frexx.de/xterm-256-notes/data/colortable16.sh http://www.frexx.de/xterm-256-notes/data/256colors2.pl
  366. _my_install_symlink_script(){
  367. mkdir -p "$HOME/bin/"
  368. for f in "$@"
  369. do
  370. ln -s "$PWD/$f" "$HOME/bin/"
  371. done
  372. }
  373. winln(){
  374. # for windose make link (actually junction)
  375. if test $# -eq 0
  376. then
  377. {
  378. echo "usage: winln TARGET LINK_NAME"
  379. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  380. echo "About other features run 'junction'."
  381. } 1>&2
  382. return 1
  383. else
  384. junction "$2" "$1"
  385. fi
  386. }
  387. __my_battery_status(){
  388. local dir=/sys/class/power_supply/BAT0
  389. if test -d $dir
  390. then
  391. local st=$(cat $dir/status)
  392. local full=$(cat $dir/charge_full)
  393. local now=$(cat $dir/charge_now)
  394. local rate=$(expr $now \* 100 / $full)
  395. printf "$1" "${st}:${rate}%"
  396. fi
  397. }
  398. alias bat='__my_battery_status %s\\n'
  399. ip-address(){
  400. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  401. test -n "$ip" && printf $1 $ip
  402. }
  403. __my_ps1_git(){
  404. __try_exec __git_ps1 "[GIT:$(__try_exec git config --get user.name):%s]"
  405. }
  406. __my_ps1_ipaddr(){
  407. test -z "$DISPLAY" && ! iswindows && ip-address [Addr:%s]
  408. }
  409. __my_ps1_bttry(){
  410. local bst="/tmp/${USER}-tmp/batterystatus"
  411. if test -z "$DISPLAY" && ! iswindows
  412. then
  413. test -f $bst && echo "[Battery:$(cat $bst)]"
  414. __my_battery_status %s >$bst &
  415. fi
  416. }
  417. __my_ps1_dirs(){
  418. dirs | wc -l
  419. }
  420. __my_ps1_jobs(){
  421. jobs | wc -l
  422. }
  423. if test "$TERM" != dumb
  424. then
  425. __my_c1="\e[1;31m" # color for PWD
  426. __my_c2="\e[0;36m" # color for user
  427. __my_c3="\e[1;30m" # color for OLDPWD
  428. __my_c4="\e[1;32m" # color for ::
  429. __my_cdef="\e[0m"
  430. fi
  431. _PS1="\
  432. ${__my_c4}:: ${__my_cdef}[${__my_c1}\w/${__my_cdef}<${__my_c3}\${PWD}${__my_cdef}]\$(__my_ps1_bttry)\$(__my_ps1_ipaddr)\n\
  433. ${__my_c4}:: ${__my_c2}\u@\H${__my_cdef} \D{%a, %d %b %Y %T %z} ${SHELL} \V\n\
  434. ${__my_c4}:: ${__my_cdef}shlv:${SHLVL} hist:\! jobs:\j last:\$? \$ "
  435. PS1=$_PS1
  436. __my_set_title(){
  437. title="$(echo $@)"
  438. case $TERM in
  439. (rxvt*|xterm*|aterm|screen*)
  440. test -t 1 &&
  441. test -n "$DISPLAY" &&
  442. test -z "$EMACS" &&
  443. echo -n -e "\033]0;${title}\007"
  444. ;;
  445. esac
  446. }
  447. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};"
  448. # copied from https://wiki.archlinux.org/index.php/X_resources
  449. invader(){
  450. # ANSI color scheme script featuring Space Invaders
  451. #
  452. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  453. # Modified by lolilolicon
  454. #
  455. f=3 b=4
  456. for j in f b; do
  457. for i in {0..7}; do
  458. printf -v $j$i %b "\e[${!j}${i}m"
  459. done
  460. done
  461. bld=$'\e[1m'
  462. rst=$'\e[0m'
  463. cat << EOF
  464. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  465. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  466. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  467. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  468. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  469. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  470. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  471. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  472. $f7▌$rst
  473. $f7▌$rst
  474. $f7 ▄█▄ $rst
  475. $f7▄█████████▄$rst
  476. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  477. EOF
  478. }
  479. #/etc/lsb-release