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.
 
 
 
 
 
 

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