You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

555 lines
15 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="\$(__my_prompt_function)\$ "
  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="-iRMX"
  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. null type stty && {
  44. stty stop undef # unbind C-s to stop displaying output
  45. # stty erase '^h'
  46. }
  47. if iswindows; then
  48. # export TMP=/tmp
  49. # export TEMP=/tmp
  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. uname -a
  56. echo TERM $TERM connected to $(tty), running $BASH $BASH_VERSION
  57. echo
  58. ###################################
  59. # some aliases and functions
  60. test "$TERM" == dumb || _coloroption=" --color=always"
  61. iswindows || _timeoption=" --time-style=long-iso"
  62. alias ls="ls -hCF${_coloroption}${_timeoption}"
  63. # export GREP_OPTIONS=""
  64. alias grep="grep -n${_coloroption}"
  65. iswindows && alias grep="grep -n"
  66. # alias ll="ls -l"
  67. # alias la="ls -A"
  68. # alias lla="ls -Al"
  69. # alias less=""
  70. alias em="emacs -nw"
  71. null type vim && alias vi=vim
  72. alias pstree="LANG=C pstree"
  73. alias cp="cp -v"
  74. alias mv="mv -v"
  75. alias psall="ps auxww"
  76. alias q=exit
  77. alias p="$PAGER"
  78. alias c=cat
  79. alias e3=e3em
  80. alias dirs="dirs -v -l | \grep -v \$(printf '%s$' \$PWD)"
  81. alias po=popd
  82. alias pu=pushd
  83. alias sudo="sudo " # use aliases through sudo
  84. alias halt="sudo halt"
  85. alias reboot="sudo reboot"
  86. alias suspend="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  87. /org/freedesktop/UPower org.freedesktop.UPower.Suspend"
  88. alias hibernate="dbus-send --system --print-reply --dest=org.freedesktop.UPower \
  89. /org/freedesktop/UPower org.freedesktop.UPower.Hibernate"
  90. alias rand="echo \$RANDOM"
  91. alias ut="ssh 6365454829@un001.ecc.u-tokyo.ac.jp"
  92. alias xunp="file-roller -h"
  93. alias pc="sudo \paco -D"
  94. alias pcalc="python -i -c 'from math import *' "
  95. alias py3=python3
  96. alias py2=python2
  97. alias _reloadrc="test -f ~/.bashrc && source ~/.bashrc"
  98. # alias mytime="date +%Y%m%d-%H%M%S"
  99. alias sh="ENV=$HOME/.shrc PS1=\$\ sh"
  100. # type trash >/dev/null 2>&1 && alias rm=trash
  101. alias wic=wicd-curses
  102. alias wil="wicd-cli -y -l | head"
  103. alias wicn="wicd-cli -y -c -n"
  104. alias aptin="apt-get install"
  105. alias aptsearch="apt-cache search"
  106. alias aptshow="apt-cache show"
  107. for f in /usr/share/vim/vimcurrent/macros/less.sh \
  108. /usr/share/vim/vim73/macros/less.sh
  109. do
  110. test -f $f && alias vl=$f && break
  111. done
  112. alias yt=yaourt
  113. null type pacman-color && {
  114. alias pacman=pacman-color
  115. export pacman_program=pacman-color # used by pacmatic
  116. export PACMAN=pacman-color # used by yaourt
  117. }
  118. null type pacmatic && {
  119. alias pacman="pacmatic"
  120. export PACMAN="pacmatic"
  121. }
  122. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  123. alias arch-upgrade="sudo pacman -Syu"
  124. alias port-upgrade="port selfupdate && port sync && port upgrade installed"
  125. if iscygwin; then
  126. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  127. alias cygsu="cygstart /cygwinsetup.exe"
  128. alias emacs="CYGWIN=tty emacs -nw"
  129. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  130. fi
  131. alias g=git
  132. if null type _git # enable programmable completion for g
  133. then
  134. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  135. || complete -o default -o nospace -F _git g
  136. fi
  137. mcrypt-stream(){
  138. test $# -eq 2 || return 1
  139. case $1 in
  140. en)
  141. mcrypt --key $2 | base64 ;;
  142. de)
  143. base64 -d | mcrypt -d --key $2 ;;
  144. esac
  145. }
  146. gpg-stream(){
  147. test $# -eq 2 || return 1
  148. case $1 in
  149. en)
  150. gpg --passphrase $2 -c --batch |base64 ;;
  151. de)
  152. base64 -d|gpg --passphrase $2 -d --batch ;;
  153. esac
  154. }
  155. alias enst="gpg-stream en"
  156. alias dest="gpg-stream de"
  157. showinfo(){
  158. echo "Japanese letters are 表示可能"
  159. __try_exec diskinfo
  160. ! isdarwin && test -n "${DISPLAY}" && {
  161. __try_exec xrandr | \grep --color=never ^Screen
  162. }
  163. iswindows || __try_exec finger $USER
  164. LANG=C __try_exec id
  165. __try_exec xset q
  166. }
  167. x(){
  168. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  169. #mkdir -p ~/.backup/log
  170. # nohup startx >~/.backup/log/xorg.log 2>&1 &
  171. # exit
  172. exec startx
  173. else
  174. echo "X cant be started! Maybe another X is already running or something." 1>&2
  175. fi
  176. }
  177. bak(){
  178. for file in "$@"
  179. do
  180. cp -v ${file} ${file}.bak
  181. done
  182. }
  183. di(){
  184. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  185. then
  186. local diffcmd=colordiff
  187. else
  188. local diffcmd=diff
  189. fi
  190. ${diffcmd} -u "$@" | ${PAGER}
  191. }
  192. throw-away(){
  193. mkdir -p ~/.backup/tb
  194. for file in "$@"
  195. do
  196. mv $file ~/.backup/tb
  197. done
  198. }
  199. mkcd(){
  200. mkdir -p $1
  201. cd $1
  202. }
  203. catclip(){
  204. if iswindows
  205. then
  206. cat /dev/clipboard | tr -d \\r
  207. else
  208. xclip -o -selection "clipboard"
  209. fi
  210. }
  211. setclip(){
  212. if iswindows
  213. then
  214. if test $# -eq 0
  215. then
  216. sed -e 's/$/\r/' | tee /dev/clipboard
  217. else
  218. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  219. fi
  220. else
  221. if test $# -eq 0
  222. then
  223. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  224. else
  225. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  226. fi
  227. fi
  228. }
  229. if iswindows; then
  230. alias _open_file='cmd.exe //c start ""'
  231. elif isdarwin; then
  232. alias _open_file=open
  233. elif islinux; then
  234. if null type pcmanfm; then
  235. alias _open_file="LC_MESSAGES= pcmanfm"
  236. else
  237. alias _open_file="LC_MESSAGES= xdg-open"
  238. fi
  239. else
  240. alias _open_file=cat
  241. fi
  242. o(){
  243. if test $# -eq 0
  244. then
  245. _open_file . >/dev/null 2>&1 &
  246. else
  247. for f in "$@"
  248. do
  249. if test -d $f
  250. then
  251. _open_file $f >/dev/null 2>&1 &
  252. else
  253. _open_file $f >/dev/null 2>&1 &
  254. fi
  255. done
  256. fi
  257. }
  258. convmv-sjis2utf8-test(){
  259. convmv -r -f sjis -t utf8 *
  260. }
  261. convmv-sjis2utf8-notest(){
  262. convmv -r -f sjis -t utf8 * --notest
  263. }
  264. _my_git_config(){
  265. git config --global user.name '10sr'
  266. git config --global user.email '8slashes+git@gmail.com'
  267. git config --global core.autocrlf false
  268. git config --global core.excludesfile '~/.gitignore'
  269. git config --global color.ui auto
  270. git config --global status.relativePaths false
  271. git config --global status.showUntrackedFiles normal
  272. git config --global log.date iso
  273. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=iso"
  274. git config --global alias.st "status -s -b"
  275. git config --global alias.b "branch"
  276. git config --global alias.ci "commit --verbose"
  277. git config --global alias.co "checkout"
  278. git config --global alias.cim "commit --verbose -m"
  279. git config --global alias.di "diff --color"
  280. git config --global alias.me "merge --no-ff --stat -v"
  281. git config --global alias.ls "ls-files -v --full-name"
  282. git config --global alias.sl "!sl"
  283. # git config --global alias.my-ls "ls-files | xargs ls"
  284. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  285. git config --global alias.addi "add -i"
  286. if iswindows; then
  287. git config --global core.fileMode false
  288. fi
  289. }
  290. __my_parse_svn_branch() {
  291. local LANG=C
  292. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  293. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  294. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  295. }
  296. __my_svn_ps1(){
  297. if svn status >/dev/null 2>&1
  298. then
  299. local svn_branch=$(__my_parse_svn_branch)
  300. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  301. fi
  302. }
  303. #Change ANSI Colors
  304. _chengecolors(){
  305. echo -e \
  306. "\e]P0000000" \
  307. "\e]P1cd0000" \
  308. "\e]P200cd00" \
  309. "\e]P3cdcd00" \
  310. "\e]P41e90ff" \
  311. "\e]P5cd00cd" \
  312. "\e]P600cdcd" \
  313. "\e]P7353535" \
  314. "\e]P8666666" \
  315. "\e]P9ff9999" \
  316. "\e]Pa99ff99" \
  317. "\e]Pbffff99" \
  318. "\e]Pc9999ff" \
  319. "\e]Pdff99ff" \
  320. "\e]Pe99ffff" \
  321. "\e]Pfffffff"
  322. }
  323. # printf "\e]P7353535" \
  324. _colors(){
  325. echo -e \
  326. "\e[30mBlack" \
  327. "\e[31mRed" \
  328. "\e[32mGreen" \
  329. "\e[33mYellow" \
  330. "\e[34mBlue" \
  331. "\e[35mMagenta" \
  332. "\e[36mCyan" \
  333. "\e[37mWhite"
  334. echo -e \
  335. "\e[30;1mBright Black" \
  336. "\e[31;1mBright Red" \
  337. "\e[32;1mBright Green" \
  338. "\e[33;1mBright Yellow" \
  339. "\e[34;1mBright Blue" \
  340. "\e[35;1mBright Magenta" \
  341. "\e[36;1mBright Cyan" \
  342. "\e[37;1mBright White\n" \
  343. "\e[0m"
  344. }
  345. _my_install_script(){
  346. mkdir -p "$HOME/bin/"
  347. for f in "$@"
  348. do
  349. bn=$(basename "$f")
  350. type $bn >/dev/null 2>&1 || {
  351. if type wget >/dev/null 2>&1
  352. then
  353. wget "$f" -P "$HOME/bin/" &&
  354. chmod u+x "$HOME/bin/${bn}"
  355. elif type curl >/dev/null 2>&1
  356. then
  357. curl --url "$f" --output "$HOME/bin/${bn}" &&
  358. chmod u+x "$HOME/bin/${bn}"
  359. fi
  360. }
  361. done
  362. }
  363. _my_install_script http://www.frexx.de/xterm-256-notes/data/colortable16.sh http://www.frexx.de/xterm-256-notes/data/256colors2.pl
  364. _my_install_symlink_script(){
  365. mkdir -p "$HOME/bin/"
  366. for f in "$@"
  367. do
  368. ln -s "$PWD/$f" "$HOME/bin/"
  369. done
  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. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  399. test -n "$ip" && printf $1 $ip
  400. }
  401. __my_prompt_function(){ # used by PS1
  402. # remove __try_exec from function
  403. local lastreturn=$?
  404. if test "${TERM}" == dumb
  405. then
  406. local c1=
  407. local c2=
  408. local c3=
  409. local cdef=
  410. else
  411. local c1="\e[1;31m" # color for PWD
  412. local c2="\e[36m" # color for user
  413. local c3="\e[1;30m" # color for OLDPWD
  414. local c4="\e[1;32m" # color for ::
  415. local cdef="\e[0m"
  416. fi
  417. if iswindows
  418. then
  419. local pwd=$PWD
  420. local oldpwd=$OLDPWD
  421. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  422. else
  423. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  424. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  425. local jobnum=$(jobs | wc -l) # a strange job exists...
  426. local git=$(__try_exec __git_ps1 [GIT:%s])
  427. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  428. local dirs=$(dirs | wc -l)
  429. fi
  430. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  431. if test -z "$DISPLAY" && ! iswindows
  432. then
  433. local ip=$(ip-address [Addr:%s])
  434. local bst="/tmp/${USER}-tmp/batterystatus"
  435. test -f $bst && local battery="[Battery:$(sed -e 's`%`%%`g' $bst)]"
  436. __my_battery_status %s >$bst &
  437. fi
  438. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  439. printf "${c4}::${cdef} [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  440. printf "${c4}::${cdef} ${c2}${USER}@${HOSTNAME}${cdef} ${date}\n"
  441. printf "${c4}::${cdef} shlv:${SHLVL} dirs:${dirs} last:${lastreturn} "
  442. }
  443. _PS1="\e[32m:: \e[0m[\e[31m\w/\e[0m]\n\
  444. \e[32m:: \e[36m\u@\H \e[0m\d \t bash \v\n\
  445. \e[32m:: \e[0mshlv:${SHLVL} hist:\! last:\$? \$ "
  446. iswindows && PS1=$_PS1
  447. __my_set_title(){
  448. title="$(echo $@)"
  449. case $TERM in
  450. (rxvt*|xterm*|aterm|screen*)
  451. test -t 1 &&
  452. test -n "$DISPLAY" &&
  453. test -z "$EMACS" &&
  454. echo -n -e "\033]0;${title}\007"
  455. ;;
  456. esac
  457. }
  458. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};"
  459. # copied from https://wiki.archlinux.org/index.php/X_resources
  460. invader(){
  461. # ANSI color scheme script featuring Space Invaders
  462. #
  463. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  464. # Modified by lolilolicon
  465. #
  466. f=3 b=4
  467. for j in f b; do
  468. for i in {0..7}; do
  469. printf -v $j$i %b "\e[${!j}${i}m"
  470. done
  471. done
  472. bld=$'\e[1m'
  473. rst=$'\e[0m'
  474. cat << EOF
  475. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  476. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  477. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  478. $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. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  482. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  483. $f7▌$rst
  484. $f7▌$rst
  485. $f7 ▄█▄ $rst
  486. $f7▄█████████▄$rst
  487. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  488. EOF
  489. }
  490. #/etc/lsb-release