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

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