Não pode escolher mais do que 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.
 
 
 
 
 
 

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