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