summaryrefslogtreecommitdiffstats
path: root/bashrc
blob: c78e7f15ec39613439ac587b99149ee2adba4a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /bin/bash

# PROMPT
function parse_git_branch()
{
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ `whoami` = "root" ]
then
    export PS1='\[\033[01;47;31m\]\h\[\033[40;31m\] \u \[\033[37m\]\W \$ \[\033[00m\]'
else
    export PS1='\[\033[01;33m\]\u\[\033[01;35m\]@\[\033[00;31m\]\h\[\033[00m\]:\[\033[0;33m\] \W \[\033[01;35m\]$(parse_git_branch)\[\033[0;33m\] \[\033[0m\]\$ '
fi
unset PROMPT_COMMAND

# TERM
if [ -r /usr/share/terminfo/x/xterm-256color ]
then
    export TERM='xterm-256color'
else
    export TERM='xterm-color'
fi
eval $(dircolors ~/.dir_colors)

# BELL
[ ! -z $DISPLAY ] && xset b 0

# VIMMODE
set -o vi

shopt -s autocd

# COLORLS
OSNAME=`uname`
case $OSNAME in
    Linux)
        alias ls="ls --color=auto -F -b -T 0"
        alias ll="ls -l --color=auto -F -b -T 0"
        ;;
    FreeBSD)
        LSCOLOR="3x5x2x3x1x464301060203"
        alias ls="ls -GF"
        export LSCOLORS
        ;;
    OpenBSD)
        alias ls="colorls -F -G"
        export PKG_PATH="ftp://mirror.switch.ch/mirror/OpenBSD/4.0/packages/i386/"
        ;;
esac

# ENV
function set_if_not_in()
{
    env_var=$1
    arg=$2
    IFS=":";
    for p in ${!env_var}; do if [ $p == $arg ]; then IFS=" "; return 0; fi done
    IFS=" ";
    export ${env_var}=$arg:${!env_var}
}

function export_if_exists()
{
    _TMP=`which $2 2>/dev/null`
    [ -z ${_TMP} ] && _TMP=$3
    eval "export $1=$_TMP"
}

[ -r /etc/profile.d/undistract-me.sh ] && source /etc/profile.d/undistract-me.sh

# prepend ~/bin to path if not already there
HOME_=$(readlink -f ${HOME%/})
export PATH=${HOME_}/bin:${PATH#${HOME_}/bin:}
export_if_exists 'PAGER' 'less' '/bin/more'
export EDITOR=vim
# RUBY
export GEM_HOME="${HOME_}/.gem/ruby/2.5.0"
set_if_not_in 'PATH' ${GEM_HOME}/bin

# catch and eval dmalloc output
#function dmalloc { eval `command dmalloc -b $*`; }
alias vim="nvim -u ~/.vimrc"
alias vimdiff="nvim -d -u ~/.vimrc"
alias fuck='eval $(thefuck $(fc -ln -1)); history -r'


# FUNCTIONS
function lip () {    # local ips
    ip -c addr | sed -n '/^[1-9]:/p;/inet /p'
}

function xip () {    # external ip
    dig +short myip.opendns.com @resolver1.opendns.com
}

function rman () {   # centered man
    env COLUMNS=$(($COLUMNS/3*2)) man "${@}" | pr -o $((COLUMNS/3/2)) | less
}

function xcon () {   # external established connections
    ss -t -o state established '( dport = :443 || dport = :80  )' \
        | grep -Po '([0-9a-z:.]*)(?=:http[s])' | sort -u  \
        | netcat whois.cymru.com 43 | grep -v "AS Name" | sort -t'|' -k3
}

function xtract() {
    if [ -f "$1" ] ; then
        case "$1" in
            *.tar.bz2)   tar xvjf "$1"                    ;;
            *.tar.gz)    tar xvzf "$1"                    ;;
            *.bz2)       bunzip2 "$1"                     ;;
            *.rar)       unrar x "$1"                     ;;
            *.gz)        gunzip "$1"                      ;;
            *.tar)       tar xvf "$1"                     ;;
            *.tbz2)      tar xvjf "$1"                    ;;
            *.tgz)       tar xvzf "$1"                    ;;
            *.zip)       unzip "$1"                       ;;
            *.ZIP)       unzip "$1"                       ;;
            *.pax)       cat "$1" | pax -r                ;;
            *.pax.Z)     uncompress "$1" —stdout | pax -r ;;
            *.Z)         uncompress "$1"                  ;;
            *.7z)        7z x "$1"                        ;;
            *)           echo "don't know how to extract '$1'..." ;;
        esac
    else
        echo "extract: error: $1 is not valid"
    fi
}

# SSH
SSH_ENV=${HOME}/.ssh/environment
function start_agent {
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null
    # /usr/bin/ssh-add $(ls ~/.ssh/*.pub | sed 's/\.pub.*//g' | tr '\n' ' ')
}
if [ -e "${SSH_ENV}" ]
then
    . ${SSH_ENV} > /dev/null
    ps ux | grep ssh-agent$ | grep ${SSH_AGENT_PID} >/dev/null || {
        # kill old agents
        PIDS=`pidof ssh-agent`
        if [ ! -z "${PIDS}" ]; then
            for PID in ${PIDS}; do
                kill ${PID} 2>/dev/null
            done
        fi
        start_agent;
    }
else
    start_agent
fi