Skip to content

Instantly share code, notes, and snippets.

@marzocchi
Last active November 27, 2022 17:32
Show Gist options
  • Save marzocchi/f1bc60858d05480c6683d563c2658665 to your computer and use it in GitHub Desktop.
Save marzocchi/f1bc60858d05480c6683d563c2658665 to your computer and use it in GitHub Desktop.
zshrc
# vim: se filetype=zsh
# zmodload zsh/zprof
autoload -U add-zsh-hook
export LANG=en_US.UTF-8
TZ="Europe/Berlin"
HISTFILE=$HOME/.zhistory
HISTSIZE=3000
SAVEHIST=3000
export EDITOR='vim'
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_AUTO_UPDATE=1
export PROMPT_EOL_MARK="💫"
# 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
HOSTNAME="`hostname`"
# 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 explict host names from Host directives in ~/.ssh/config
hosts=($(grep "Host " ~/.ssh/config | cut -c 6- | xargs))
zstyle ':completion:*:hosts' hosts $hosts
zstyle ':completion:*:ssh:argument-1:*' tag-order hosts
# 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@localhost:dir1/dir2 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
path=(
/Applications/terminal-notifier.app/Contents/MacOS
~/.local/bin
$path
)
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets pattern)
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
path=(
~/bin
$path
~/projects/rig.py
~/projects/mrz.io/itermctl/bin
~/projects/mrz.io/iterm-notify/bin
)
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
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()))'
}
function nix-grep() {
if [[ $# -lt 1 ]]; then
echo usage: "$(basename "$0")" REGEXP >&2
exit 1
fi
set -x
PAGER="" nix-env -qaP | grep "$1"
}
function nix-install-my-packages() {
nix-env -iA nixpkgs.myPackages && rehash
}
# if [[ -d "$HOME/.nix-profile/bin" ]]; then
# export PATH="$PATH:$HOME/.nix-profile/bin"
# fi
test -f "$HOME"/.nix-profile/etc/profile.d/hm-session-vars.sh \
&& source "$HOME"/.nix-profile/etc/profile.d/hm-session-vars.sh
if [[ -d "$HOME/bin/terminal-notifer.app" ]]; then
export PATH="$PATH:$HOME/terminal-notifier.app/Contents/MacOS"
fi
if command -v mcfly > /dev/null; then
export MCFLY_KEY_SCHEME=vim
eval "$(mcfly init zsh)"
fi
export DUPLICACY_SSH_KEY_FILE="$HOME"/.ssh/id_ed25519
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment