Skip to content

Instantly share code, notes, and snippets.

@marzocchi marzocchi/zshrc

Last active May 21, 2020
Embed
What would you like to do?
zshrc
# vim: se filetype=zsh
autoload -U add-zsh-hook
TZ="Europe/Rome"
HISTFILE=$HOME/.zhistory
HISTSIZE=3000
SAVEHIST=3000
HOSTNAME="`hostname`"
export EDITOR='vim'
# setopt NOHUP #don't kill bg processes on exit
setopt NOTIFY \
APPEND_HISTORY \
INC_APPEND_HISTORY \
SHARE_HISTORY \
CORRECT \
EXTENDED_HISTORY \
HIST_ALLOW_CLOBBER \
HIST_REDUCE_BLANKS \
MENUCOMPLETE \
AUTO_PARAM_SLASH \
EXTENDED_GLOB \
RC_QUOTES \
MAILWARNING \
HIST_IGNORE_DUPS \
NOCLOBBER \
LONG_LIST_JOBS \
AUTO_CD \
GLOB_DOTS \
CDABLE_VARS \
AUTO_LIST \
PROMPT_SUBST \
NO_LIST_BEEP # makes completion less annoying
# Escape URL's special chars (eg.: ?)
autoload -U url-quote-magic
zle -N self-insert url-quote-magic
# FTP functions
zstyle ':zftp:*' progress percent
autoload -U zfinit
zfinit
bindkey -v
# Backspace works as expected even in vi mode
bindkey '^?' backward-delete-char
# Use the nicer incremental search in vim mode
bindkey -M vicmd '/' history-incremental-search-backward
# Unbind "space" in list, I use it to select a match from the completion menu and
# continue typing
# bindkey -M listscroll -r ' '
# ESC-v opens command line in vim
autoload -U edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line
zmodload -i zsh/complist
setopt menu_complete
setopt auto_menu
setopt complete_in_word
setopt always_to_end
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' list-colors ''
bindkey -M menuselect '^o' accept-and-infer-next-history
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
zstyle ':completion:*:*:*:*:processes' command "ps -u `whoami` -o pid,user,comm -w -w"
# use only known_hosts for hostname completion
# [ -r ~/.ssh/known_hosts ] && _ssh_hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[\|]*}%%\ *}%%,*}) || _ssh_hosts=()
# hosts=(
# "$_ssh_hosts[@]"
# )
#
# use only explictly host names from Host directives in ~/.ssh/config
hosts=($(grep "Host " ~/.ssh/config | cut -c 6- | xargs))
zstyle ':completion:*:hosts' hosts $hosts
# Use caching so that commands like apt and dpkg complete are useable
zstyle ':completion::complete:*' use-cache 1
zstyle ':completion::complete:*' cache-path ~/.oh-my-zsh/cache/
# Don't complete uninteresting users
zstyle ':completion:*:*:*:users' ignored-patterns \
adm amanda apache avahi beaglidx bin cacti canna clamav daemon \
dbus distcache dovecot fax ftp games gdm gkrellmd gopher \
hacluster haldaemon halt hsqldb ident junkbust ldap lp mail \
mailman mailnull mldonkey mysql nagios \
named netdump news nfsnobody nobody nscd ntp nut nx openvpn \
operator pcap postfix postgres privoxy pulse pvm quagga radvd \
rpc rpcuser rpm shutdown squid sshd sync uucp vcsa xfs
zstyle ':completion:*' menu select
zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format '%B%d%b'
autoload -U compinit
compinit -i
# Simple two lines prompt, with minimal git info.
#
# Looks something like:
#
# federico@Bubastis:oh-my-zsh/themes on master*
# $
#
autoload colors; colors;
# Defaults, override in .zshenv
[[ $MRZ_HOSTNAME_COLOR == "" ]] && MRZ_HOSTNAME_COLOR=magenta
[[ $MRZ_USERNAME_COLOR == "" ]] && MRZ_USERNAME_COLOR=red
[[ $MRZ_ROOT_COLOR == "" ]] && MRZ_ROOT_COLOR=red
[[ $MRZ_PATH_COLOR == "" ]] && MRZ_PATH_COLOR=yellow
[[ $MRZ_SHOW_HOSTNAME == "" ]] && MRZ_SHOW_HOSTNAME=true
[[ $MRZ_HOSTNAME == "" ]] && MRZ_HOSTNAME="%m"
function git-repo-path() {
local paths
local ok
paths=($(git rev-parse --show-toplevel --show-prefix 2>&1)) || return 1
ok=$?
echo -n `basename $paths[1]`
[[ $ok == 0 ]] && echo -n "/"$paths[2] | sed "s:/$::"
}
function mrz-get-git-stats() {
[[ "$MRZ_DISABLE_GIT_PROMPT" == "true" ]] && return 1
local branch repo_path dirty
branch=$(git symbolic-ref HEAD 2>/dev/null | cut -c 12-) || return 10
echo $branch
repo_path=`git-repo-path 2>/dev/null` || return 20
echo $repo_path
dirty=`git-dirty 2>/dev/null` || return 30
echo $dirty
}
# Print repository path + branch name + ZSH_THEME_GIT_PROMPT_DIRTY
# if in a git repository, or pwd.
function mrz-get-pwd() {
local stats
stats=($(mrz-get-git-stats))
if [ "$stats" != "" ]; then
print -P "$fg[$MRZ_PATH_COLOR]$stats[2]$reset_color on $fg[black]$stats[3]$reset_color$fg[$MRZ_PATH_COLOR]$stats[1]$reset_color"
else
print -P "$fg[$MRZ_PATH_COLOR]%6~$reset_color"
fi
}
function mrz-get-user-host() {
local user
local hostname
if [[ $USER == "root" ]]; then
user="$fg[$MRZ_ROOT_COLOR]%n$reset_color"
else
user="$fg[$MRZ_USERNAME_COLOR]%n$reset_color"
fi
[[ $MRZ_SHOW_HOSTNAME == true ]] && hostname="@$fg[$MRZ_HOSTNAME_COLOR]$MRZ_HOSTNAME$reset_color"
print -P "$user$hostname"
}
function mrz-print-header() {
local user
local dir
user=`mrz-get-user-host`
dir=`mrz-get-pwd`
print -P "$user$hostname:$dir"
}
autoload -U vcs_info
add-zsh-hook precmd mrz-print-header
PS1="%(?..(%?%) )%{${fg[red]}%}%(!.#.$)%{$reset_color%} "
function browse-with-ranger() {
# Absolute path to relative, from:
# http://stackoverflow.com/questions/2564634/bash-convert-absolute-path-into-relative-path-given-a-current-directory
function relpath(){
python -c "import os.path; print os.path.relpath('$1','${2:-$PWD}')"
}
function shortest-path() {
local abs rel abs_len rel_len
abs="$1"
rel=`relpath "$abs"`
abs_len=`echo "$abs" | wc -c`
rel_len=`echo "$rel" | wc -c`
if [[ $abs_len -gt $rel_len ]]; then
echo $rel
else
echo $abs
fi
}
local str tmp_file1 tmp_file2 file dir
tmp_file1=`mktemp "$TMPDIR"/ranger-chosen-dir.XXXX`
tmp_file2=`mktemp "$TMPDIR"/ranger-chosen-file.XXXX`
exec </dev/tty
ranger --choosedir="$tmp_file1" --choosefile="$tmp_file2"
dir=`cat "$tmp_file1"`
file=`cat "$tmp_file2"`
rm "$tmp_file1"
rm "$tmp_file2"
if [[ "$file" != "" ]]; then
str=`shortest-path "$file"`
elif [[ "$dir" != "" && "$dir" != `pwd` ]]; then
str=`shortest-path "$dir"`
else
zle reset-prompt
return
fi
if [[ $str != "" ]]; then
LBUFFER="$LBUFFER${(q)str}"
fi
zle reset-prompt
}
zle -N browse-with-ranger
bindkey -M viins '^o' browse-with-ranger
if tmux has 2>/dev/null; then
sessions=(`tmux ls -F "#{session_name}"`)
echo Tmux sessions: ${(j:, :)sessions}
fi
export EDITOR=vim
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
export GOPATH="$HOME/projects/go"
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1
path=(
/Applications/terminal-notifier.app/Contents/MacOS
~/.local/bin
"$GOPATH/bin"
~/bin
$path
)
setopt CSH_NULL_GLOB
if [ -d "$HOME/.zsh.d" ]; then
for f in "$HOME"/.zsh.d/*.plugin.zsh $HOME/.zsh.d/*/*.plugin.zsh;
do
source "$f"
done
fi
unsetopt CSH_NULL_GLOB
zmodload zsh/terminfo
bindkey '' history-substring-search-up
bindkey '' history-substring-search-down
bindkey -M vicmd 'k' history-substring-search-up
bindkey -M vicmd 'j' history-substring-search-down
if [[ -f "$HOME"/.iterm2_shell_integration.zsh ]]; then
source "$HOME"/.iterm2_shell_integration.zsh
fi
if [[ -f "$HOME"/.nix-profile/etc/profile.d/nix.sh ]]; then
. "$HOME"/.nix-profile/etc/profile.d/nix.sh
fi
if command -v direnv > /dev/null; then
eval "$(direnv hook zsh)"
function direnv-reload() {
test -f "$PWD"/.envrc && direnv reload
}
add-zsh-hook precmd direnv-reload
fi
if [ -f "$HOME/.iterm-notify-init.sh" ]; then
source "$HOME/.iterm-notify-init.sh"
iterm-notify config-set logger-name "zsh $PID"
iterm-notify config-set failure-title "wow such #fail ({duration_seconds:d}s)"
iterm-notify config-set success-title "very #success. wow ({duration_seconds:d}s)"
iterm-notify config-set - <<'FOO'
notifications-backend terminal-notifier
command-complete-timeout 5
failure-icon https://media3.giphy.com/media/10ECejNtM1GyRy/200_s.gif
success-icon https://s-media-cache-ak0.pinimg.com/564x/b5/5a/18/b55a1805f5650495a74202279036ecd2.jpg
FOO
fi
function dict-to-json() {
cat | python -c 'import json; import sys; print(json.dumps(eval(sys.stdin.read())))'
}
function json-to-dict() {
cat | python -c 'import json; import sys; print(json.loads(sys.stdin.read()))'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.