diff options
| author | Mitch Riedstra <mitch@riedstra.us> | 2015-12-28 13:52:48 -0500 |
|---|---|---|
| committer | Mitch Riedstra <mitch@riedstra.us> | 2015-12-28 13:52:48 -0500 |
| commit | d32ab30dc3611481cb55ce7b18bb1a80352f9146 (patch) | |
| tree | 0b3ee3c8fefbfc8dfa98349bb22fc9ca1358137a /mkshrc | |
| download | dotfiles-d32ab30dc3611481cb55ce7b18bb1a80352f9146.tar.gz dotfiles-d32ab30dc3611481cb55ce7b18bb1a80352f9146.tar.xz | |
Initial
Diffstat (limited to 'mkshrc')
| -rw-r--r-- | mkshrc | 587 |
1 files changed, 587 insertions, 0 deletions
@@ -0,0 +1,587 @@ +## Mitch's General shell configuration +## MIT LICENSE +# vim: syn=sh + +# How to install: +# For ZSH: +# curl https://www.rygel.us/etc/mkshrc > $HOME/.mkshrc && echo '. $HOME/.mkshrc' >> .zshrc.local +# For Bash: +# curl https://www.rygel.us/etc/mkshrc > $HOME/.mkshrc && echo '. $HOME/.mkshrc' >> .bashrc +# +# Personally I use this with mksh, .mkshrc is auto loaded by mksh + + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# ____ _ +# | _ \ _ __ ___ _ __ ___ _ __ | |_ +# | |_) | '__/ _ \| '_ ` _ \| '_ \| __| +# | __/| | | (_) | | | | | | |_) | |_ +# |_| |_| \___/|_| |_| |_| .__/ \__| +# |_| +# _____ _ _ +# | ___| _ _ __ ___| |_(_) ___ _ __ ___ +# | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| +# | _|| |_| | | | | (__| |_| | (_) | | | \__ \ +# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ +# +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +precmd() { + typeset e=$? + + (( e )) && print -n "$e|" +} +set_ps1() { + PS1='$(precmd)${BG}${ACCENT_C}${ACCENT_P}${USER_C}${USER:=$(id -un)}@$(hostname -s)${ACCENT_C}${ACCENT_P} ${ACCENT_P}${PATH_C}${PWD:-?}${ACCENT_C}${ACCENT_P}${END_C} +${END_P} [0m' + export PS1; +} +set_root_ps1() { +PS1='$(precmd)${BG}${ACCENT_C}${ACCENT_P}${HOST_C}$(hostname -s)${ACCENT_C}${ACCENT_P} ${ACCENT_P}${PATH_C}${PWD:-?}${ACCENT_C}${ACCENT_P}${END_C} +${END_P} [0m' +export PS1; +} +# RED: 31, GREEN: 32, YELLOW: 33, BLUE: 34, MAGENTA: 35, CYAN: 36, WHITE: 37, GREY 38. and No color which is 0. +set_prompt() { + if [ `id -u` -eq 0 ] ; then + # Previously + # Red for root, and no username + # PS1="[1;31m[1;40m$(hostname -s)[1;33m \$PWD + BG="[1;40m" + ACCENT_C="[0;31m" + ACCENT_P="|" + HOST_C="[1;31m" + PATH_C="[1;33m" + END_C="[1;31m" + END_P="#" + set_root_ps1 + else + # Previously + # PS1="[1;32m[1;40m${USER:=$(id -un)}@$(hostname -s)[1;33m \$PWD + BG="[1;40m" + ACCENT_C="[1;34m" + ACCENT_P="|" + USER_C="[1;32m" + PATH_C="[1;33m" + END_C="[1;33m" + END_P="$" + set_ps1 + fi +} +set_basic_prompt() { + PS1="$(precmd)${USER:=$(id -un)}@$(hostname -s) \$PWD $ " + export PS1; +} +set_super_basic_prompt() { + alias cd=do_cd +} +do_cd() { + builtin cd $@ + pwd=${PWD##HOME} + [ ${#pwd} -lt ${#PWD} ] && pwd="~$pwd" + [ "$hostname" ] || hostname=$(hostname) + PS1="$USER@$hostname:$pwd \$ " + [ $(id -u) -eq 0 ] && PS1="$USER@$hostname:$pwd # " +} + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# _ _ _ _____ _ _ +# / \ | (_) __ _ ___ | ___| _ _ __ ___| |_(_) ___ _ __ ___ +# / _ \ | | |/ _` / __| | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| +# / ___ \| | | (_| \__ \ | _|| |_| | | | | (__| |_| | (_) | | | \__ \ +# /_/ \_\_|_|\__,_|___/ |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ +# +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +setaliases() { + alias ls="ls -F"; + alias l="ls -lh"; + alias ll="ls -l"; + alias g="grep"; + alias m="$PAGER"; + alias j="jobs"; + alias p="pwd"; + # [ $(which nvi 2>/dev/null) ] && alias vi='nvi'; + alias gdb='gdb -q' + alias bc='bc -q' + alias egrep='egrep --color=auto' + alias eg='egrep' + alias egf='egrep --color=force' + alias search='egrep -rniI' +} + +setcoloraliases() { + if [ `uname` = "Linux" ] ; then + alias ls="ls --color=auto -F"; + alias lm='ls -lh --color=force | more'; + elif [ `uname` = "FreeBSD" ] ; then + alias ls="ls -FG"; + alias lm='CLICOLOR_FORCE="1" ls -l | less -r'; + fi + + alias grep="grep --color=auto"; + alias diff='colordiff'; + alias search='egrep --color=auto -rnI'; + alias t='tree -CdL' +} +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# ____ ____ _ _____ _ _ +# / ___/ ___|| | | ___| _ _ __ ___| |_(_) ___ _ __ ___ +# \___ \___ \| | | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| +# ___) |__) | |___ | _|| |_| | | | | (__| |_| | (_) | | | \__ \ +# |____/____/|_____| |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ +# +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +mkrootcrt() { + name="$1" + $openssl genpkey -algorithm RSA -out ${name}.key -aes-128-cbc -pkeyopt rsa_keygen_bits:4096 + $openssl req -x509 -new -nodes -key ${name}.key -days 20000 -out ${name}.pem +} +# Depnds on ENV variable CA_CERT and CA_KEY +gencrt() { + name="$1" + $openssl genpkey -algorithm RSA -out ${name}.key -pkeyopt rsa_keygen_bits:4096 + $openssl req -new -key ${name}.key -out ${name}.csr + $openssl x509 -req -in ${name}.csr -CA $CA_CERT -CAkey $CA_KEY \ + -CAcreateserial -out ${name}.crt -days 365 +} +gencsr() { + name="$1" + $openssl genpkey -algorithm RSA -out ${name}.key -pkeyopt rsa_keygen_bits:4096 + $openssl req -new -key ${name}.key -out ${name}.csr +} +# Usage debugstarttls $ipaddr:$port +debugstarttls() { + openssl s_client -starttls smtp -crlf -connect $1 $2 +} + + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# __ __ _ _____ _ _ +# | \/ (_)___ ___ | ___| _ _ __ ___| |_(_) ___ _ __ ___ +# | |\/| | / __|/ __| | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| +# | | | | \__ \ (__ | _|| |_| | | | | (__| |_| | (_) | | | \__ \ +# |_| |_|_|___/\___| |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ +# +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +set_nocaps() { + setxkbmap -option ctrl:nocaps +} +pkg_switch_branch() { + _f="/etc/pkg/FreeBSD.conf" + cp $_f ${_f}.orig + sed -e's/quarterly/latest/' < ${_f}.orig > ${_f} +} +find_most_recent_python() { +_python="$(which python2 2>/dev/null)" +$_python -c 'import os + +max_mtime = 0 +for dirname,subdirs,files in os.walk("."): + for fname in files: + full_path = os.path.join(dirname, fname) + mtime = os.stat(full_path).st_mtime + if mtime > max_mtime: + max_mtime = mtime + max_dir = dirname + max_file = fname + +print max_dir, max_file' +} +install_tmx() { +location="/usr/local/bin/tmx" +if ! [ -z $1 ]; then + location="$1" +fi +echo '#!/bin/sh + +# Modified further by Mitchell Riedstra to be compatible with +# /bin/sh + +# +# Modified TMUX start script from: +# http://forums.gentoo.org/viewtopic-t-836006-start-0.html +# +# Store it to `~/bin/tmx` and issue `chmod +x`. +# + +# Works because bash automatically trims by assigning to variables and by +# passing arguments +trim() { echo $1; } + +if [ -z "$1" ]; then + echo "Specify session name as the first argument" + exit +fi + +# Only because I often issue `ls` to this script by accident +if [ "$1" == "ls" ]; then + tmux ls + exit +fi + +base_session="$1" +# This actually works without the trim() on all systems except OSX +tmux_nb=$(trim `tmux ls | grep "^$base_session" | wc -l`) +if [ "$tmux_nb" == "0" ]; then + echo "Launching tmux base session $base_session ..." + tmux new-session -s $base_session +else + # Make sure we are not already in a tmux session + if [ -z "$TMUX" ]; then + # Kill defunct sessions first + old_sessions=$(tmux ls 2>/dev/null | egrep "^[0-9]{14}.*[0-9]+\)$" | cut -f 1 -d:) + for old_session_id in $old_sessions; do + tmux kill-session -t $old_session_id + done + + echo "Launching copy of base session $base_session ..." + # Session is is date and time to prevent conflict + session_id=`date +%Y%m%d%H%M%S` + # Create a new session (without attaching it) and link to base session + # to share windows + tmux new-session -d -t $base_session -s $session_id + # Create a new window in that session + #tmux new-window + # Attach to the new session + tmux attach-session -t $session_id + # When we detach from it, kill the session + tmux kill-session -t $session_id + fi +fi +' > $location && chmod +x $location && echo "Installed to: $location" + +} +find_most_recent() { + if [ "$(uname)" != 'linux'] ; then + path="0" + while [ $# -gt 0 ] ; do + case $1 in + -p) + path="$2"; shift; shift; ;; + esac + done + if [ $path = "0" ] ; then + find "$path" -printf "%T@ %Tc %p\n" | sort -n + else + echo "You need to specify a path with -p" + fi + else + echo "GNU Find only" + fi +} +startup_zram() { + size="512M" + + while [ $# -gt 0 ] ; do + case $1 in + -s) + size="$2" + shift; shift; ;; + *) + echo "Unknown option $1" + shift; ;; + esac + done + + modprobe zram + zramctl --find --size="$size" + mkswap -f /dev/zram0 + swapon /dev/zram0 + +} +# Sometimes useful function for unmounting a chroot dir on Linux +umount_recursive() { + case $1 in + -h|--help|"") + cat <<-EOF + Specifiy the full path to the mountpoint + and this function will do it's best to + unmount all child filesystems and the path + specified + Example: + $0 /path/to/filesystem/with/lots/of/mountpoints + Options: + -f|--force + EOF + ;; + -f|--force) + local _path="$2" + local filesystems=`cat /proc/mounts | grep $_path | awk '{print $2}' | sort -r` + for filesystem in $filesystems ; do + umount -f $filesystem + done + ;; + *) + local _path="$1" + local filesystems=`cat /proc/mounts | grep $_path | awk '{print $2}' | sort -r` + for filesystem in $filesystems ; do + umount $filesystem + done + ;; + esac +} +time_at_yac() { + date --date='September 22 2014' +} +find_php_short_tags() { + egrep --include="*.php" -rniI '<\?[^px=q]' . --color=force +} +gentags() { + options="-R --links --languages=php,-javascript" + dir="." + while [ $# -gt 0 ] ; do + case $1 in + -o) + options="$2" + shift; shift; + ;; + esac + done + + if [ -x $(which exctags 2>/dev/null) ] ; then + $(which exctags 2>/dev/null) $options $dir + elif [ -x $(which ctags 2>/dev/null) ] ; then + $(which ctags 2>/dev/null) $options $dir + fi +} +set_tmux_title() { + text="" + while [ $# -gt 0 ] ; do + text="$( echo $text $1)"; + shift; + done + printf $'\033k'"${text}"$'\033\\'; +} +set_title() { + text="" + while [ $# -gt 0 ] ; do + text="$( echo $text $1)"; + shift; + done + printf $'\033]0;'"$text"$'\007'; +} + +# Usage: dir_oct:file_oct path +setperms() { + dir_perms=$(echo $1 | awk -F: '{print $1}') + file_perms=$(echo $1 | awk -F: '{print $2}') + find $2 -type f -exec chmod $file_perms {} \; ; + find $2 -type d -exec chmod $dir_perms {} \; ; +} + +timestamp() { + date +%m.%d.%y_%H.%M.%S +} + + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# ____ __ _ _ _ +# / ___|___ _ __ / _(_) __ _ _ _ _ __ __ _| |_(_) ___ _ __ +# | | / _ \| '_ \| |_| |/ _` | | | | '__/ _` | __| |/ _ \| '_ \ +# | |__| (_) | | | | _| | (_| | |_| | | | (_| | |_| | (_) | | | | +# \____\___/|_| |_|_| |_|\__, |\__,_|_| \__,_|\__|_|\___/|_| |_| +# |___/ +# __ __ _ +# | \/ | __ _ _ __ __ _ __ _ ___ _ __ ___ ___ _ __ | |_ +# | |\/| |/ _` | '_ \ / _` |/ _` |/ _ \ '_ ` _ \ / _ \ '_ \| __| +# | | | | (_| | | | | (_| | (_| | __/ | | | | | __/ | | | |_ +# |_| |_|\__,_|_| |_|\__,_|\__, |\___|_| |_| |_|\___|_| |_|\__| +# |___/ +# _____ _ _ +# | ___| _ _ __ ___| |_(_) ___ _ __ ___ +# | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| +# | _|| |_| | | | | (__| |_| | (_) | | | \__ \ +# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ +# +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +pull() { + url="" + file="" + while [ $# -gt 0 ] ; do + case $1 in + -f) + file="$2" + shift; shift; ;; + -u) + url="$2" + shift; shift; ;; + esac + done + if [ -x $(which curl 2>/dev/null) ] ; then + $(which curl 2>/dev/null) $url > $file + elif [ -x $(which wget 2>/dev/null) ] ; then + $(which wget 2>/dev/null) $url -O $file + elif [ -x $(which fetch 2>/dev/null) ] ; then + $(which fetch 2>/dev/null) $url -o $file + fi +} +pulldotfile() { + file="$1" + pull -u "${UPDATE_URL}${file}" -f "$HOME/.${file}" +} +pullkeys() { + file="keys/$1" + if ! [ -d $HOME/.ssh ] ; then mkdir $HOME/.ssh ; fi + chmod 700 $HOME/.ssh + chmod 600 $HOME/.ssh/authorized_keys + mv "$HOME/.ssh/authorized_keys" "$HOME/.ssh/authorized_keys.$(timestamp)" + pull -u "${UPDATE_URL}${file}" -f "$HOME/.ssh/authorized_keys" + chmod 600 $HOME/.ssh/authorized_keys +} +pullsshconfig() { + file="ssh/config" + pull -u "${UPDATE_URL}${file}" -f "$HOME/.ssh/config" +} +updatetmuxconf() { + pulldotfile "tmux.conf" + if ! [ -e $HOME/.tmux.conf.local ] ; then + touch $HOME/.tmux.conf.local + fi +} +updategitconf() { + pulldotfile "gitconfig" +} +updateshell() { + pulldotfile "mkshrc" + . $HOME/.mkshrc +} +updatevimrc() { + cd $HOME + curl ${UPDATE_URL}vim.txz | tar xfJv - + cd - +} + + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# __ __ _ ____ _ +# | \/ (_)___ ___ / ___|___ | | ___ _ __ +# | |\/| | / __|/ __| | | / _ \| |/ _ \| '__| +# | | | | \__ \ (__ | |__| (_) | | (_) | | +# |_| |_|_|___/\___| \____\___/|_|\___/|_| +# +# _____ _ _ +# | ___| _ _ __ ___| |_(_) ___ _ __ ___ +# | |_ | | | | '_ \ / __| __| |/ _ \| '_ \/ __| +# | _|| |_| | | | | (__| |_| | (_) | | | \__ \ +# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/ +# +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +setcolorterminal() { + case $TERM in + xterm*) + export TERM=xterm-256color + set_title $(hostname) + # printf $'\033]0;'"$(hostname)"$'\007'; + ;; + screen*) + export TERM=screen-256color + set_tmux_title $(whoami) + # printf $'\033k'"$(whoami)"$'\033\\'; + ;; + esac +} + +setlscolors() { + if [ "$(uname)" == "Linux" ] ; then + export LS_COLORS='di=36;40:ln=1;31;40:so=32;40:pi=33;40:ex=1;32;40:bd=34;46:cd=34;43:su=0;41:sg=0;46:tw=0;42:ow=0;43:' + elif [ "$(uname)" == "FreeBSD" ] ; then + export LSCOLORS='gxBxcxdxCxegedabagacad' + fi +} + +colormanpages() { + export LESS_TERMCAP_mb="[01;33m" + export LESS_TERMCAP_md="[01;33m" + export LESS_TERMCAP_me="[0m" + export LESS_TERMCAP_se="[0m" + export LESS_TERMCAP_so="[01;44;33m" + export LESS_TERMCAP_ue="[0m" + export LESS_TERMCAP_us="[01;36m" + + ## Gentoo is nuts + export GROFF_NO_SGR=1 +} + + +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +# ____ _ _ _ +# / ___|| |__ ___| | | +# \___ \| '_ \ / _ \ | | +# ___) | | | | __/ | | +# |____/|_| |_|\___|_|_| +# +# ____ __ _ _ _ +# / ___|___ _ __ / _(_) __ _ _ _ _ __ __ _| |_(_) ___ _ __ +# | | / _ \| '_ \| |_| |/ _` | | | | '__/ _` | __| |/ _ \| '_ \ +# | |__| (_) | | | | _| | (_| | |_| | | | (_| | |_| | (_) | | | | +# \____\___/|_| |_|_| |_|\__, |\__,_|_| \__,_|\__|_|\___/|_| |_| +# |___/ +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +NEWLINE='$(printf "\n")' +SHELL_CONFIG=".mkshrc" +PRELOAD="$HOME/${SHELL_CONFIG}.pre" +LOCAL="$HOME/${SHELL_CONFIG}.local" +UPDATE_URL="https://www.rygel.us/etc/" +FANCY_PROMPT=1 +COLOR=1 + +############################################################ +########## Enviroment variables +export PAGER="less" + +# For use with SSL functions +export openssl="openssl" +export CA_CERT="/srv/ssl/root.pem" +export CA_KEY="/srv/ssl/root.key" + + +# Load our system profile +. /etc/profile + +# Load pre config +if [ -e $PRELOAD ] ; then + . $PRELOAD +fi + + +if + [ $SHELL = "/bin/ksh" ] || + [ $SHELL = "/bin/mksh" ] || + [ $SHELL = "/usr/bin/mksh" ] || + [ $SHELL = "/usr/local/bin/mksh" ] +then + export HISTFILE="$HOME/.ksh_history" + set -o emacs + bind ^L=clear-screen + + setaliases + + if [ $COLOR -eq 1 ] ; then + set_prompt + colormanpages + setcolorterminal + setcoloraliases + setlscolors + fi +else + # Support other shells + setaliases + + [ $SHELL = "/bin/sh" ] && set_super_basic_prompt + + if [ $COLOR -eq 1 ] ; then + colormanpages + setcolorterminal + setcoloraliases + setlscolors + fi +fi + +# Include local configuration options +if [ -f $LOCAL ] ; then + . $LOCAL +fi |
