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.
 
 
 
 
 
 

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