Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

592 строки
15 KiB

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