Prevent pip from running if there isn't a virtualenv
[dotfiles] / .bashrc
1 # ~/.bashrc: executed by bash(1) for non-login shells.
2
3 # If not running interactively, don't do anything
4 case $- in
5     *i*) ;;
6       *) return;;
7 esac
8
9 # don't put duplicate lines in the history. See bash(1) for more options
10 # don't overwrite GNU Midnight Commander's setting of `ignorespace'.
11 export HISTCONTROL=$HISTCONTROL${HISTCONTROL+,}ignoredups
12 # ... or force ignoredups and ignorespace
13 export HISTCONTROL=ignoreboth
14
15 # append to the history file, don't overwrite it
16 shopt -s histappend
17
18 # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
19
20 # check the window size after each command and, if necessary,
21 # update the values of LINES and COLUMNS.
22 shopt -s checkwinsize
23
24 # make less more friendly for non-text input files, see lesspipe(1)
25 [ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
26
27 # set the prompt
28 PS1='\w\$ '
29
30 # set variable identifying the chroot you work in (used in the prompt below)
31 if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
32     debian_chroot=$(cat /etc/debian_chroot)
33 fi
34
35 # enable color support of ls and also add handy aliases
36 if [ -x /usr/bin/dircolors ]; then
37     eval "`dircolors -b`"
38     alias ls='ls --color=auto'
39     alias dir='dir --color=auto'
40     alias vdir='vdir --color=auto'
41
42     alias grep='grep --color=auto'
43     alias fgrep='fgrep --color=auto'
44     alias egrep='egrep --color=auto'
45 fi
46
47 alias emacs='emacs -nw'
48 alias serve='python -m SimpleHTTPServer 8080'
49 alias gpg='gpg2'
50 alias gnupg='gnupg2'
51
52 export SVN_EDITOR=vim
53
54 # Enable programmable completion features (you don't need to enable
55 # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
56 # sources /etc/bash.bashrc).
57 if [ -f /etc/bash_completion ]; then
58     . /etc/bash_completion
59 fi
60
61 # Add the ~/bin directory to the path.  This allows you to install simple
62 # binaries by simply moving them into ~/bin.
63 if [ -d $HOME/bin ]; then
64         PATH="$HOME/bin:$PATH"
65         export PATH
66 fi
67
68 # Put $HOME/.cabal/bin on the path. Installing cabal doesn't
69 # automatically put installed packages on the path.
70 if [ -d $HOME/.cabal/bin ]; then
71     PATH="$HOME/.cabal/bin:$PATH"
72     export PATH
73 fi
74
75 # Put /usr/local/bin (where homebrew installs stuff) before /usr/bin on the
76 # path. This means that if a program exists at both locations, calls to that
77 # program will use the homebrew version rather than the system version.
78 PATH="/usr/local/bin:$PATH"
79 export PATH
80
81 # Automatically open screen.
82 # The if statement prevents it from recursing (since screen opens bash).
83 if [ $TERM != screen ]; then
84         screen
85 fi
86
87 # Gets a directory named .env if it exists in the currend directory or any of its parents
88 get_env() {
89     if [ -d "$1/.env" ] ; then
90         echo "$1/.env"
91     else
92         if [ -d "$1/.." ] ; then
93             get_env "$1/.."
94         fi
95     fi
96 }
97
98 on_prompt() {
99     # Load a virtualenv environment if it exists in a file named .env
100     env_folder=$(get_env $(pwd))
101
102     if [ -d "$env_folder" ] ; then
103         if [[ $VIRTUAL_ENV != $env_folder ]] ; then
104             echo "Activating env '$env_folder'"
105             source "$env_folder/bin/activate"
106         fi
107     else
108         if [ -d "$VIRTUAL_ENV" ] ; then
109             deactivate
110         fi
111     fi
112 }
113
114 # Set vi keybindings
115 set -o vi
116
117 # Call on_prompt() every time the command prompt executes
118 PROMPT_COMMAND=on_prompt
119
120 # pip should only run if there is a virtualenv currently activated
121 export PIP_REQUIRE_VIRTUALENV=true