您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

684 行
17 KiB

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