Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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