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.
 
 
 
 
 
 

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