aboutsummaryrefslogtreecommitdiffhomepage
path: root/zsh/zshrc
blob: a43dc2e1b6189314dec5e69742aa813c1de615d3 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# ZLE
set -o emacs

# Set cursor as blinking block
# 1 blinging block          2 block
# 3 blinking under score    4 under score
# 5 blinking bar            6 bar
echo -n '\e[1 q'

# helper repo
export SETTING_DIR=$HOME/helper
fpath=($SETTING_DIR/zsh $fpath)
source $SETTING_DIR/zsh/completion.zsh

# Reload zshrc
function .() {
    if [ $# -eq 0 ]; then
      source $ZDOTDIR/.zshrc
    else
      source $@
    fi
}

# Options
setopt extended_glob
setopt HIST_SAVE_NO_DUPS         # Do not write a duplicate event to the history file.

# Edit Command Line
autoload -Uz edit-command-line; zle -N edit-command-line
bindkey -- "^X^E" edit-command-line


# Enable completion
autoload -Uz compinit; compinit
_comp_options+=(globdots) # With hidden files
zstyle ':completion:*' menu select
zstyle ':completion::complete:*' gain-privileges 1


_get_context() {
  CONTEXT_FILE=${CONTEXT_FILE:-~/.task/context}

  LAST_MODIFY_TIME=$(stat -c %y "$CONTEXT_FILE")
  if [ ! "$MODIFY_TIME" = "$LAST_MODIFY_TIME" ]; then
    CONTEXT=$(cat "$CONTEXT_FILE")
    MODIFY_TIME="$LAST_MODIFY_TIME"
  fi
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd precmd
function precmd() {
  PROMPT='%B%(?:%F{green}%m%f:%K{red}%F{black}%m%f%k)%f%F{cyan} %c%f%b '
  # Show context and git status of tracking repos at right
  RIGHT=$(NUM=$($SETTING_DIR/bin/git/check-repos.sh -n); (( $NUM != 0 )) && echo $NUM)
  _get_context
  RPROMPT="%B%K{blue}%F{yellow}${CONTEXT:+ $CONTEXT }%f%k%K{red}%F{black}${RIGHT}%f%k%b"
  [ -n "$PRE_POPULATE" ] && print -z "$PRE_POPULATE"
}


# Directory Stack
setopt AUTO_PUSHD           # Push the current directory visited on the stack.
setopt PUSHD_IGNORE_DUPS    # Do not store duplicates in the stack.
setopt PUSHD_SILENT         # Do not print the directory stack after pushd or popd.
alias d='dirs -v'
for index ({1..9}) alias "$index"="cd +${index}"; unset index


# Create a zkbd compatible hash
# To add other keys to this hash, see: man 5 terminfo
typeset -g -A key

key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Shift-Tab]="${terminfo[kcbt]}"

bindkey --  ${key[Shift-Tab]}  reverse-menu-complete

insert-first-word () { zle insert-last-word -- -1 1 }
zle -N insert-first-word
bindkey '^[,' insert-first-word
#bindkey -- "\C-A"               beginning-of-line
#bindkey -- "\C-E"               end-of-line
#bindkey -- "\C-d"               delete-char
#bindkey -- "\C-h"               backward-delete-char
#bindkey -- "\Ed"                delete-word
#bindkey -- "\C-w"               backward-delete-word
#bindkey -- "\C-k"               kill-line
#bindkey -- "\C-p"               up-line-or-history
#bindkey -- "\C-n"               down-line-or-history
#bindkey -- "\C-b"               backward-char
#bindkey -- "\C-f"               forward-char
#bindkey -- "\Eb"                backward-word
#bindkey -- "\Ef"                forward-word
#bindkey -- "\C-v"               quoted-insert
#bindkey -- "\E."                insert-last-word
#bindkey -- "${key[PageUp]}"     beginning-of-buffer-or-history
#bindkey -- "${key[PageDown]}"   end-of-buffer-or-history
#bindkey -- "${key[Shift-Tab]}"  reverse-menu-complete
#bindkey -- "${key[Insert]}"     overwrite-mode


# Quick jump to parent folders
source $SETTING_DIR/zsh/bd.zsh


# Load my general settings
source $SETTING_DIR/bin/init/load-settings.sh


# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
	autoload -Uz add-zle-hook-widget
	function zle_application_mode_start { echoti smkx }
	function zle_application_mode_stop { echoti rmkx }
	add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
	add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi