Skip to content

Latest commit

 

History

History
351 lines (270 loc) · 9.02 KB

profile.org

File metadata and controls

351 lines (270 loc) · 9.02 KB

Shell Dot Profile

This generates my .profile that contains environment variables and similar settings applicable to all my shells. Yeah, I’m some-what of a shell-slut, and dabble between Zshell and Fish.

Homebrew

Make sure that Homebrew is both installed and configured correctly.

if which osascript >/dev/null 2>&1
then
  export ON_A_MAC=1
fi

if [ ! -d /usr/local/Cellar -a -n "$ON_A_MAC" ]
then
  echo "ZOMG! No Homebrew installed! Installing now..."
  echo 'ruby -e "`curl -fsSL https://raw.github.com/mxcl/homebrew/go/install`"'
fi

Path

We want to add these directories, but only if they exist. This makes this more portable between my computers.

OLDPATH=$PATH
PATH=$HOME/bin

for DIR in /opt/local/bin /opt/local/sbin /usr/local/bin /usr/local/sbin
do
  if [ -d $DIR ]
  then
      PATH=$PATH:$DIR
  fi
done

PATH=$PATH:$OLDPATH

Emacs

Set EDITOR to start up an emacsclient, but do that from the one I built from Homebrew:

export ALTERNATE_EDITOR=/usr/local/bin/emacs
export EDITOR=/usr/local/bin/emacsclient

Global Aliases

Just standard things that I keep typing:

alias e='emacsclient -nw'
alias ee='emacsclient'
alias gst='git status'

if type gls >/dev/null 2>&1
then
    alias ls='gls --color'
    alias ll='gls -al --color'
else
    alias ll='ls -al'
fi

Python

Use Homebrew to install the pyenv project:

brew install pyenv

Initialize the project with the following code:

export PYENV_ROOT="${HOME}/.pyenv"

if [ -d "${PYENV_ROOT}" ]; then
    export PATH="${PYENV_ROOT}/bin:${PATH}"
    eval "$(pyenv init -)"
fi

Install a particular version of Python:

pyenv install 2.7.5
pyenv global 2.7.5

Use a particular Python version with:

pyenv virtualenv $NAME    # Creates the virtual env
pyenv activate $NAME      # Choose the virtual env
pyenv deactivate          # Stops using it

While in the root directory of a project, automatically use the appropriate Python version with the local command (do this just once):

pyenv local <virtualenv or version>

When entering this directory, the chosen virtualenv or Python version will be activated automatically. The file that is creatied and specifies the appropriate environment is named .python-version (add this to git).

Enhance pyenv with the pyenv-virtualenv plugin. If installed, this code initializes it:

if which pyenv-virtualenv-init > /dev/null
then
    eval "$(pyenv virtualenv-init -)"
fi

Create a virtual environment with:

pyenv virtualenv 2.7.10 lp-demo

List the created virtual environments:

pyenv virtualenvs

It seems that the local command may make this a moot point, activate a virtual environment manually with:

pyenv activate <name>
pyenv deactivate

In other words, this pyenv project subsumes both autoenv and virtualenvwrapper. See Virtual Environments in the Python Emacs setup for details.

I like the prompt changing feature that will be removed from some future release. Let’s turn it on.

export PYENV_VIRTUALENV_DISABLE_PROMPT=1

Completion for pip comes from the command: pip completion --bash:

_pip_completion()
{
    COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   PIP_AUTO_COMPLETE=1 $1 ) )
}
complete -o default -F _pip_completion pip

Ruby

Install RVM via:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable

And then just source the following:

source $HOME/.rvm/scripts/rvm

Create new virtual environments with:

rvm install 2.0.0
rvm use ruby-2.0.0-p643
rvm gemset create chef
rvm gemset use chef

And use those environements with:

rvm use ruby-2.0.0-p643@chef

And now gem commands work as expected:

gem install bundler

RVM can give completion capabilities to Bash:

[[ -r $rvm_path/scripts/completion ]] && . $rvm_path/scripts/completion

Completions

Assuming the location of all the Bash completions:

source /usr/local/etc/bash_completion.d/brew
# source /usr/local/etc/bash_completion.d/coreutils
# source /usr/local/etc/bash_completion.d/findutils
source /usr/local/etc/bash_completion.d/git-prompt.sh
source /usr/local/etc/bash_completion.d/git-completion.bash
# source /usr/local/etc/bash_completion.d/ssh

Prompt

A helper function to help trim down lengthy directories:

function _trim_dir {
    V='[[:alnum:]._-]'
    D='[[:alnum:]._/-]'
    sed -E "s|/$D+/($V+)|../\1|; s/ / /g" <<< $1
}

The __git_ps1 is nice, but can be lengthy when pulling down Gerrit reviews, so let’s make a simpler branch:

function _mygit_ps1 {
  __git_ps1 " :%s" | sed 's|^ :review/.*/\([0-9]*\)$| :review-\1|'
}

Better approach to displaying the current path, is to only display the first or second directory name … any maybe the name of the Git project. Holy hell, so many exceptions and so few patterns…

function prompt_dir {
    PWD=$(pwd)

    if [[ $PWD == $HOME ]]
    then
        echo -n '~'
    elif [[ $PWD == $HOME/Work ]]
    then
        echo -n '~/Work'

         # In a Git project?
    elif PRJ=$(git rev-parse --show-toplevel 2>/dev/null)
    then
        name=$(basename $PRJ)
        rest=$(sed "s|$PRJ||" <<< $PWD)
        echo -n "$(sed -e 's/ / /g' <<< [$name])$(_trim_dir $rest)"

         # In work-related directory...
    elif [[ $PWD == $HOME/Work/* ]]
    then
        name=$(sed -E "s|$HOME/Work/([[:alnum:]_-]+).*|\1|; s/ / /g" <<< $PWD)
        base=$(basename $PWD)
        intr=$(basename "`dirname \"$PWD\"`")

        if [[ "$name" == "$base" ]]
        then
            echo -n "Ⓦ/$name"
        elif [[ "$intr" == "$name" ]]
        then
            echo -n "Ⓦ/$name/$base"
        else
            echo -n "Ⓦ/$name/../$base"
        fi

         # In a home directory
    elif [[ $PWD == $HOME/* ]]
    then
        base=$(basename $PWD)
        intr=$(basename "`dirname \"$PWD\"`")
        if [[ "$intr" == $(basename $HOME) ]]
        then
            echo -n "~/$base"
        else
            echo -n "~/$(_trim_dir $PWD)"
        fi
    else
        _trim_dir $PWD
    fi
}

I wanna add everything to my command line prompt: the Git repository, the Python virtual environment (in white), the Ruby Virtual Environment (in red) … of course, now I have no room to type commands. ;-)

export PS1='\[\e[1;34m\]$(prompt_dir)\[\e[1;32m\]$(_mygit_ps1)\[\e[0m\] \$ '

if [ -d ~/.rvm ]
then
    export PS1='\[\e[1;31m\]$(~/.rvm/bin/rvm-prompt v g)'"$PS1"
fi

Good thing I seldom use a shell.

My Function Collection

Load up my shared functions. These can be shared with Bash, Fish and Zshell.

if [ -f $HOME/.sh-funcs.sh ]
then
    . $HOME/.sh-funcs.sh
fi

Host-specific values, are stored in a separate profile.

if [ -x $HOME/.profile-local ]
then
  . $HOME/.profile-local
fi

Technical Gunk

Anything else that is interesting, will be set up in more either more shell-specific files, or in Shell Functions file. The following are the tangled settings. Type: C-c C-v t to create the script file.