Making .bashrc modular (and awesome)

.bashrc files quickly get complex.

Putting them into revision control is handy for persistance, but not always the best for sharing when not everyone has the same environment.

So lets fix that!

Base Script

Replace the .bashrc file with a simple script that will run other scripts.

~/.bashrc

#!/bin/bash

#-------------------------------------------------------------
# Modular bash scripts
#-------------------------------------------------------------

# iterate over our bashrc script files
for script in bashrc_scripts/*.sh
do
# check if the script is executable
if [ -x "${script}" ]; then
# run the script
source ${script}
fi
done

Now create the directory ~/bashrc_scripts and begin adding scripts in there.

Scripts must be executable to be included. So to enable a script simply make it executable:

chmod +x bashrc_scripts/[script name]

Example Scripts

The following are the scripts that I’m currently using.

bash.sh – Sets nice defaults for bash environment

#!/bin/bash

#-------------------------------------------------------------
# Source global definitions (if any)
#-------------------------------------------------------------

if [ -f /etc/bashrc ]; then
    # Read /etc/bashrc if present.
    . /etc/bashrc
fi

# don't put duplicate commands in bash history
export HISTCONTROL=erasedups

# expand the bash history to X commands
export HISTSIZE=5000

brew.sh – Script for adding Mac Brew completion to bash

#!/bin/bash

#-------------------------------------------------------------
# Brew for OS-X support
#-------------------------------------------------------------

# bash completion
if [ -f `brew --prefix`/etc/bash_completion ]; then
    . `brew --prefix`/etc/bash_completion
fi

python_brew.sh – Script for adding pythonbrew bash completion

#!/bin/bash

#-------------------------------------------------------------
# Python definitions
#-------------------------------------------------------------

# Pythonbrew
# add pythonbrew support
if [[ -s $HOME/.pythonbrew/etc/bashrc ]]; then
    source $HOME/.pythonbrew/etc/bashrc
fi

python_virtualenv.sh – Script to add virtualenv / virtualenvwrapper support and bash completion

#!/bin/bash

#-------------------------------------------------------------
# Python definitions
#-------------------------------------------------------------

# Virtualenvwrapper
# virtualenv wrapper support
export WORKON_HOME=~/Workspace/VirtualEnvs

if [[ -s /usr/local/bin/virtualenvwrapper.sh ]]; then
    source /usr/local/bin/virtualenvwrapper.sh
fi

# PIP
# tell pip to only install inside virtualenvs
export PIP_REQUIRE_VIRTUALENV=true

# make pip use the virtualenv dir
export PIP_VIRTUALENV_BASE=$WORKON_HOME

# add pip bash completion
# use eval to avoid the error "Could not find an activated virtualenv (required)."
eval `PIP_REQUIRE_VIRTUALENV= pip completion --bash`
Advertisements

One Response to “Making .bashrc modular (and awesome)”

  1. […] Follow this guide if you want to make your .bashrc modular. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: