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.
 
 
 
 
 
 

528 rivejä
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. iswindows || test "$TERM" == dumb || _coloroption=" --color=always"
  61. alias ls="ls -hCF --time-style=long-iso${_coloroption}"
  62. iswindows && alias ls="ls -hCF"
  63. # export GREP_OPTIONS=""
  64. alias grep="grep -n${_coloroption}"
  65. # alias ll="ls -l"
  66. # alias la="ls -A"
  67. # alias lla="ls -Al"
  68. # alias less=""
  69. # alias vl=/usr/share/vim/vimcurrent/macros/less.sh
  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. alias yt=yaourt
  108. null type pacman-color && {
  109. alias pacman=pacman-color
  110. export pacman_program=pacman-color # used by pacmatic
  111. export PACMAN=pacman-color # used by yaourt
  112. }
  113. null type pacmatic && {
  114. alias pacman="pacmatic"
  115. export PACMAN="pacmatic"
  116. }
  117. alias ubuntu-upgrade="sudo apt-get autoremove --yes && sudo apt-get update --yes && sudo apt-get upgrade --yes"
  118. alias arch-upgrade="sudo pacman -Syu"
  119. alias port-upgrade="port selfupdate && port sync && port upgrade installed"
  120. if iscygwin; then
  121. null type windate || alias windate="/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%'"
  122. alias cygsu="cygstart /cygwinsetup.exe"
  123. alias emacs="CYGWIN=tty emacs -nw"
  124. alias ls="ls -CFG $(iswindows || test "$TERM" == dumb || echo --color=auto)"
  125. fi
  126. alias g=git
  127. if null type _git # enable programmable completion for g
  128. then
  129. complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \
  130. || complete -o default -o nospace -F _git g
  131. fi
  132. crypt-stream(){
  133. test $# -eq 2 || return 1
  134. case $1 in
  135. en)
  136. mcrypt --key $2 | base64 ;;
  137. de)
  138. base64 -d | mcrypt -d --key $2 ;;
  139. esac
  140. }
  141. gpg-stream(){
  142. test $# -eq 2 || return 1
  143. case $1 in
  144. en)
  145. gpg --passphrase $2 -c --batch |base64 ;;
  146. de)
  147. base64 -d|gpg --passphrase $2 -d --batch ;;
  148. esac
  149. }
  150. alias enst="gpg-stream en"
  151. alias dest="gpg-stream de"
  152. showinfo(){
  153. echo "Japanese letters are 表示可能"
  154. __try_exec diskinfo
  155. ! isdarwin && test -n "${DISPLAY}" && {
  156. __try_exec xrandr | \grep --color=never ^Screen
  157. }
  158. iswindows || __try_exec finger $USER
  159. LANG=C __try_exec id
  160. __try_exec xset q
  161. }
  162. x(){
  163. if [[ -z $DISPLAY ]] && ! [[ -e /tmp/.X11-unix/X0 ]] && (( EUID )); then
  164. #mkdir -p ~/.backup/log
  165. # nohup startx >~/.backup/log/xorg.log 2>&1 &
  166. # exit
  167. exec startx
  168. else
  169. echo "X cant be started! Maybe another X is already running or something." 1>&2
  170. fi
  171. }
  172. bak(){
  173. for file in "$@"
  174. do
  175. cp -v ${file} ${file}.bak
  176. done
  177. }
  178. di(){
  179. if type colordiff >/dev/null 2>&1 && test $TERM != dumb
  180. then
  181. local diffcmd=colordiff
  182. else
  183. local diffcmd=diff
  184. fi
  185. ${diffcmd} -u "$@" | ${PAGER}
  186. }
  187. throw-away(){
  188. mkdir -p ~/.backup/tb
  189. for file in "$@"
  190. do
  191. mv $file ~/.backup/tb
  192. done
  193. }
  194. mkcd(){
  195. mkdir -p $1
  196. cd $1
  197. }
  198. catclip(){
  199. if iswindows
  200. then
  201. cat /dev/clipboard | tr -d \\r
  202. else
  203. xclip -o -selection "clipboard"
  204. fi
  205. }
  206. setclip(){
  207. if iswindows
  208. then
  209. if test $# -eq 0
  210. then
  211. sed -e 's/$/\r/' | tee /dev/clipboard
  212. else
  213. cat "$@" | sed -e 's/$/\r/' | tee /dev/clipboard
  214. fi
  215. else
  216. if test $# -eq 0
  217. then
  218. xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  219. else
  220. cat "$@" | xclip -i -f -selection "primary" | xclip -i -f -selection "clipboard"
  221. fi
  222. fi
  223. }
  224. if iswindows; then
  225. alias _open_file='cmd.exe //c start ""'
  226. elif isdarwin; then
  227. alias _open_file=open
  228. elif islinux; then
  229. if null type pcmanfm; then
  230. alias _open_file="LC_MESSAGES= pcmanfm"
  231. else
  232. alias _open_file="LC_MESSAGES= xdg-open"
  233. fi
  234. else
  235. alias _open_file=cat
  236. fi
  237. o(){
  238. if test $# -eq 0
  239. then
  240. _open_file . >/dev/null 2>&1 &
  241. else
  242. for f in "$@"
  243. do
  244. if test -d $f
  245. then
  246. _open_file $f >/dev/null 2>&1 &
  247. else
  248. _open_file $f >/dev/null 2>&1 &
  249. fi
  250. done
  251. fi
  252. }
  253. convmv-sjis2utf8-test(){
  254. convmv -r -f sjis -t utf8 *
  255. }
  256. convmv-sjis2utf8-notest(){
  257. convmv -r -f sjis -t utf8 * --notest
  258. }
  259. _my_git_config(){
  260. git config --global user.name '10sr'
  261. git config --global user.email '8slashes+git@gmail.com'
  262. git config --global core.autocrlf false
  263. git config --global core.excludesfile '~/.gitignore'
  264. git config --global color.ui auto
  265. git config --global status.relativePaths false
  266. git config --global status.showUntrackedFiles normal
  267. git config --global log.date iso
  268. git config --global alias.graph "log --graph --date-order -C -M --pretty=tformat:\"<%h> %ad [%an] %Cgreen%d%Creset %s\" --all --date=iso"
  269. git config --global alias.st "status -s -b"
  270. git config --global alias.b "branch"
  271. git config --global alias.ci "commit --verbose"
  272. git config --global alias.co "checkout"
  273. git config --global alias.cim "commit --verbose -m"
  274. git config --global alias.di "diff --color"
  275. git config --global alias.me "merge --no-ff --stat -v"
  276. git config --global alias.ls "ls-files -v --full-name"
  277. git config --global alias.sl "!sl"
  278. # git config --global alias.my-ls "ls-files | xargs ls"
  279. # git config --global alias.ll "!git ls-files | xargs ls -l -CFG --color=auto --time-style=long-iso"
  280. git config --global alias.addi "add -i"
  281. if iswindows; then
  282. git config --global core.fileMode false
  283. fi
  284. }
  285. __my_parse_svn_branch() {
  286. local LANG=C
  287. local svn_url=$(svn info 2>/dev/null | sed -ne 's#^URL: ##p')
  288. local svn_repository_root=$(svn info 2>/dev/null | sed -ne 's#^Repository Root: ##p')
  289. echo ${svn_url} | sed -e 's#^'"${svn_repository_root}"'##g' | awk '{print $1}'
  290. }
  291. __my_svn_ps1(){
  292. if svn status >/dev/null 2>&1
  293. then
  294. local svn_branch=$(__my_parse_svn_branch)
  295. test -n "${svn_branch}" && printf "$1" "{$svn_branch}"
  296. fi
  297. }
  298. #Change ANSI Colors
  299. _chengecolors(){
  300. echo -e \
  301. "\e]P0000000" \
  302. "\e]P1cd0000" \
  303. "\e]P200cd00" \
  304. "\e]P3cdcd00" \
  305. "\e]P41e90ff" \
  306. "\e]P5cd00cd" \
  307. "\e]P600cdcd" \
  308. "\e]P7353535" \
  309. "\e]P8666666" \
  310. "\e]P9ff9999" \
  311. "\e]Pa99ff99" \
  312. "\e]Pbffff99" \
  313. "\e]Pc9999ff" \
  314. "\e]Pdff99ff" \
  315. "\e]Pe99ffff" \
  316. "\e]Pfffffff"
  317. }
  318. # printf "\e]P7353535" \
  319. _colors(){
  320. echo -e \
  321. "\e[30mBlack" \
  322. "\e[31mRed" \
  323. "\e[32mGreen" \
  324. "\e[33mYellow" \
  325. "\e[34mBlue" \
  326. "\e[35mMagenta" \
  327. "\e[36mCyan" \
  328. "\e[37mWhite"
  329. echo -e \
  330. "\e[30;1mBright Black" \
  331. "\e[31;1mBright Red" \
  332. "\e[32;1mBright Green" \
  333. "\e[33;1mBright Yellow" \
  334. "\e[34;1mBright Blue" \
  335. "\e[35;1mBright Magenta" \
  336. "\e[36;1mBright Cyan" \
  337. "\e[37;1mBright White\n" \
  338. "\e[0m"
  339. }
  340. _my_install_script(){
  341. mkdir -p $HOMO/bin/
  342. for f in "$@"
  343. do
  344. bn=$(basename "$f")
  345. type $bn >/dev/null 2>&1 || {
  346. wget "$f" -P "$HOME/bin/" &&
  347. chmod u+x "$HOME/bin/${bn}"
  348. }
  349. done
  350. }
  351. _my_install_script http://www.frexx.de/xterm-256-notes/data/colortable16.sh
  352. winln(){
  353. # for windose make link (actually junction)
  354. if test $# -eq 0
  355. then
  356. {
  357. echo "usage: winln TARGET LINK_NAME"
  358. echo "Create a link to TARGET with the name LINK_NAME (that is, TARGET must already exist)."
  359. echo "About other features run 'junction'."
  360. } 1>&2
  361. return 1
  362. else
  363. junction "$2" "$1"
  364. fi
  365. }
  366. __my_battery_status(){
  367. local dir=/sys/class/power_supply/BAT0
  368. if test -d $dir
  369. then
  370. local st=$(cat $dir/status)
  371. local full=$(cat $dir/charge_full)
  372. local now=$(cat $dir/charge_now)
  373. local rate=$(expr $now \* 100 / $full)
  374. printf "$1" "${st}:${rate}%"
  375. fi
  376. }
  377. alias bat='__my_battery_status %s\\n'
  378. ip-address(){
  379. local ip=$(LANG=C ifconfig | \grep --color=never "inet " | \grep --color=never -v "127.0.0.1" | awk '{print $2}')
  380. test -n "$ip" && printf $1 $ip
  381. }
  382. __my_prompt_function(){ # used by PS1
  383. # remove __try_exec from function
  384. local lastreturn=$?
  385. if test "${TERM}" == dumb
  386. then
  387. local c1=
  388. local c2=
  389. local c3=
  390. local cdef=
  391. else
  392. local c1="\e[33m"
  393. local c2="\e[36m"
  394. local c3="\e[37m"
  395. local cdef="\e[0m"
  396. fi
  397. if iswindows
  398. then
  399. local pwd=$PWD
  400. local oldpwd=$OLDPWD
  401. git branch >/dev/null 2>&1 && local git="[GIT]"
  402. local date=$(/c/Windows/System32/cmd.exe //c 'echo %DATE%-%TIME%')
  403. else
  404. local pwd=$(echo "${PWD}/" | sed -e "s#${HOME}#~#")
  405. local oldpwd=$(echo "${OLDPWD}/" | sed -e "s#${HOME}#~#")
  406. local jobnum=$(jobs | wc -l) # a strange job exists...
  407. local git=$(__try_exec __git_ps1 [GIT:%s])
  408. local date=$(LANG=C __try_exec date +"%a, %d %b %Y %T %z")
  409. fi
  410. local dirs=$(dirs | wc -l)
  411. # local svn=$(type svn >/dev/null 2>&1 && __try_exec __my_svn_ps1 [SVN:%s])
  412. if test -z "$DISPLAY" && ! iswindows
  413. then
  414. local ip=$(ip-address [Addr:%s])
  415. local bst="/tmp/${USER}-tmp/batterystatus"
  416. test -f $bst && local battery="[Battery:$(sed -e 's`%`%%`g' $bst)]"
  417. __my_battery_status %s >$bst &
  418. fi
  419. # local battery=$(battery-state [%s] | sed -e 's`%`%%`g') # very slow
  420. printf " [${c1}${pwd}${cdef}<${c3}${oldpwd}${cdef}]${git}${svn}${battery}${ip}\n"
  421. printf "${c2}${USER}@${HOSTNAME}${cdef} ${date}\n"
  422. printf "shlv:${SHLVL} dirs:${dirs} last:${lastreturn} "
  423. }
  424. __my_set_title(){
  425. title="$(echo $@)"
  426. case $TERM in
  427. (rxvt*|xterm*|aterm|screen*)
  428. test -t 1 &&
  429. test -n "$DISPLAY" &&
  430. test -z "$EMACS" &&
  431. echo -n -e "\033]0;${title}\007"
  432. ;;
  433. esac
  434. }
  435. export PROMPT_COMMAND="__my_set_title \${USER}@\${HOSTNAME}\ \${PWD};"
  436. # copied from https://wiki.archlinux.org/index.php/X_resources
  437. invader(){
  438. # ANSI color scheme script featuring Space Invaders
  439. #
  440. # Original: http://crunchbanglinux.org/forums/post/126921/#p126921
  441. # Modified by lolilolicon
  442. #
  443. f=3 b=4
  444. for j in f b; do
  445. for i in {0..7}; do
  446. printf -v $j$i %b "\e[${!j}${i}m"
  447. done
  448. done
  449. bld=$'\e[1m'
  450. rst=$'\e[0m'
  451. cat << EOF
  452. $f1 ▀▄ ▄▀ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4 ▀▄ ▄▀ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  453. $f1 ▄█▀███▀█▄ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4 ▄█▀███▀█▄ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  454. $f1█▀███████▀█ $f2▀▀███▀▀███▀▀ $f3▀█▀██▀█▀ $f4█▀███████▀█ $f5▀▀███▀▀███▀▀ $f6▀█▀██▀█▀$rst
  455. $f1▀ ▀▄▄ ▄▄▀ ▀ $f2 ▀█▄ ▀▀ ▄█▀ $f3▀▄ ▄▀ $f4▀ ▀▄▄ ▄▄▀ ▀ $f5 ▀█▄ ▀▀ ▄█▀ $f6▀▄ ▄▀$rst
  456. $bld$f1▄ ▀▄ ▄▀ ▄ $f2 ▄▄▄████▄▄▄ $f3 ▄██▄ $f4▄ ▀▄ ▄▀ ▄ $f5 ▄▄▄████▄▄▄ $f6 ▄██▄ $rst
  457. $bld$f1█▄█▀███▀█▄█ $f2███▀▀██▀▀███ $f3▄█▀██▀█▄ $f4█▄█▀███▀█▄█ $f5███▀▀██▀▀███ $f6▄█▀██▀█▄$rst
  458. $bld$f1▀█████████▀ $f2▀▀▀██▀▀██▀▀▀ $f3▀▀█▀▀█▀▀ $f4▀█████████▀ $f5▀▀▀██▀▀██▀▀▀ $f6▀▀█▀▀█▀▀$rst
  459. $bld$f1 ▄▀ ▀▄ $f2▄▄▀▀ ▀▀ ▀▀▄▄ $f3▄▀▄▀▀▄▀▄ $f4 ▄▀ ▀▄ $f5▄▄▀▀ ▀▀ ▀▀▄▄ $f6▄▀▄▀▀▄▀▄$rst
  460. $f7▌$rst
  461. $f7▌$rst
  462. $f7 ▄█▄ $rst
  463. $f7▄█████████▄$rst
  464. $f7▀▀▀▀▀▀▀▀▀▀▀$rst
  465. EOF
  466. }
  467. #/etc/lsb-release