選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bashrc 14 KiB

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