Organize Your Zsh Configurations and Plugins
In this guide we are going to make our .zshrc
file more readable and maintainable. We will split our aliases
, functions
, completions
and plugins
into their own files and folders.
Prerequisite
Note: Better not close the current terminal session. We’ll reload the session at the end.
Backup
Backup your .zshrc
before you proceed.
cp ~/.zshrc ~/.zshrc.bak
Start fresh
Now open .zshrc
the file with your favorite text editor, clear all the text-content and paste this lines:
# The completion system activation
autoload -Uz compinit
compinit
# extended history size
export HISTSIZE=1000000000
export SAVEHIST=1000000000
setopt EXTENDED_HISTORY
Config Folder
Create a folder to store our files.
mkdir -p ~/.config/zsh
Aliases
Aliases make our lives easier. Let’s create a aliases.zsh
file in our zsh config folder.
touch ~/.conifg/zsh/aliases.zsh
open aliases.zsh
and paste this lines:
Note: These aliases are hand crafted by me. You can tweak them to your liking.
# aliases
alias edit="open -e"
alias reload="source ~/.zshrc"
alias zshconfig="edit ~/.zshrc"
# React native aliases
alias iospod="cd iOS && pod install && cd .."
alias rnrelease="cd android && ./gradlew assembleRelease && cd .."
alias rndebug="cd android && ./gradlew assembleDebug && cd .."
alias rnbundlejs="npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res"
alias rnbundle="cd android && ./gradlew bundleRelease && cd .."
# NPM aliases
alias nr="npm run"
alias ni="npm install"
alias ns="npm start"
alias nu="npm uninstall"
#yarn aliases
alias yr="yarn run"
alias yi="yarn add"
alias ys="yarn start"
alias yu="yarn remove"
#pg aliases
alias pg_start="launchctl load ~/Library/LaunchAgents"
alias pg_stop="launchctl unload ~/Library/LaunchAgents"
alias grep="grep --color"
alias ll="ls -al"
We need to source this file in our .zshrc
for it to work. Open .zshrc
and append this line,
[[ -f ~/.config/zsh/aliases.zsh ]] && source ~/.config/zsh/aliases.zsh
Completions
Some programs do not provide shell completions out of the box. For those programs we are going to create completions. First create the completions.zsh
file:
Note: If you’ve not installed that kind of program, you can skip this.
touch ~/.config/zsh/completions.zsh
Now paste these lines (I am only using flutter
):
###-begin-flutter-completion-###
if type complete &>/dev/null; then
__flutter_completion() {
local si="$IFS"
IFS=$'\n' COMPREPLY=($(COMP_CWORD="$COMP_CWORD" \
COMP_LINE="$COMP_LINE" \
COMP_POINT="$COMP_POINT" \
flutter completion -- "${COMP_WORDS[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
complete -F __flutter_completion flutter
elif type compdef &>/dev/null; then
__flutter_completion() {
si=$IFS
compadd -- $(COMP_CWORD=$((CURRENT-1)) \
COMP_LINE=$BUFFER \
COMP_POINT=0 \
flutter completion -- "${words[@]}" \
2>/dev/null)
IFS=$si
}
compdef __flutter_completion flutter
elif type compctl &>/dev/null; then
__flutter_completion() {
local cword line point words si
read -Ac words
read -cn cword
let cword-=1
read -l line
read -ln point
si="$IFS"
IFS=$'\n' reply=($(COMP_CWORD="$cword" \
COMP_LINE="$line" \
COMP_POINT="$point" \
flutter completion -- "${words[@]}" \
2>/dev/null)) || return $?
IFS="$si"
}
compctl -K __flutter_completion flutter
fi
###-end-flutter-completion-###
Finaly source the file in .zshrc
. Open .zshrc
and append:
[[ -f ~/.config/zsh/completions.zsh ]] && source ~/.config/zsh/completions.zsh
Functions
I am using some function aliases so I like to keep them separate from the other aliases. You can use this file functions.zsh
for any function you might want. Let’s create the file:
touch ~/.config/zsh/functions.zsh
Open functions.zsh
and paste these lines:
# for downloading gdrive files with wget
# usage gdive_download file_id output_file_name
gdrive_download () {
CONFIRM=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate "https://docs.google.com/uc?export=download&id=$1" -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$CONFIRM&id=$1" -O $2
rm -rf /tmp/cookies.txt
}
# connect android device to port 8081 ( react native development )
# usage connect_rn_android device_id ( run `adb devices` to get the device id )
connect_rn_android () {
adb -s $1 reverse tcp:8081 tcp:8081
echo $1 connected to port 8081
}
Now append in .zshrc
:
[[ -f ~/.config/zsh/functions.zsh ]] && source ~/.config/zsh/functions.zsh
Plugins
There are so many useful plugins available for zsh. I am using only few of them to make my life easier. Here’s the list:
- git ~ The git plugin provides many aliases and a few useful functions.
- you-should-use ~ Simple zsh plugin that reminds you that you should use one of your existing aliases for a command you just typed.
- zsh-completions ~ Additional completion definitions for Zsh.
- zsh-z ~ Zsh-z is a command line tool that allows you to jump quickly to directories.
I have gatherd all the above plugins in one place. Download zsh-plugins.zip to get started. Now extract the zip and move plugins
folder to ~/.config/zsh
folder.
Open .zshrc
and append:
[[ -f ~/.config/zsh/plugins/plugins.zsh ]] && source ~/.config/zsh/plugins/plugins.zsh
We are done organizing. Your bulky .zshrc
file should look like this:
# The completion system activation
autoload -Uz compinit
compinit
export HISTSIZE=1000000000
export SAVEHIST=1000000000
setopt EXTENDED_HISTORY
# my configs and setups
[[ -f ~/.config/zsh/aliases.zsh ]] && source ~/.config/zsh/aliases.zsh
[[ -f ~/.config/zsh/completions.zsh ]] && source ~/.config/zsh/completions.zsh
[[ -f ~/.config/zsh/functions.zsh ]] && source ~/.config/zsh/functions.zsh
# plugins
[[ -f ~/.config/zsh/plugins/plugins.zsh ]] && source ~/.config/zsh/plugins/plugins.zsh
Note: Now close all terminal session and start a new one to verify our changes.
Happy Hacking 😉