-
Notifications
You must be signed in to change notification settings - Fork 1
/
zshrc
219 lines (160 loc) · 7.63 KB
/
zshrc
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#########################################################################################
# zsh-completions
#########################################################################################
if type brew &>/dev/null; then
FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
# Initialize the zsh completion system
# http://zsh.sourceforge.net/Doc/Release/Completion-System.html#Completion-System
autoload -Uz compinit
compinit
# case insensitive path-completion
# https://scriptingosx.com/2019/07/moving-to-zsh-part-5-completions/
zstyle ':completion:*' matcher-list \
'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' \
'm:{[:lower:][:upper:]}={[:upper:][:lower:]} l:|=* r:|=*' \
fi
source $(brew --prefix)/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=243'
ZSH_AUTOSUGGEST_STRATEGY=(history completion)
ZSH_AUTOSUGGEST_USE_ASYNC=true
MENU_COMPLETE=true
#########################################################################################
# ZSH History Settings
#########################################################################################
# https://scarff.id.au/blog/2019/zsh-history-conditional-on-command-success/
# Prevent the command from being written to history before it's
# executed; save it to LASTHIST instead. Write it to history
# in precmd.
#
# called before a history line is saved. See zshmisc(1).
function zshaddhistory() {
# Remove line continuations since otherwise a "\" will eventually
# get written to history with no newline.
LASTHIST=${1//\\$'\n'/}
# Return value 2: "... the history line will be saved on the internal
# history list, but not written to the history file".
return 2
}
# zsh hook called before the prompt is printed. See zshmisc(1).
function precmd() {
# Write the last command if successful, using the history buffered by
# zshaddhistory().
if [[ $? == 0 && -n ${LASTHIST//[[:space:]\n]/} && -n $HISTFILE ]] ; then
print -sr -- ${=${LASTHIST%%'\n'}}
fi
}
# History location.
export HISTFILE=~/.zsh_history
# It’s not possible to set the history to an unlimited size in zsh. The max
# history size can be the value of LONG_MAX variable from limits.h header,
# which is 9223372036854775807.
export HISTFILESIZE=9223372036854775800
export HISTSIZE=9223372036854775800
# Maximum number of items for the history file
export SAVEHIST=9223372036854775800
# The meaning of these options can be found in man page of `zshoptions`.
setopt HIST_IGNORE_ALL_DUPS # do not put duplicated command into history list
setopt HIST_SAVE_NO_DUPS # do not save duplicated command
setopt HIST_REDUCE_BLANKS # remove unnecessary blanks
setopt INC_APPEND_HISTORY_TIME # append command to history file immediately after execution
# Append history without exiting shell.
setopt inc_append_history
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
# Add Timestamp to history.
setopt extended_history
# When searching, skip duplicates and show each command only once.
setopt hist_find_no_dups
# To retrieve the history file everytime history is called upon. The only
# caveat is the need to press enter to fetch history.
setopt share_history
#########################################################################################
# ShellHistory App
#########################################################################################
# Adding shhist to PATH, so we can use it from Terminal
PATH="${PATH}:/Applications/ShellHistory.app/Contents/Helpers"
# creating an unique session id for each terminal session
__shhist_session="${RANDOM}"
# prompt function to record the history
__shhist_prompt() {
local __exit_code="${?:-1}"
\history -D -t "%s" -1 | sudo --preserve-env --user ${SUDO_USER:-${LOGNAME}} shhist insert --session ${TERM_SESSION_ID:-${__shhist_session}} --username ${LOGNAME} --hostname $(hostname) --exit-code ${__exit_code} --shell zsh
return ${__exit_code}
}
# integrating prompt function in prompt
precmd_functions=(__shhist_prompt $precmd_functions)
#########################################################################################
# Powerlevel10k
#########################################################################################
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
source $(brew --prefix)/share/powerlevel10k/powerlevel10k.zsh-theme
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f $HOME/.p10k.zsh ]] || source $HOME/.p10k.zsh
typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet
#########################################################################################
# Aliases
#########################################################################################
alias .dotfiles="cd $HOME/Dotfiles && nvim -c ':Neotree'"
alias .brewrc="nvim $HOME/Dotfiles/Brewfile"
alias .gitconfig="nvim $HOME/Dotfiles/git/config"
alias .nvimrc="nvim $HOME/Dotfiles/nvim/init.lua"
alias .zshrc="nvim $HOME/Dotfiles/zshrc"
alias .history="nvim $HOME/.zsh_history"
alias nvim-lazy="NVIM_APPNAME=nvim-lazy nvim"
# Show all the history stored with fzf.
alias history="fc -l 1 | fzf"
#########################################################################################
# Environment Variables
#########################################################################################
export XDG_CONFIG_HOME="$HOME/.config"
export HOMEBREW_BUNDLE_FILE="$HOME/Dotfiles/Brewfile"
export LANG="en_US.UTF-8"
export EDITOR="nvim"
export PATH="$PATH:$HOME/.exo/bin" # https://github.com/deref/exo
source "$HOME/Documents/Dotfiles/env-vars.sh"
#########################################################################################
# Functions
#########################################################################################
function rubocop_changes() {
git status --porcelain | sed s/^...// | xargs rubocop
}
function bundle_rubocop_changes() {
git status --porcelain | sed s/^...// | xargs bundle exec rubocop
}
function rz() {
echo "sourcing ~/.zshrc"
source ~/.zshrc
}
function fixadguard() {
sudo ifconfig lo0 up
}
#########################################################################################
# Homebrew
#########################################################################################
# Export these to your path because doctor said so! :)
export PATH="/usr/local/opt/openjdk/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
#########################################################################################
# Other
#########################################################################################
# Add fzf to shell
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# iTerm2's Shell Integration
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
# Add direnv to shell
eval "$(direnv hook zsh)"
# Add https://github.com/ajeetdsouza/zoxide
eval "$(zoxide init zsh)"
#########################################################################################
# asdf - MUST BE LAST
#########################################################################################
# Get all available language versions rather than waiting someone to bump it
# https://github.com/asdf-vm/asdf-ruby/commit/af80345be901838ce2c6a58c6536a6fccc573b91
export ASDF_RUBY_BUILD_VERSION=master
# Add to shell
. $(brew --prefix asdf)/libexec/asdf.sh