Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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