778a65dea03efd20bf316d87a374fd0f691e405d
[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 or .venv 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/.venv" ] ; then
93       echo "$1/.venv"
94     else
95       if [ -d "$1/.." ] ; then
96         get_env "$1/.."
97       fi
98     fi
99   fi
100 }
101
102 get_absolute_path() {
103   python3 -c "import os; print(os.path.realpath('$1'))"
104 }
105
106 on_prompt() {
107   # Load a virtualenv environment if it exists in a file named .env
108   env_folder=$(get_env $(pwd))
109
110   if [ -d "$env_folder" ] ; then
111     if [[ $VIRTUAL_ENV != $(get_absolute_path $env_folder) ]] ; then
112       echo "Activating env '$env_folder'"
113       source "$env_folder/bin/activate"
114     fi
115   else
116     if [ -d "$VIRTUAL_ENV" ] ; then
117       deactivate
118     fi
119   fi
120 }
121
122 # Call on_prompt() every time the command prompt executes
123 PROMPT_COMMAND=on_prompt
124
125 # Set vi keybindings
126 set -o vi
127
128 # pip should only run if there is a virtualenv currently activated
129 export PIP_REQUIRE_VIRTUALENV=true