Skip to content

Instantly share code, notes, and snippets.

@dsouzajude
Last active April 11, 2022 03:20
Show Gist options
  • Save dsouzajude/80a0f6cb1bfabf6a2acf824d5b8169ea to your computer and use it in GitHub Desktop.
Save dsouzajude/80a0f6cb1bfabf6a2acf824d5b8169ea to your computer and use it in GitHub Desktop.
My .zshrc configuration with Zplugin manager
#
# Prerequisite installations for using this zsh configuration
#
# Following utilities must be installed on the system for this configuration to work
#
# fd https://github.com/sharkdp/fd
# fasd https://github.com/clvv/fasd/wiki/Installing-via-Package-Managers#mac-os-x
# fzf https://github.com/junegunn/fzf
# ripgrep https://github.com/BurntSushi/ripgrep
# zplugin https://github.com/zdharma/zplugin/wiki/INSTALLATION
# zsh https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH#install-and-set-up-zsh-as-default
#
# This configuration uses powerlevel10k theme with the following settings
# Font: On iTerm, MesloLGL Nerd Font 15pt.
# For non-ascii also MesloLGL Nerd Font 18pt
# https://github.com/ryanoasis/nerd-fonts
# Theme: POWERLEVEL9K_MODE="nerdfont-complete"
#
#
# Using the zsh shell
# Installation guide https://github.com/robbyrussell/oh-my-zsh/wiki/Installing-ZSH#install-and-set-up-zsh-as-default
#
# Using zplugin plugin manager for zsh
# For updating all plugins:
# >> zplugin update --all
# List all plugins
# >> zplugin ls
# Delete a plugin, examaple fasd from PZT module
# >> zplugin delete PZT::modules/fasd
# Status of zplugin and location of plugins and directories
# >> zplugin zstatus
# https://github.com/zdharma/zplugin/wiki/INSTALLATION
#
source ~/.zplugin/bin/zplugin.zsh
autoload -Uz _zplugin
(( ${+_comps} )) && _comps[zplugin]=_zplugin
#
# Variable and option settings
#
# ZSH Options
# http://zsh.sourceforge.net/Doc/Release/Options.html
setopt promptsubst # Allow funky stuff in prompt
setopt autocd
setopt auto_list
setopt auto_pushd
setopt hash_list_all # Hash everything before completion
setopt completealiases # Complete alisases
setopt always_to_end # When completing from the middle of a word, move the cursor to the end of the word
setopt correct # Spelling correction for commands
setopt list_ambiguous # Complete as much of a completion until it gets ambiguous.
setopt append_history
setopt auto_menu
setopt complete_in_word
setopt hist_ignore_dups # Ignore consecutive duplicates.
setopt hist_reduce_blanks # Trim blanks
setopt inc_append_history
setopt chase_links # Resolve symlinks
unsetopt share_history
CASE_SENSITIVE="true"
# History
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE=10000
export SAVEHIST=$HISTSIZE
export TERM="xterm-256color"
#
# Utility Functions
#
# Use fd (https://github.com/sharkdp/fd) instead of the default find
# command for listing path candidates.
# - The first argument to the function ($1) is the base path to start traversal
# - See the source code (completion.{bash,zsh}) for the details.
_fzf_compgen_path() {
fd --hidden --follow --exclude ".git" . "$1"
}
# Use fd to generate the list for directory completion
_fzf_compgen_dir() {
fd --type d --hidden --follow --exclude ".git" . "$1"
}
#
# fzf fasd Integrations
# Extracted from https://github.com/junegunn/fzf/wiki/examples
#
# Interactive cd
cd() {
if [[ "$#" != 0 ]]; then
builtin cd "$@";
return
fi
while true; do
local lsd=$(echo ".." && ls -p | grep '/$' | sed 's;/$;;')
local dir="$(printf '%s\n' "${lsd[@]}" |
fzf --reverse --preview '
__cd_nxt="$(echo {})";
__cd_path="$(echo $(pwd)/${__cd_nxt} | sed "s;//;/;")";
echo $__cd_path;
echo;
ls -p --color=always "${__cd_path}";
')"
[[ ${#dir} != 0 ]] || return 0
builtin cd "$dir" &> /dev/null
done
}
# Better file selection
v() {
local file
file="$(fasd -Rfl "$1" | fzf -1 -0 --no-sort +m)" && vi "${file}" || return 1
}
# Faster "cd" into recent directories
zd() {
local dir
dir="$(fasd -Rdl "$1" | fzf -1 -0 --no-sort +m)" && cd "${dir}" || return 1
}
# From Sudhindra's config
# https://github.com/bhilburn/powerlevel9k/wiki/Show-Off-Your-Config#sudhindras-config
zsh_wifi_signal(){
local output=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I)
local airport=$(echo $output | grep 'AirPort' | awk -F': ' '{print $2}')
if [ "$airport" = "Off" ]; then
local color='%F{white}'
echo -n "%{$color%}\ufaa9"
else
local ssid=$(echo $output | grep ' SSID' | awk -F': ' '{print $2}')
local speed=$(echo $output | grep 'lastTxRate' | awk -F': ' '{print $2}')
local color='%F{white}'
[[ $speed -gt 100 ]] && color='%F{white}'
[[ $speed -lt 50 ]] && color='%F{red}'
echo -n "%{$color%}$speed Mbps \uf1eb%{%f%} " # removed char not in my PowerLine font
fi
}
# Powerlevel9K theme settings
theme_default() {
#
# Icons and Symbols codes
#
# To see all icon names and symbols http://nerdfonts.com/?set=nf-dev-#cheat-sheet
# Example github icon code \ufbd9
# >> get_icon_names
#
# Color codes
#
# For all color code, usually represented like "%F{<code>} <TEXT> %f"
# >> for code ({000..255}) print -P -- "$code: %F{$code}This is how your text would look like%f"
# Read more here https://github.com/bhilburn/powerlevel9k/wiki/Stylizing-Your-Prompt#segment-color-customization
# Set iTerm Font to SourceCodePro+Powerline+Awesome Regular 15pt
# Set iTerm Non-ASCII Font to Droid Sans Mono Awesome 12pt
# POWERLEVEL9K_MODE='awesome-patched'
# Set iTerm Font to MesloLGL Nerd Font 15pt
# Set iTerm Non-ASCII Font to MesloLGL Nerd Font 18pt
# Download from https://github.com/ryanoasis/nerd-fonts
POWERLEVEL9K_MODE="nerdfont-complete"
# prompt
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(os_icon dir vcs)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status command_execution_time load ram battery ip custom_wifi_signal time newline)
POWERLEVEL9K_MULTILINE_FIRST_PROMPT_PREFIX=''
POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="%F{244}❯%F{242}❯%F{246}❯ %f"
POWERLEVEL9K_LEFT_SEGMENT_SEPARATOR="\uE0B0"
POWERLEVEL9K_PROMPT_ADD_NEWLINE=true
POWERLEVEL9K_PROMPT_ON_NEWLINE=true
POWERLEVEL9K_RPROMPT_ON_NEWLINE=false
# ram
POWERLEVEL9K_RAM_FOREGROUND="249"
POWERLEVEL9K_RAM_BACKGROUND="black"
POWERLEVEL9K_RAM_ELEMENTS=(ram_free) # (ram_free|both|swap_free)
# load
POWERLEVEL9K_LOAD_NORMAL_FOREGROUND="249"
POWERLEVEL9K_LOAD_NORMAL_BACKGROUND="black"
POWERLEVEL9K_LOAD_CRITICAL_FOREGROUND="249"
POWERLEVEL9K_LOAD_CRITICAL_BACKGROUND="black"
POWERLEVEL9K_LOAD_WARNING_FOREGROUND="249"
POWERLEVEL9K_LOAD_WARNING_BACKGROUND="black"
POWERLEVEL9K_LOAD_CRITICAL_VISUAL_IDENTIFIER_COLOR="red"
POWERLEVEL9K_LOAD_WARNING_VISUAL_IDENTIFIER_COLOR="yellow"
POWERLEVEL9K_LOAD_NORMAL_VISUAL_IDENTIFIER_COLOR="green"
# status
POWERLEVEL9K_STATUS_VERBOSE=true
POWERLEVEL9K_STATUS_CROSS=true
# command_execution
POWERLEVEL9K_COMMAND_EXECUTION_TIME_THRESHOLD=3
POWERLEVEL9K_COMMAND_EXECUTION_TIME_PRECISION=1
# dir
POWERLEVEL9K_SHORTEN_DIR_LENGTH=5
POWERLEVEL9K_DIR_HOME_FOREGROUND="white"
POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND="white"
POWERLEVEL9K_DIR_DEFAULT_FOREGROUND="white"
# vcs
POWERLEVEL9K_VCS_GIT_ICON='\uf408'
POWERLEVEL9K_CHANGESET_HASH_LENGTH=6
POWERLEVEL9K_HIDE_BRANCH_ICON=false
POWERLEVEL9K_SHOW_CHANGESET=true # Show hash
POWERLEVEL9K_VCS_SHORTEN_LENGTH=11
POWERLEVEL9K_VCS_SHORTEN_MIN_LENGTH=20
POWERLEVEL9K_VCS_SHORTEN_DELIMITER=".."
POWERLEVEL9K_VCS_SHORTEN_STRATEGY="truncate_from_right"
# battery
POWERLEVEL9K_BATTERY_VERBOSE=false
POWERLEVEL9K_BATTERY_LOW_THRESHOLD='35'
POWERLEVEL9K_BATTERY_LOW_COLOR='red'
POWERLEVEL9K_BATTERY_CHARGING='yellow'
POWERLEVEL9K_BATTERY_CHARGED='green'
POWERLEVEL9K_BATTERY_DISCONNECTED='$DEFAULT_COLOR'
POWERLEVEL9K_BATTERY_ICON='\uf1e6 '
# ip
POWERLEVEL9K_IP_INTERFACE="en0"
POWERLEVEL9K_NETWORK_ICON=""
POWERLEVEL9K_IP_BACKGROUND="grey19"
POWERLEVEL9K_IP_FOREGROUND="grey89"
# custom wifi
POWERLEVEL9K_CUSTOM_WIFI_SIGNAL="zsh_wifi_signal"
POWERLEVEL9K_CUSTOM_WIFI_SIGNAL_BACKGROUND="grey35"
# time
POWERLEVEL9K_TIME_FOREGROUND='grey23'
POWERLEVEL9K_TIME_BACKGROUND='grey93'
}
# Configures the powerline prompt
config_powerline_prompt() {
theme_default
}
#
# Key bindings
# On the terminal, press Ctrl+v and then enter the key sequence for the binding
# The Ctrl+v is a special instruction or escape sequence that output the next
# character you press as a "literal unescaped" character.
#
# Eg: On a terminal
# Ctrl+v + Fn+Del => ^[[3~
#
# You can then bind "^[[3~" to the zsh binding key
#
bindkey "^[[3~" delete-char
bindkey "^E" end-of-line
bindkey "^A" beginning-of-line
#
# Zplugin Programs
#
zplugin ice as"program" atclone"rm -f src/auto/config.cache; ./configure" atpull"%atclone" make pick"src/vim"
zplugin light vim/vim
zplugin ice wait"2" lucid as"program" pick"bin/git-dsf"
zplugin light zdharma/zsh-diff-so-fancy
#
# Zplugin Loading plugins
#
zplugin ice wait'0' lucid
zplugin light zdharma/z-p-submods
zplugin ice atclone"dircolors -b LS_COLORS > clrs.zsh" atpull'%atclone' pick"clrs.zsh"
zplugin load trapd00r/LS_COLORS
alias ls='gls --color=always' # To enable the coloring on ls
# Dependent installation of fzf to work
# Check above for better interactive cd utility function
zplugin light changyuheng/zsh-interactive-cd
zplugin ice wait'0' atload'_zsh_autosuggest_start' lucid
zplugin light zsh-users/zsh-autosuggestions
export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=23'
#
# Zplugin Snippets
#
zplugin ice wait"0" lucid
zplugin snippet OMZ::lib/git.zsh
zplugin ice wait"0" atload"unalias grv" lucid
zplugin snippet OMZ::plugins/git/git.plugin.zsh
# Setup fasd with caching from PZT module
# Requirement: Install fasd for this to work
# Install: https://github.com/clvv/fasd/wiki/Installing-via-Package-Managers#mac-os-x
# Read more: https://github.com/clvv/fasd
# Watch tutorial: https://www.youtube.com/watch?v=ur81Y-mV5Us
#
# fasd PZT module uses a cache file for optimization
# The cache file should be located here:
# >> cache_file="${TMPDIR:-/tmp}/prezto-fasd-cache.$UID.zsh"; cat $cache_file
# >> rm $cache_file
# Ref: https://github.com/sorin-ionescu/prezto/blob/master/modules/fasd/init.zsh#L22
# Remove it when freshly reinstalling the fasd plugin
# Currently it doesnt' regenerate this file.
zplugin ice svn submods"clvv/fasd -> external"
zplugin snippet PZT::modules/fasd
alias o='a -e xdg-open'
alias j='zz'
#
# Themes and settings
#
zplugin env-whitelist 'POWERLEVEL9K_*'
# Powerlevel10k theme
# Load custom `config_powerline_prompt` function above
zplugin ice atinit"config_powerline_prompt"; zplugin light romkatv/powerlevel10k
# Alternative way to load powerline10k
# zplugin ice atclone'curl -fsSL https://gist.githubusercontent.com/romkatv/7cbab80dcbc639003066bb68b9ae0bbf/raw/pure10k.zsh -o pure10k.zsh' \
# atpull'%atclone' run-atpull atload"powerlevel9k_prepare_prompts" \
# src"pure10k.zsh" wait"0" lucid reset-prompt
# zplugin load romkatv/powerlevel10k
#
# Completions
# Some (or all) completion plugins require compinit to be loaded and installed
#
# zsh
zplugin ice wait"0" blockf lucid
zplugin light zsh-users/zsh-completions
# docker
zplugin ice as"completion"
zplugin snippet https://github.com/docker/cli/blob/master/contrib/completion/zsh/_docker
# Syntax Highlighting (should always be at the end)
zplugin ice wait"0" atinit"zpcompinit; zpcdreplay" lucid
zplugin light zdharma/fast-syntax-highlighting
#
# Source files
#
# Source local common environment variables
[ -f $HOME/.env ] && source $HOME/.env
# Source fzf
# Requires installation of fzf
# fzf wiki https://github.com/junegunn/fzf/wiki/examples
#
# Also for this setup, installation of fd and ripgrep
# More on fd https://github.com/sharkdp/fd
# More on ripgrep https://github.com/BurntSushi/ripgrep
# More on these tools https://bluz71.github.io/2018/06/07/ripgrep-fd-command-line-search-tools.html
#
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
FD_OPTIONS="--follow --exclude .git --exclude node_modules"
export FZF_DEFAULT_OPTS="
--no-mouse --height 50% -1 --reverse --multi --inline-info
--color=dark
--color=fg:-1,bg:-1,hl:#c678dd,fg+:#ffffff,bg+:#4b5263,hl+:#d858fe
--color=info:#98c379,prompt:#61afef,pointer:#be5046,marker:#e5c07b,spinner:#61afef,header:#61afef
"
export FZF_COMPLETION_TRIGGER=''
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules}/*" 2> /dev/null'
export FZF_CTRL_T_OPTS="--select-1 --exit-0"
export FZF_ALT_C_COMMAND='fd --type d $FD_OPTIONS'
@dsouzajude
Copy link
Author

My zsh outlook managed by zplugin manager and powerlevel9k theme including fzf functionality with side preview.

demo-including-fzf

Demo with fast-syntax-highlighting, zsh-auto-suggestion, fasd and ls color coding.

demo-with-auto-suggestions-syntax-highlighting-fasd

@RichardBronosky
Copy link

zplugin seems to have been renamed to zinit. Might I suggest you checkout my update to your code...

curl -sL \
    https://gist.github.com/dsouzajude/80a0f6cb1bfabf6a2acf824d5b8169ea/raw/b6786875f3a8db0e8443b2060c279e599e43e8f9/.zshrc | \
        tee /tmp/zshrc.before | \
        sed -E '
            s?zplugin   https://github.com/zdharma/zplugin/wiki/INSTALLATION?zinit     https://github.com/zdharma/zinit#installation?;
            s/([zZ])plugin/\1init/g;
        ' > /tmp/zshrc.after
vimdiff /tmp/zshrc.{before,after}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment