How to activate local history on ZSH by default

With zsh, particularly with oh-my-zsh, the default history is global i.e. shared by all shells. This can often be weird if you are working with multiple terminal sessions and expect each session to have its own history.

In order to fix this, append the following to your ~/.zshrc file:

# Local history - https://superuser.com/a/691603/285486

bindkey "${key[Up]}" up-line-or-local-history
bindkey "${key[Down]}" down-line-or-local-history

up-line-or-local-history() {
    zle set-local-history 1
    zle up-line-or-history
    zle set-local-history 0
}
zle -N up-line-or-local-history
down-line-or-local-history() {
    zle set-local-history 1
    zle down-line-or-history
    zle set-local-history 0
}
zle -N down-line-or-local-history

bindkey "^[[1;5A" up-line-or-history    # [CTRL] + Cursor up
bindkey "^[[1;5B" down-line-or-history  # [CTRL] + Cursor down