randalltux

zshrc

Apr 12th, 2013
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.32 KB | None | 0 0
  1. # .zshrc
  2. # Author: Piotr Karbowski <[email protected]>
  3. # License: beerware.
  4.  
  5. # Basic zsh config.
  6. ZDOTDIR=${ZDOTDIR:-${HOME}}
  7. ZSHDDIR="${HOME}/.config/zsh.d"
  8. HISTFILE="${ZDOTDIR}/.zsh_history"
  9. HISTSIZE='10000'
  10. SAVEHIST="${HISTSIZE}"
  11. # env
  12. export EDITOR=vim
  13. export PAGER=less
  14. export PATH="$PATH:/usr/local/bin;/usr/bin/jre1.6.0_30/bin"
  15. #AndroidDev PATH
  16. export PATH=${PATH}:~/Projects/Android/tools
  17. export PATH=${PATH}:~/Projects/Android/platform-tools
  18. export PATH=${PATH}:/usr/lib/x86_64-linux-gnu
  19. export TMP="$HOME/tmp"
  20. export TEMP="$TMP"
  21. export TMPDIR="$TMP"
  22. fpath=(path/to/zsh-completions $fpath)
  23. export LANG="en_US.UTF-8"
  24. export LC_CTYPE="en_US.UTF-8"
  25. setopt NO_HUP
  26. setopt IGNORE_EOF
  27. setopt NO_CASE_GLOB
  28. setopt NUMERIC_GLOB_SORT
  29. setopt EXTENDED_GLOB
  30.  
  31. # Colors.
  32. red='\e[0;31m'
  33. RED='\e[1;31m'
  34. green='\e[0;32m'
  35. GREEN='\e[1;32m'
  36. yellow='\e[0;33m'
  37. YELLOW='\e[1;33m'
  38. blue='\e[0;34m'
  39. BLUE='\e[1;34m'
  40. purple='\e[0;35m'
  41. PURPLE='\e[1;35m'
  42. cyan='\e[0;36m'
  43. CYAN='\e[1;36m'
  44. NC='\e[0m'
  45.  
  46. # Functions
  47. if [ -f '/etc/profile.d/prll.sh' ]; then
  48. . "/etc/profile.d/prll.sh"
  49. fi
  50.  
  51. ewarn() { echo -e "\033[1;33m>>> \033[0m$@"; }
  52.  
  53. over_ssh() {
  54. if [ -n "${SSH_CLIENT}" ]; then
  55. return 0
  56. else
  57. return 1
  58. fi
  59. }
  60.  
  61. confirm() {
  62. local answer
  63. echo -ne "zsh: sure you want to run '${YELLOW}$@${NC}' [yN]? "
  64. read -q answer
  65. echo
  66. if [[ "${answer}" =~ ^[Yy]$ ]]; then
  67. command "${=1}" "${=@:2}"
  68. else
  69. return 1
  70. fi
  71. }
  72.  
  73. confirm_wrapper() {
  74. if [ "$1" = '--root' ]; then
  75. local as_root='true'
  76. shift
  77. fi
  78.  
  79. local runcommand="$1"; shift
  80.  
  81. if [ "${as_root}" = 'true' ] && [ "${USER}" != 'root' ]; then
  82. runcommand="sudo ${runcommand}"
  83. fi
  84. confirm "${runcommand}" "$@"
  85. }
  86.  
  87. poweroff() { confirm_wrapper --root $0 "$@"; }
  88. reboot() { confirm_wrapper --root $0 "$@"; }
  89. hibernate() { confirm_wrapper --root $0 "$@"; }
  90.  
  91. detox() {
  92. if [ "$#" -ge 1 ]; then
  93. confirm detox "$@"
  94. else
  95. command detox "$@"
  96. fi
  97. }
  98.  
  99. has() {
  100. local string="${1}"
  101. shift
  102. local element=''
  103. for element in "$@"; do
  104. if [ "${string}" = "${element}" ]; then
  105. return 0
  106. fi
  107. done
  108. return 1
  109. }
  110.  
  111. begin_with() {
  112. local string="${1}"
  113. shift
  114. local element=''
  115. for element in "$@"; do
  116. if [[ "${string}" =~ "^${element}" ]]; then
  117. return 0
  118. fi
  119. done
  120. return 1
  121.  
  122. }
  123.  
  124. termtitle() {
  125. case "$TERM" in
  126. rxvt*|xterm|nxterm|gnome|screen|screen-*)
  127. case "$1" in
  128. precmd)
  129. print -Pn "\e]0;%n@%m: %~\a"
  130. ;;
  131. preexec)
  132. zsh_cmd_title="$2"
  133. # Escape '\' char.
  134. zsh_cmd_title="${zsh_cmd_title//\\/\\\\}"
  135. # Escape '$' char.
  136. zsh_cmd_title="${zsh_cmd_title//\$/\\\\\$}"
  137. # Escape '%' char.
  138. #zsh_cmd_title="${zsh_cmd_title//%/\%\%}"
  139. # As I am unable to deal with all %, especialy
  140. # the nasted one, I will just strip this char.
  141. zsh_cmd_title="${zsh_cmd_title//\%/<percent>}"
  142. print -Pn "\e]0;${zsh_cmd_title} [%n@%m: %~]\a"
  143. ;;
  144. esac
  145. ;;
  146. esac
  147. }
  148.  
  149.  
  150.  
  151. git_check_if_worktree() {
  152. # This function intend to be only executed in chpwd().
  153. # Check if the current path is in git repo.
  154.  
  155. # We would want stop this function, on some big git repos it can take some time to cd into.
  156. if [ -n "${skip_zsh_git}" ]; then
  157. git_pwd_is_worktree='false'
  158. return 1
  159. fi
  160. # The : separated list of paths where we will run check for git repo.
  161. # If not set, then we will do it only for /root and /home.
  162. if [ "${UID}" = '0' ]; then
  163. # running 'git' in repo changes owner of git's index files to root, skip prompt git magic if CWD=/home/*
  164. git_check_if_workdir_path="${git_check_if_workdir_path:-/root}"
  165. else
  166. git_check_if_workdir_path="${git_check_if_workdir_path:-/home}"
  167. git_check_if_workdir_path_exclude="${git_check_if_workdir_path_exclude:-${HOME}/_sshfs}"
  168. fi
  169.  
  170. if begin_with "${PWD}" ${=git_check_if_workdir_path//:/ }; then
  171. if ! begin_with "${PWD}" ${=git_check_if_workdir_path_exclude//:/ }; then
  172. local git_pwd_is_worktree_match='true'
  173. else
  174. local git_pwd_is_worktree_match='false'
  175. fi
  176. fi
  177.  
  178. if ! [ "${git_pwd_is_worktree_match}" = 'true' ]; then
  179. git_pwd_is_worktree='false'
  180. return 1
  181. fi
  182.  
  183. # todo: Prevent checking for /.git or /home/.git, if PWD=/home or PWD=/ maybe...
  184. # damn annoying RBAC messages about Access denied there.
  185. if [ -d '.git' ] || [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" = 'true' ]; then
  186. git_pwd_is_worktree='true'
  187. git_worktree_is_bare="$(git config core.bare)"
  188. else
  189. unset git_branch git_worktree_is_bare
  190. git_pwd_is_worktree='false'
  191. fi
  192. }
  193.  
  194. git_branch() {
  195. git_branch="$(git symbolic-ref HEAD 2>/dev/null)"
  196. git_branch="${git_branch##*/}"
  197. git_branch="${git_branch:-no branch}"
  198. }
  199.  
  200. git_dirty() {
  201. if [ "${git_worktree_is_bare}" = 'false' ] && [ -n "$(git status --untracked-files='no' --porcelain)" ]; then
  202. git_dirty='%F{green}*'
  203. else
  204. unset git_dirty
  205. fi
  206. }
  207.  
  208. precmd() {
  209. # Set terminal title.
  210. termtitle precmd
  211.  
  212. if [ "${git_pwd_is_worktree}" = 'true' ]; then
  213. git_branch
  214. git_dirty
  215.  
  216. git_prompt=" %F{blue}[%F{253}${git_branch}${git_dirty}%F{blue}]"
  217. else
  218. unset git_prompt
  219. fi
  220. }
  221.  
  222. preexec() {
  223. # Set terminal title along with current executed command pass as second argument
  224. termtitle preexec "${(V)1}"
  225. }
  226.  
  227. chpwd() {
  228. git_check_if_worktree
  229. }
  230.  
  231. # Are we running under grsecurity's RBAC?
  232. rbac_auth() {
  233. local auth_to_role='admin'
  234. if [ "${USER}" = 'root' ]; then
  235. if ! grep -qE '^RBAC:' "/proc/self/status" && command -v gradm > /dev/null 2>&1; then
  236. echo -e "\n${BLUE}*${NC} ${GREEN}RBAC${NC} Authorize to '${auth_to_role}' RBAC role."
  237. gradm -a "${auth_to_role}"
  238. fi
  239. fi
  240. }
  241. #rbac_auth
  242.  
  243. # Le features!
  244. # extended globbing, awesome!
  245. setopt extendedGlob
  246.  
  247. # zmv - a command for renaming files by means of shell patterns.
  248. autoload -U zmv
  249.  
  250. # zargs, as an alternative to find -exec and xargs.
  251. autoload -U zargs
  252.  
  253. # Turn on command substitution in the prompt (and parameter expansion and arithmetic expansion).
  254. setopt promptsubst
  255.  
  256. # Include user-specified configs.
  257. if [ ! -d "${ZSHDDIR}" ]; then
  258. mkdir -p "${ZSHDDIR}" && echo "# Put your user-specified config here." > "${ZSHDDIR}/example.zsh"
  259. fi
  260.  
  261. #for zshd in $(ls -A ${HOME}/.config/zsh.d/^*.(z)sh$); do
  262. #. "${zshd}"
  263. #done
  264.  
  265. # Completion.
  266. autoload -Uz compinit
  267. compinit
  268. zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
  269. zstyle ':completion:*' completer _expand _complete _ignored _approximate
  270. zstyle ':completion:*' menu select=2
  271. zstyle ':completion:*' select-prompt '%SScrolling active: current selection at %p%s'
  272. zstyle ':completion::complete:*' use-cache 1
  273. zstyle ':completion:*:descriptions' format '%U%F{cyan}%d%f%u'
  274.  
  275. # If running as root and nice >0, renice to 0.
  276. if [ "$USER" = 'root' ] && [ "$(cut -d ' ' -f 19 /proc/$$/stat)" -gt 0 ]; then
  277. renice -n 0 -p "$$" && echo "# Adjusted nice level for current shell to 0."
  278. fi
  279.  
  280. # Fancy prompt.
  281. if over_ssh && [ -z "${TMUX}" ]; then
  282. prompt_is_ssh='%F{blue}[%F{red}SSH%F{blue}] '
  283. elif over_ssh; then
  284. prompt_is_ssh='%F{blue}[%F{253}SSH%F{blue}] '
  285. else
  286. unset prompt_is_ssh
  287. fi
  288.  
  289. case $USER in
  290. root)
  291. PROMPT='%B%F{cyan}%m%k %(?..%F{blue}[%F{253}%?%F{blue}] )${prompt_is_ssh}%B%F{blue}%1~${git_prompt}%F{blue} %# %b%f%k'
  292. ;;
  293.  
  294. *)
  295. PROMPT='%B%F{blue}%n@%m%k %(?..%F{blue}[%F{253}%?%F{blue}] )${prompt_is_ssh}%B%F{cyan}%1~${git_prompt}%F{cyan} %# %b%f%k'
  296.  
  297. ;;
  298. esac
  299.  
  300. # Ignore duplicate in history.
  301. setopt hist_ignore_dups
  302.  
  303. # Prevent record in history entry if preceding them with at least one space
  304. setopt hist_ignore_space
  305.  
  306. # Nobody need flow control anymore. Troublesome feature.
  307. #stty -ixon
  308. setopt noflowcontrol
  309.  
  310. # Shell config.
  311. umask 077
  312. if ! [[ "${PATH}" =~ "^${HOME}/bin" ]]; then
  313. export PATH="${HOME}/bin:${PATH}"
  314. fi
  315.  
  316. # Not all servers have terminfo for rxvt-256color. :<
  317. if [ "${TERM}" = 'rxvt-256color' ] && ! [ -f '/usr/share/terminfo/r/rxvt-256color' ] && ! [ -f '/lib/terminfo/r/rxvt-256color' ]; then
  318. export TERM='rxvt-unicode'
  319. fi
  320.  
  321.  
  322.  
  323. if [ ! -d "${TMP}" ]; then mkdir "${TMP}"; fi
  324.  
  325. if command -v colordiff > /dev/null 2>&1; then
  326. alias diff="colordiff -Nuar"
  327. else
  328. alias diff="diff -Nuar"
  329. fi
  330.  
  331. alias grep='grep --colour=auto'
  332. alias egrep='egrep --colour=auto'
  333. alias ls='ls --color=auto --human-readable --group-directories-first --classify'
  334.  
  335.  
  336. ### FUNCTIONS
  337.  
  338. cdl() {builtin cd $@; ls }
  339.  
  340. # Easy extract
  341. extract () {
  342. if [ -f $1 ] ; then
  343. case $1 in
  344. *.tar.bz2) tar xvjf $1 ;;
  345. *.tar.gz) tar xvzf $1 ;;
  346. *.tar.xz) tar xvJf $1 ;;
  347. *.bz2) bunzip2 $1 ;;
  348. *.rar) unrar x $1 ;;
  349. *.gz) gunzip $1 ;;
  350. *.tar) tar xvf $1 ;;
  351. *.tbz2) tar xvjf $1 ;;
  352. *.tgz) tar xvzf $1 ;;
  353. *.txz) tar xvJf $1 ;;
  354. *.rar) unrar $1 ;;
  355. *.zip) unzip $1 ;;
  356. *.Z) uncompress $1 ;;
  357. *.7z) 7z e $1 ;;
  358. *) echo "don't know how to extract '$1'..." ;;
  359. esac
  360. else
  361. echo "'$1' is not a valid file!"
  362. fi
  363. }
  364.  
  365. # Get weather (replace the 71822 in the url with your own zipcode, call it by typing weather)
  366. weather ()
  367. {
  368. declare -a WEATHERARRAY
  369. WEATHERARRAY=( `elinks -dump "http://www.google.com/search?hl=en&lr=&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&q=weather+21122&btnG=Search" | grep -A 5 -m 1 "Weather for" | grep -v "Add to "`)
  370. echo ${WEATHERARRAY[@]}
  371. }
  372. # Makes directory then moves into it
  373. function mkcdr {
  374. mkdir -p -v $1
  375. cd $1
  376. }
  377.  
  378. # Creates an archive from given directory
  379. mktar() { tar cvf "${1%%/}.tar" "${1%%/}/"; }
  380. mktgz() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
  381. mktbz() { tar cvjf "${1%%/}.tar.bz2" "${1%%/}/"; }
  382.  
  383. upinfo ()
  384. {
  385. echo -ne "\t ";uptime | awk /'up/ {print $3,$4,$5,$6,$7,$8,$9,$10}'
  386. }
  387.  
  388. # list files only
  389. lf () { ls -1p $@ | grep -v '\/$' }
  390.  
  391.  
  392. # define words with wordnet
  393. ddefine () { curl dict://dict.org/d:${1}:wn; }
  394.  
  395.  
  396. ## Aliases
  397.  
  398. alias ls='ls --color=auto' # color is useful.
  399. alias l='ls -lFh'
  400. alias la='ls -lAFh'
  401. alias lr='ls -tRFh'
  402. alias lt='ls -ltFh'
  403. alias rm='mv -t ~/.local/share/Trash/files'
  404. alias cp='cp -i'
  405. alias mv='mv -i'
  406. alias mkdir='mkdir -p -v'
  407. alias df='df -h'
  408. alias du='du -h -c'
  409. alias reload='source ~/.zshrc'
  410. alias biggest='BLOCKSIZE=1048576; du -x | sort -nr | head -10'
  411. alias remdir='"rm" -rf'
  412. alias trashman='"rm" -rf ~/.local/share/Trash/files/*'
  413. alias scrott='"scrot" -d 7 -c'
  414. alias zshrc="$EDITOR ~/.zshrc"
  415. alias mountiso="sudo mount -t iso9660 -o loop"
  416. alias blank="sleep 1 && xset dpms force off"
  417. alias lesslast="less !:*"
  418. alias mine="sudo chown ro"
  419. alias apacman="~/ansipacman.sh"
  420. alias atank="~/ansitanks.sh"
  421. alias invaders="~/colors.sh"
  422. alias rvundle="vim +BundleInstall +qall"
  423. alias -g G="| grep"
  424. alias -g L="| less"
  425. alias xcolors='xrdb -l ~/.Xresources'
  426. ## changing dirs with just dots.
  427. alias .='cd ../'
  428. alias ..='cd ../../'
  429. alias ...='cd ../../../'
  430. alias ....='cd ../../../../'
  431.  
  432. ## these are the dirs i use most commonly.
  433. alias home='cd ~/'
  434. alias documents='cd ~/documents'
  435. alias downloads='cd ~/downloads'
  436. alias books='cd ~/ebooks'
  437. alias pictures='cd ~/images'
  438. alias packages='cd ~/packages'
  439. alias torrents='cd ~/torrents'
  440. alias videos='cd ~/videos'
  441. alias wallpaper='cd ~/images/wallpaper'
  442. alias screenshots='cd ~/images/screenshots'
  443. alias music='cd ~/music'
  444. alias gitdir='cd ~/git'
  445. alias projects='cd ~/projects'
  446.  
  447. ## MORE aliases that i use a lot.
  448. alias ports='netstat -nape --inet'
  449. alias ping='ping -c 4'
  450. alias ns='netstat -alnp --protocol=inet'
  451. alias top="sudo htop d 1"
  452. alias ps="ps aux"
  453. alias today='date "+%d %h %y"'
  454. alias now='date "+%A, %d %B, %G%t%H:%M %Z(%z)"'
  455. alias msgs='tail -f /var/log/messages'
  456. alias nautilus='nautilus --no-desktop'
  457. alias grep="egrep --color=auto" # color grep output
  458. alias cls="clear;fortune" # another quickie
  459. alias k=kill # QUICKKEY - kill process
  460. alias e=$EDITOR # QUICKKEY - edit quickly
  461. alias m=more # QUICKKEY - yeah
  462. alias q=exit # QUICKKEY - like vim
  463.  
  464. ## Package Manager - Uncomment for your distro
  465. # Distributions Included: Debian and Debian-based Distributions like Ubuntu and Mint
  466. # Fedora, and other Yum-based Distros like OpenSUSE and Fuduntu
  467. # Sayabon, Gentoo, Archlinux.
  468.  
  469. ## Debian/Debian-based. Ubuntu, Mint, Crunchbang, etc
  470.  
  471. alias ainstall='sudo apt-get install'
  472. alias aremove='sudo apt-get remove'
  473. alias system_update='sudo aptitude safe-upgrade'
  474. alias afind='aptitude search'
  475. alias affind='apt-file search'
  476. alias afupdate='sudo apt-file update'
  477. alias aupdate='sudo aptitude update'
  478. #alias addppa='add-apt-repository'
  479. alias orphand='sudo deborphan | xargs sudo apt-get -y remove --purge'
  480. alias cleanup='sudo apt-get autoclean && sudo apt-get autoremove && sudo apt-get clean && sudo apt-get remove && orphand'
  481. alias updatedb='updatedb'
  482. alias lessswap="sysctl vm.swappiness=10"
  483. alias moreswap="sysctl vm.swappiness=100"
  484.  
  485. ## Fedora/Fuduntu/etc
  486.  
  487. #alias yinstall='sudo yum install'
  488. #alias yremove='sudo yum remove'
  489. #alias system_update='sudo yum update'
  490. #alias yfind='sudo yum search'
  491. #alias yginstall='sudo yum groupinstall'
  492.  
  493. ## Sabayon
  494.  
  495. #alias einstall='sudo equo install'
  496. #alias efind='equo search'
  497. #alias eremove='sudo equo remove'
  498. #alias eupdate='sudo equo update'
  499. #alias eupgrade='sudo equo upgrade --ask'
  500.  
  501. ## Gentoo
  502.  
  503. #alias einstall='sudo emerge -a'
  504. #alias eremove='sudo emerge -ac'
  505. #alias system_update='sudo emerge -aev world'
  506. #alias efind='sudo emerge -S'
  507. #alias eupdate='sudo emerge --sync'
  508. #alias depclean='sudo emerge --depclean'
  509.  
  510. ## Archlinux
  511.  
  512. #alias aurinst='sudo yaourt -S'
  513. #alias aurpkg='sudo pacman -U'
  514. #alias pinstall='sudo pacman -S'
  515. #alias premove='sudo pacman -R'
  516. #alias system_update='sudo pacman -Syu'
  517. #alias aurfind='sudo yaourt -Ss'
  518. #alias pfind='sudo pacman -Ss'
  519. #alias pinfo='sudo pacman -Si'
  520. #alias installedby='sudo pacman -Ql'
  521. #alias whatfrom='sudo pacman -Qo'
  522. #alias showorphan='sudo pacman -Qdt'
  523. #alias removeuseless='sudo pacman -Rsnc $(pacman -Qdqt)'
  524. #alias pacupgrade='sh ~/.mirrorfix'
  525. #alias giveswhat='sudo pkgfile --list'
  526. ## Misc
  527. alias edit='vim'
  528.  
  529.  
  530. # key binding
  531. bindkey "\e[1~" beginning-of-line # Home
  532. bindkey "\e[4~" end-of-line # End
  533. bindkey "\e[5~" beginning-of-history # PageUp
  534. bindkey "\e[6~" end-of-history # PageDown
  535. bindkey "\e[2~" quoted-insert # Ins
  536. bindkey "\e[3~" delete-char # Del
  537. bindkey "\e[5C" forward-word
  538. bindkey "\eOc" emacs-forward-word
  539. bindkey "\e[5D" backward-word
  540. bindkey "\eOd" emacs-backward-word
  541. bindkey "\e\e[C" forward-word
  542. bindkey "\e\e[D" backward-word
  543. bindkey "\e[Z" reverse-menu-complete # Shift+Tab
  544. # for rxvt
  545. bindkey "\e[7~" beginning-of-line # Home
  546. bindkey "\e[8~" end-of-line # End
  547. # for non RH/Debian xterm, can't hurt for RH/Debian xterm
  548. bindkey "\eOH" beginning-of-line
  549. bindkey "\eOF" end-of-line
  550. # for freebsd console
  551. bindkey "\e[H" beginning-of-line
  552. bindkey "\e[F" end-of-line
  553. # for guake
  554. bindkey "\eOF" end-of-line
  555. bindkey "\eOH" beginning-of-line
  556. bindkey "^[[1;5D" backward-word
  557. bindkey "^[[1;5C" forward-word
  558. bindkey "\e[3~" delete-char # Del
  559. # History
  560. bindkey "^[[A" history-beginning-search-backward
  561. bindkey "^[[B" history-beginning-search-forward
  562.  
  563.  
  564. if [ -f ~/.alert ]; then cat ~/.alert; fi
  565.  
  566. #------------------------------------------////
  567. # System Information:
  568. #------------------------------------------////
  569. clear
  570. # echo -e "${white}";toilet -f future --metal "Welcome, " $USER;
  571. echo -ne "${blue}Today is:\t\t${darkgray}" `date`; echo ""
  572. echo -e "${blue}Kernel Information: \t${darkgray}" `uname -smr`
  573. echo -ne "${purple}";upinfo;echo ""
  574. echo -e "${blue}"; cal -3
Advertisement
Add Comment
Please, Sign In to add comment