Dotfiles: zsh

I've been a bash user for over 4 years and I needed a change. I tried zsh and I felt in love with whis shell in the fist 10 minutes.

It is awesome. One of the things that made me change so quickly is that zsh seems to have all the funcionality that bash have, and a lot more. That's way it hasn't been a pain swtiching from one to the other. I've just have to spend a few hours migrating the configuration files and everything is working like always, but with more power under my finger.

.zshrc

I've seen different approaches of the way you can manage the zshrc file, but the one I liked most is to have a clean .zshrc file. In zsh you can configure a lot of stuff and have all the configurations in the same file is not as useful as it may seems. And now that I have bash and zsh in the same system, is a good idea to have separate files for each thing because some parts can be shared among shells.

autoload -U compinit promptinit
compinit
promptinit
prompt walters


. ~/.shell/zsh_default
. ~/.shell/variables
. ~/.shell/alias
. ~/.shell/completions
. ~/.shell/functions


# Custom zsh alias
alias rld="source ~/.zshrc"
alias conf="vim ~/.zshrc"

As you can see there is nothing to see here. I tried the walters prompt and I liked it. I store all the files under the ~/.shell directory

.zsh_default

This file is a mixture of the default configuration that came with zsh and some minor configurations commented inline.

setopt histignorealldups

# Use emacs keybindings even if our EDITOR is set to vi
bindkey -e

# Keep 10000 lines of history within the shell and save it to ~/.zsh_history:
HISTSIZE=10000
SAVEHIST=10000
HISTFILE=~/.zsh_history

# Use modern completion system
autoload -Uz compinit
compinit

zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' menu select=long
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true

zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'

# CUSTOM
setopt autocd
setopt no_beep
autoload -Uz compinit compinit

# Bash-like navigation (Cntl-Left/Right moves only one word at a time)
autoload -U select-word-style
select-word-style bash

# Cntl-Left/Right moves only one word at a time
bindkey ";5C" forward-word
bindkey ";5D" backward-word


# Remove-auto-created-directories-don't-know-who-do-it-but-is-very-annoying :)
[ -d "sketchbook" ] && rmdir sketchbook
[ -d "Desktop" ] && rmdir Desktop

# Why the fuck this sed at the end?
export LS_COLORS=`echo $LS_COLORS  | sed 's/01;/00;/g'`

export GREP_OPTIONS='--color=always'
export LESS="-R"

.variables

Simple file shared with bash with globals.

export LANG_LAYOUT='es'
export EDITOR='vim'
export VISUAL=$EDITOR
export DEITY=diox

.alias

I love aliases, and I use them a lot (even more now with zsh!)

Basic

Mostyl the aliases help me to "fix" the typos I make.

alias sl="ls --group-directories-first --color"
alias ls="ls --group-directories-first --color"
alias l="ls --group-directories-first --no-group --color"
alias la="ls -lhatr --no-group --color"
alias vi=vim
alias vm=vim
alias im=vim
alias cim=vim
alias vom=vim
alias v=vim
alias moc=mocp
alias mopc=mocp

Launching apps

alias redshift="redshift -l 39.60:2.93  -g 0.8 -g 1.5 -m vidmode &"
alias firefox="/opt/firefox/firefox"
alias thunderbird="/opt/thunderbird/thunderbird"

Utils

alias tsyslog="tailf /var/log/syslog"    # Not typing always the same over and over again
alias rm="rm -I"                         # Confirm the deletion of the files
alias p="ping tuenti.es -c 4"            # Check if I have internet (very very used)
alias pp="ping tuenti.es -c 200"         # Same, but much more requests
alias dt="date '+%F %H:%M'"              # Give me the date
alias tmp="mkdir -p /tmp/lala && cd /tmp/lala"  # Creates me a directory where I can play but is not /tmp
# Get my external IP
alias ipext="curl --silent checkip.dyndns.org | grep --extended-regexp --only-matching '[0-9\.]+'"

Completions

Bash don't have completions and I really really love this feature from zsh.

Bind programs to extensions

alias -s pdf=evince
alias -s zip=unzip rar="rar x"
alias -s tar.gz=tar zvxf
alias -s ods=libreoffice3.6
alias -s jpg="feh -F" gif="feh -F" png="feh -F"

"Inline" aliases

These are awesome too. I save a lot of typing with this aliases.

alias -g C=' | wc -l '
alias -g L=' | less '
alias -g G=' | grep -iI --color=always '
alias -g H='| head'
alias -g NUL="2> /dev/null"

.functions

The extract function is very useful but I strongly recommend you to take a look to the last one. It defines the behaviour of the keybinding Alt-U or Esc-U: move one directory up. Reeeeaaaally useful. When you get use to it, you cannot live without it.

# Useful functions
extract () {
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)        tar xjf $1        ;;
            *.tar.gz)         tar xzf $1        ;;
            *.bz2)            bunzip2 $1        ;;
            *.rar)            rar x $1        ;;
            *.gz)             gunzip $1         ;;
            *.tar)            tar xf $1         ;;
            *.tbz2)           tar xjf $1        ;;
            *.tgz)            tar xzf $1        ;;
            *.zip)            unzip $1          ;;
            *.Z)              uncompress $1     ;;
            *)                echo "'$1' cannot be extracted via extract()" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

dgrep() {
    # A recursive, case-insensitive grep that excludes binary files
    grep -iIR "$@" *
}

psgrep() {
    if [ ! -z $1 ] ; then
        ps aux | grep $1 | grep -v grep
    else
        echo "!! Need name to grep for"
    fi
}


### This allows me to do Esc-U or Alt-U and automatically execute the "cd .." command
### Very very useful went you get use to it
	function up_directory() {
	  BUFFER="cd .."
	  zle accept-line
	}
	# Define a widget called "up_directory", mapped to our function above.
	zle -N up_directory
	# Bind it to ESC-u // Alt-u
	bindkey "^[u" up_directory