More readline configuration
[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 # Run a machine-specific bashrc (if it exists).
82 if [ -f $HOME/.bashrc_local ]; then
83         source $HOME/.bashrc_local
84 fi
85
86 # Automatically open screen.
87 # The if statement prevents it from recursing (since screen opens bash).
88 if [ $TERM != screen ]; then
89         screen
90 fi
91
92 # Gets a directory named .env or .venv if it exists in the currend directory or any of its parents
93 get_env() {
94   if [ -d "$1/.env" ] ; then
95     echo "$1/.env"
96   else
97     if [ -d "$1/.venv" ] ; then
98       echo "$1/.venv"
99     else
100       if [ -d "$1/.." ] ; then
101         get_env "$1/.."
102       fi
103     fi
104   fi
105 }
106
107 get_absolute_path() {
108   python3 -c "import os; print(os.path.realpath('$1'))"
109 }
110
111 on_prompt() {
112   # Load a virtualenv environment if it exists in a file named .env
113   env_folder=$(get_env $(pwd))
114
115   if [ -d "$env_folder" ] ; then
116     if [[ $VIRTUAL_ENV != $(get_absolute_path $env_folder) ]] ; then
117       echo "Activating env '$env_folder'"
118       source "$env_folder/bin/activate"
119     fi
120   else
121     if [ -d "$VIRTUAL_ENV" ] ; then
122       deactivate
123     fi
124   fi
125 }
126
127 # Call on_prompt() every time the command prompt executes
128 PROMPT_COMMAND=on_prompt
129
130 # Set vi keybindings
131 set -o vi
132
133 # pip should only run if there is a virtualenv currently activated
134 export PIP_REQUIRE_VIRTUALENV=true