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.
 
 
 
 
 
 

576 lines
14 KiB

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