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

564 рядки
14 KiB

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