Source

dotfiles / shellrc

#!/bin/sh
# ~/.shellrc - shell agnostic rc file, sourced by bashrc, zshrc etc.
# NOTE: Try to keep things in here sh-compliant.

#### Setup prompt colour ############################################
function getPromptColour {
  #  31     32      33    34       35    36
  # red  green  yellow  blue  magenta  cyan
  case "$1" in
    firefly-desky ) echo "green"   ;;
    firefly-lappy ) echo "red"     ;;
    firefly-xen   ) echo "yellow"  ;;
    firefly-n900  ) echo "cyan"    ;;
    *.kth.se      ) echo "blue"    ;;
    Nokia-N900    ) echo "cyan"    ;;
    *             ) echo "magenta" ;;
  esac
}

unamen="`uname -n`"
prompt_color="`getPromptColour $unamen`"


#### Vars & aliases #################################################

# Make sure that a command exists and allows the given flags, by attempting to
# run it with said flags + "--version" (to prevent default operation).
#   Makes the assumption that the program complains about unrecognized flags,
# and that it has a "--version" flag that returns zero on success.
function try_alias {
  to_be_bound="$1" # $to_be_bound is the command that is to be bound,
  shift            # $* is the target of the aliasing.

  # Attempt to run the command...
  $@ --version >/dev/null 2>&1 \
  && alias $to_be_bound="$*"
  # ...and make sure that it exited with an exit code of 0.
}

# Like try_alias, but for environment variables
function try_export {
  # see `try_alias` above
  to_be_bound="$1"  # the variable to assign & export
  shift

  # Attempt to run..
  $@ --version >/dev/null 2>&1 \
  && export $to_be_bound="$*"
}

## Environment variables
export TERMINAL=pangoterm # since $TERM has a special (termcap-related) meaning
export EDITOR=vim
#export VISUAL=gvim
export PAGER=less
export BROWSER=luakit

export GREP_OPTIONS='--color=auto' # -P
#export GREP_COLORS='cx=37'
export GREP_COLORS="mt=38;5;208:cx=38;5;246"

# see termcap(5)
export LESS_TERMCAP_md=$'\e[1;33m'
export LESS_TERMCAP_me=$'\e[m'
export LESS_TERMCAP_us=$'\e[0;32m'
export LESS_TERMCAP_ue=$'\e[m'
export LESS_TERMCAP_so=$'\e[0;40;38m'
export LESS_TERMCAP_se=$'\e[m'

export PATH="$PATH:$HOME/local/bin:$HOME/node_modules/.bin:$HOME/.cabal/bin"
export NODE_PATH=/usr/lib/jsctags:$NODE_PATH

export DEVKITPRO="/opt/devkitpro"
export DEVKITARM="${DEVKITPRO}/devkitARM"

export GOPATH="$HOME/prog/lang/go/site:$HOME/prog/lang/go:$GOPATH"
export ABSROOT="$HOME/local/abs"

if [ "$TERM" = "xterm" ]; then
  TERM="${TERM}-256color"
fi


## Aliases
try_alias  ls    ls --color=auto
#try_alias  grep  grep -P --color=auto
#try_alias  sed   ssed -R
try_alias  grepall  grep --color=always -C9999
# not ready yet--the word wrapping is too bad :(
#try_export PAGER "$HOME/.vim/macros/less.sh"

# `less` is in my muscle memory...
#alias less=$PAGER

alias hc=herbstclient

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."

alias ff_colours='for i in `seq 30 37`; do echo -e "\e[0;${i}m $i \e[1;${i}m $i \e[m"; done'
#alias make-luakit="make clean && USE_UNIQUE=1 USE_LUAJIT=1 CC=clang make -j4"
alias make-luakit="make clean && USE_UNIQUE=1 CC=clang make -j4"

alias tray='stalonetray -bg "#333" --geometry "1x1-0+0" --grow-gravity E -i 16 --log-level info'

function thumb {
  convert -resize 250x250 "$1" "${1/\./_thumb.}"
}

function mkdircd {
  mkdir "$@"
  cd "${@[$#]}"  # (last parameter)
}

# allow vim's :h to work in the shell, and make it behave like less
function :h {
  vim +":h $1" +'wincmd o' +'nnoremap q :q!<CR>'
}

# pretty Go docs
function godoc {
  go doc "$@" | vim +'set ft=godoc' +'runtime macros/pager.vim' -
}