Skip to content

macOS dotfiles

dotfiles on my mac

Note: I first had the dotfiles set up with Bash. Then I moved to zsh, when Apple decided that it will default to zsh in the future.

Current Setup : zsh

TODO: update here

Prior Setup : Bash

The system is simple:

  • .bash_profile => loads .bashrc
  • .bashrc => loads the rest
  • the other dotfiles are in a root-level folder called dotfiles

.bash_profile:

    ######################################################################
    #
    #   THIS FILE LOADS .bashrc
    #
    #
    ######################################################################

    # Load .bashrc if it exists
    test -f ~/.bashrc && source ~/.bashrc

.bash_rc:

    ######################################################################
    #
    #   MAIN FILE WITH CONFIGURATION FOR THE TERMINAL PROMPT
    #
    #
    ######################################################################


    # Load .env if it exists
    test -f ~/dotfiles/.env && source ~/dotfiles/.env

    # Load .aliases if it exists
    test -f ~/dotfiles/.aliases && source ~/dotfiles/.aliases

    # Load .prompt if it exists
    test -f ~/dotfiles/.prompt && source ~/dotfiles/.prompt

    # Load .functions if it exists
    test -f ~/dotfiles/.functions && source ~/dotfiles/.functions

    # Load .venvsconf if it exists
    test -f ~/dotfiles/.venvsconf && source ~/dotfiles/.venvsconf

.env:

    ######################################################################
    #
    #   ENVIRONMENT VARIABLES
    #
    #
    ######################################################################


    # Set vim useability in the terminal, when unchecked
    #set -o vi

    # Set the PATH
    # Ensure user-installed binaries take precedence
    # Removed /usr/local/bin here and added it to /etc/paths file
    # export PATH=$PATH
    # export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin
    export PATH=/usr/local/git/bin:$PATH

    # Set architecture flags
    export ARCHFLAGS="-arch x86_64"

    # Make the terminal UTF-8 aware:
    #export LC_CTYPE=en_US.UTF-8
    export LC_ALL=en_GB.UTF-8
    export LANG=en_GB.UTF-8


    # Save all bash history
    #export HISTTIMEFORMAT="%s "
    export HISTTIMEFORMAT="%F %T "

    # Add [Homebrew-installed] nvm (node version manager) to source
    source $(brew --prefix nvm)/nvm.sh

    # add nvm bash-completion
    [[ -r $NVM_DIR/bash_completion ]] && . $NVM_DIR/bash_completion


    # Add tab completion for many Bash commands
    if which brew > /dev/null && [ -f "$(brew --prefix)/etc/bash_completion" ]; then
    . "$(brew --prefix)/etc/bash_completion";
    elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion;
    fi;


    # Make Tab autocomplete regardless of filename case
    set completion-ignore-case on

    # List all matches in case multiple possible completions are possible
    set show-all-if-ambiguous on


    # Be more intelligent when autocompleting by also looking at the text after
    # the cursor. For example, when the current line is "cd ~/src/mozil", and
    # the cursor is on the "z", pressing Tab will not autocomplete it to "cd
    # ~/src/mozillail", but to "cd ~/src/mozilla". (This is supported by the
    # Readline used by Bash 4.)
    set skip-completed-text on

    # git editor
    export EDITOR="subl3 -w"

.aliases:

    ######################################################################
    #
    #   ALIASES
    #
    #
    ######################################################################

    # filesystem, navigation, terminal
    alias path='echo $PATH | tr ":" "\n" | sort'    # print $path nicely

    alias clr='clear;echo "Currently logged in on $(tty), as $(whoami) in directory $(pwd)."'
    alias cls='clr'                                 # clean screen

    alias ..="cd ../"
    alias ...="cd ../../"

    alias lsa="ls -a"
    alias lsl="ls -l"

    alias reloadbash=". ~/.bash_profile"
    alias sourcebash="source ~/.bashrc"


    # helpers (defaults)
    alias grep='grep --color=auto'                  # color grep matches
    alias count='wc -l'                             # count lines
    alias size='du -sh'                             # get folder size
    alias sizer='du -h -c'                          # get and print folder size for all folders, recursively
    alias disks='df -H -l'                          # show available disk space
    alias ip='ifconfig | grep "inet "'              # quickly print ip address
    alias ping='ping -c 5'                          # pings with 5 packets, not unlimited


    # subl
    #alias subl='/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl'
    alias hosts='subl /private/etc/hosts'           # shortcut for editing hosts file
    alias dotfiles='subl ~/dotfiles'


    # mac helpers
    alias show="defaults write com.apple.Finder AppleShowAllFiles YES && killall Finder"
    alias hide="defaults write com.apple.Finder AppleShowAllFiles NO && killall Finder"
    alias fixopenwith='/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user | killall Finder'    # Quick way to rebuild the Launch Services database and get rid of duplicates in the Open With submenu.
    alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl"
    alias cleanup="find . -type f -name '*.DS_Store' -ls -delete"
    alias updateall='sudo softwareupdate -i -a; brew update; brew upgrade; brew cleanup; npm update npm -g; npm update -g; sudo gem update --system; sudo gem update'
    alias updatebrew='brew update; brew upgrade; brew cleanup'


    # misc
    alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'


    # git
    alias gs='git status'
    alias ga='git add -A'
    #alias gp='git push origin HEAD'
    alias gd='git difftool'
    alias gc='git commit -m'
    #alias gl='git log -n 20 --format="%ai  %Cgreen[%h]%Creset  [%<(12,trunc)%aN]  %s"'


    # vagrant
    alias vi='vagrant init ubuntu/trusty64'
    alias vu='vagrant up'
    alias vp='vagrant provision'
    alias vpshell='vagrant provision --provision-with shell'
    alias vr='vagrant reload'
    alias vs='vagrant status'
    alias vd='vagrant destroy -f'
    alias vsus='vagrant suspend'
    alias vssh='vagrant ssh'
    alias vgs='vagrant global-status'


    # curl
    alias ie6curl='curl -H "User-Agent: Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"'
    alias ffcurl='curl -H "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.0 (.NET CLR 3.5.30729)"'
    alias chcurl='curl -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.19 Safari/537.36"'
    alias gbcurl='curl -H "User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"'


    # APACHE & MySQL
    alias restartApache="sudo apachectl restart"
    alias startApache="sudo apachectl start"
    alias stopApache="sudo apachectl stop"
    alias mysql-stop='launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist'
    #alias mysql-start='launchctl load -w -F ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist'
    alias mysql-start='launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist'


    # PYTHON SIMPLE_SERVER
    alias sserver='open http://localhost:8000 && python -m SimpleHTTPServer'


    # iPython Notebook Server (first activate the VirtualEnv)
    alias ipynb="ipython notebook"
    alias ipyff="ipython notebook --browser=Firefox"
    alias ipyqt="ipython qtconsole"


    # FOLDER SHORTCUTS
    alias home="cd /Users/ska/"
    alias desk="cd ~/Desktop/"
    alias venvs="cd ~/Virtualenvs/"
    alias vag='cd ~/Vagrants'
    alias dcs='cd ~/Documents'
    alias dl='cd ~/Downloads'
    alias dp='cd ~/Dropbox'
    alias sites='cd ~/Sites'

    # SHORTCUTS TO VIRTUAL ENVIRONMENTS
    alias gaev='cd ~/Vagrants/gaepw02/vagrant'
    alias flask1='cd ~/Virtualenvs/flask01/startapp/app'
    alias flask2='cd ~/Virtualenvs/flask02/'
    alias flask2a='cd ~/Virtualenvs/flask02/microblog'
    alias vflask2a='workon flask02'

.prompt:

    ######################################################################
    #
    #   PROMPT SETTINGS INCLUDING
    #   COLOURFUL PROMPT
    #
    ######################################################################

    # Prompt settings
    PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND ; }"'echo $$ $USER \
                "$(history 1)" >> ~/.bash_eternal_history'

    # apply thousands separator to file sizes
    BLOCK_SIZE=\'1; export BLOCK_SIZE

    # Colorful Bash Prompt, inspired by "Extravagant Zsh Prompt"
    # Screenshot: http://img.gf3.ca/d54942f474256ec26a49893681c49b5a.png
    # A big thanks to \amethyst on Freenode

    if [[ $COLORTERM = gnome-* && $TERM = xterm ]]  && infocmp gnome-256color >/dev/null 2>&1; then export TERM=gnome-256color
    elif infocmp xterm-256color >/dev/null 2>&1; then export TERM=xterm-256color
    fi

    if tput setaf 1 &> /dev/null; then
        tput sgr0
        if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
        MAGENTA=$(tput setaf 9)
        ORANGE=$(tput setaf 172)
        GREEN=$(tput setaf 190)
        PURPLE=$(tput setaf 141)
        WHITE=$(tput setaf 256)
        else
        MAGENTA=$(tput setaf 5)
        ORANGE=$(tput setaf 4)
        GREEN=$(tput setaf 2)
        PURPLE=$(tput setaf 1)
        WHITE=$(tput setaf 7)
        fi  
        BOLD=$(tput bold)
        RESET=$(tput sgr0)
    else
        MAGENTA="\033[1;31m"
        ORANGE="\033[1;33m"
        GREEN="\033[1;32m"
        PURPLE="\033[1;35m"
        WHITE="\033[1;37m"
        BOLD=""
        RESET="\033[m"
    fi

    parse_git_dirty () {
    [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
    }
    parse_git_branch () {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
    }

    # PROMPT" "PS1" SETTINGS:

    # REMOVED THE BOLD
    PS1="\[${MAGENTA}\]\u \[\e[37m\]at \[$ORANGE\]\h \[\e[37m\]in \[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" on \")\[$PURPLE\]\$(parse_git_branch)\[\e[37m\]\n\$ ~ >> \[$RESET\]"


    # Always use color output for `ls`
    if [[ "$OSTYPE" =~ ^darwin ]]; then
    alias ls="command ls -G"
    export LSCOLORS=gxfxcxdxbxegedabagaced
    else
    alias ls="command ls --color"
    export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
    fi

.functions:

    ######################################################################
    #
    #   SHELL FUNCTIONS
    #
    #
    ######################################################################

    # Starter template for web page project
    function starterTemplate() {
      git clone https://github.com/eltioska/My-Starting-Template.git $@
    }

    # Kills any process that matches a regexp passed to it
    killit() {
      ps aux | grep -v "grep" | grep "$@" | awk '{print $2}' | xargs sudo kill
    }

    # Open man page as PDF
    function manpdf() {
     man -t "${1}" | open -f -a /Applications/Preview.app/
    }

.venvsconf:

    ######################################################################
    #
    #   CONFIGURATION FOR PYTHON AND VIRTUALENV(WRAPPER)
    #
    #
    ######################################################################

    # Pip should only run if there is a virtualenv currently activated
    export PIP_REQUIRE_VIRTUALENV=true

    # Cache pip-installed packages to avoid re-downloading
    export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

    # Create a command to install into global packages
    # Python 2
    syspip(){
       PIP_REQUIRE_VIRTUALENV="" pip "$@"
    }

    # Create a command to install into global packages
    # Python 3
    syspip3(){
       PIP_REQUIRE_VIRTUALENV="" pip3 "$@"
    }

    # Set the virtualenvwrapper HOME directory
    export WORKON_HOME=~/Virtualenvs

    # Load the virtualenvwrapper script
    if [[ -r /usr/local/bin/virtualenvwrapper.sh ]]; then
        source /usr/local/bin/virtualenvwrapper.sh
    else
        echo "WARNING: Can't find virtualenvwrapper.sh"
    fi

A Sample Bash Dotfiles setup

Note

THIS SEEMS LIKE A GOOD BASH SETTING
Originally at: http://github.com/gf3/dotfiles/blob/master/bash_prompt

Basically have a short .bash_profile which contains links to other files with specific settings.

The setup goes like this:

FILES

FILE LINKS TO
.bash_profile .bash_prompt
" " .commonrc
.bash_prompt nothing
.commonrc .aliases
" " .functions
" " ..scripts

FOLDERS: .scripts folder contains scripts and is added to PATH

.bash_profile

.bash_profile has 2 main links: - file called .bash_prompt for colourful prompt settings - file called .commonrc for setting other stuff

    # Prompt
    [[ -f "$HOME/.bash_prompt" ]] && source "$HOME/.bash_prompt"

    # Larger bash history (default is 500)
    export HISTFILESIZE=10000
    export HISTSIZE=10000

    PATH="/usr/local/bin:/usr/local/sbin:$PATH"

    # Common junk
    [[ -s "$HOME/.commonrc" ]] && source "$HOME/.commonrc"

.commonrc:

.commonrc sets the main settings: - has a couple of links to other files such as .aliases and .functions - also has other settings (not all of which interest me but copied whole gist here)

    # Load things
    [[ -f "$HOME/.aliases" ]] && source "$HOME/.aliases"

    [[ -f "$HOME/.functions" ]] && source "$HOME/.functions"

    # Adjust $PATH
    [[ -d "$HOME/.scripts" ]] && export PATH=$HOME/.scripts:$PATH

    [[ -d "$HOME/.lein/bin" ]] && export PATH=$HOME/.lein/bin:$PATH

    # Locale
    export LC_ALL=en_US.UTF-8  
    export LANG=en_US.UTF-8

    # Misc settings
    export EDITOR=vim

    # rbenv
    if [ -d "$HOME/.rbenv/bin" ] ; then 
      export PATH="$HOME/.rbenv/bin":$PATH
      eval "$(rbenv init -)"
    fi

    # rvm
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

.aliases

.aliases sets the aliases (not all of which interest me but copied whole gist here)

    # Dir navigation
    alias ..='cd ..'
    alias ...='cd ../..'
    alias ....='cd ../../..'
    alias ~='cd ~'
    alias ll='tree --dirsfirst -ChFupDaLg 1'

    # Utilities
    alias a="ack -ia"
    alias b="bundle exec"
    alias c="pygmentize -O style=monokai -f console256 -g"
    alias d="du -h -d=1"
    alias df="df -h"
    alias g="git"
    alias grep='GREP_COLOR="1;37;45" LANG=C grep --color=auto'
    alias h="history"
    alias httpdump="sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E \"Host\: .*|GET \/.*\""
    alias ip="curl -s http://checkip.dyndns.com/ | sed 's/[^0-9\.]//g'"
    alias localip="ipconfig getifaddr en1"
    alias mp="mvim -p"
    alias rkt="racket -il xrepl"
    alias tmux="tmux -2"
    alias view="vim -p -R"
    alias vp="vim -p"

    # Sync
    alias sync_irc="rsync -avz ~/Sites/IRC/ talos:/mnt/www/irc.gf3.ca/public"

.functions

.functions sets some commonly used bash functions (not all of which interest me but copied whole gist here)

    # Image width
    width () {
      echo $(sips -g pixelWidth $1 | grep -oE "[[:digit:]]{1,}$")
    }

    # Image height
    height () {
      echo $(sips -g pixelHeight $1 | grep -oE "[[:digit:]]{1,}$")
    }

    # All the dig info, 'cause I can never remember it
    digga () {
      dig +nocmd $1 any +multiline +noall +answer
    }

    # Copy w/ progress
    cp_p () {
      rsync -WavP --human-readable --progress $1 $2
    }

    # Pretty print JSON
    cjson () {
      local url=$(echo $1)
      if [[ "http" == $url[0,4] ]] ; then
        curl --silent $url | python -mjson.tool | pygmentize -O style=monokai -f console256 -g
      else
        cat $url | python -mjson.tool | pygmentize -O style=monokai -f console256 -g
      fi
    }