dot-zsh/zshrc -rw-r--r-- 2.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Install oh-my-zsh-git (AUR)
ZSH='/usr/share/oh-my-zsh/'

setopt hist_ignore_all_dups
setopt hist_find_no_dups

CASE_SENSITIVE="true"
COMPLETION_WAITING_DOTS="true"

plugins=(
    # auto complete with sudo
    sudo
    # better man pages with colors
    colored-man-pages
    # auto complete with git
    git
    # managing history
    per-directory-history
    history-substring-search
)

source "${ZSH}/oh-my-zsh.sh"

# configure ↑ and ↓ to search zsh history
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

# This will prompt the return code of the last command iff it is not 0
# (ie. something went wrong with the last command).
function precmd {
    local last_ret="$?"

    if [[ ${last_ret} -ne 0 ]]; then
        echo "\033[0;33m→ ${last_ret}"
    fi
}

# This will echo the right prompt format string.
#
# If the current directory is inside a git repository, it will print the root
# folder name, followed by the relative path within the repository, followed by
# the branch name.
#
# Otherwise, it just output the current directory.
function get_rprompt {
    if [[ -n $(current_branch) ]]; then
        echo -n "%{%12F%}$(basename `git rev-parse --show-toplevel`):%{%f%}"
        echo -n "%{%11F%}/$(git rev-parse --show-prefix | sed 's:/*$::')%{%f%}"
        if [[ $(git status | grep "nothing to commit") ]]; then
            echo -n " %{%2F%}($(current_branch))%{%f%}"
        else
            echo -n " %{%1F%}($(current_branch))%{%f%}"
        fi
    else
        echo -n "%{%12F%}$(pwd)%{%f%}"
    fi
}

PROMPT='%{%12F%}λ %{%f%}'
RPROMPT='$(get_rprompt)'

# Start SSH agent in order to type passphrases only once
SSH_ENV="${HOME}/.ssh/environment"

function start_agent {
    echo -n "Initialising new SSH agent... "
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo "succeeded"
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

# Opam configuration
test -r "${HOME}/.opam/opam-init/init.zsh" && . "${HOME}/.opam/opam-init/init.zsh" > /dev/null 2> /dev/null || true