My Zsh Config

My personal config for Zsh (Z Shell), noting for next time.

Installation

Windows

Native GNU bash doesn't run on windows, but you can use something like git bash (msys2) to run bash on windows. Furthermore, you can also use zsh on windows. The articles below may be helpful:

The default character set used on your Windows platform may not be UTF-8. You can add these lines to your ~/.zshrc to enable UTF-8 in your terminal:

1
2
3
4
# Set Windows codepage to 65001 (UTF-8).
if [[ "$OSTYPE" == "msys" ]]; then
chcp.com 65001 &> /dev/null
fi

You may also need a terminal emulator on windows. I use alacritty and my config file looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
working_directory = "C:\\Users\\BlockLune"

[shell]
program = "C:\\Program Files\\Git\\bin\\bash.exe"
args = ["--login", "-i"]

[font]
bold = { family = "FiraCode Nerd Font Mono", style = "Bold" }
normal = { family = "FiraCode Nerd Font Mono", style = "Light" }
size = 12
offset = { x = 0, y = 4 }

[window]
dimensions = { columns = 80, lines = 30 }
padding = { x = 12, y = 12 }

Linux

Use your favorite package manager to install zsh and then run chsh -s $(which zsh) to set Z Shell as your default shell.

Mac

It's installed by default.

Themes and Plugins

Firstly, install oh-my-zsh and powerlevel10k.

1
2
3
4
# installs oh-my-zsh using curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# clones powerlevel10k
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k

Then, to set powerlevel10k as the theme, edit in ~/.zshrc:

1
ZSH_THEME="powerlevel10k/powerlevel10k"

Plugins

Oh-my-zsh provides many plugins (See the list), and you can install more by yourself.

I installed some plugins not provided by oh-my-zsh:

1
2
3
4
5
6
# git clone https://github.com/esc/conda-zsh-completion ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/conda-zsh-completion
git clone https://github.com/sukkaw/zsh-osx-autoproxy ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-osx-autoproxy --depth=1 # ONLY on macOS
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions --depth=1
git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search --depth=1
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting --depth=1
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git $ZSH_CUSTOM/plugins/fast-syntax-highlighting --depth=1 # a faster choice

Edit in ~/.zshrc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# I disable some plugins here
plugins=(
# conda-zsh-completion
# fd
# fzf
# git
# git-commit
history-substring-search
# pip
ripgrep
z
zsh-autosuggestions
zsh-osx-autoproxy
# zsh-syntax-highlighting
fast-syntax-highlighting
)
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=white'

Usage of some plugins:

Alias Command
g git
ga git add
gaa git add --all
gapa git add --patch
gau git add --update
gav git add --verbose
gb git branch
gco git checkout
gcl git clone --recurse-submodules
gcam git commit --all --message
gc git commit --verbose
gc! git commit --verbose --amend
gd git diff
gf git fetch
glog git log --oneline --decorate --graph
gl git pull
gp git push
gst git status
gss git status --short
... ...

Syntax: git <type> [(-s, --scope) "<scope>"] "<message>", where <type> is:

  1. build
  2. chore
  3. ci
  4. docs
  5. feat
  6. fix
  7. perf
  8. refactor
  9. rev
  10. style
  11. test

NOTE: the alias for revert type is rev, as otherwise it conflicts with the git command of the same name.
It will still generate a commit message in the format revert: <message>

Alias Command
git style "remove trailing whitespace" git commit -m "style: remove trailing whitespace"
git fix -s "router" "correct redirect link" git commit -m "fix(router): correct redirect link"

Aliases

Actually, the two plugins shown above (git & git-commit) are some aliases. A better practice is to define them yourself, so that they are more useful to you. First create a file ~/.zsh_aliases and define your aliases:

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
# .bash_aliases

# ls
## -F appends file type indicators
## / for directories
## * for executables
## @ for symlinks
## etc.
alias ls="ls -FG --color=auto"
alias ll="lsd -lh"
alias la="ls -a"
alias l="ll -a"

# interactive
alias rm="rm -i"
alias cp="cp -i"
alias mv="mv -i"
alias ln="ln -i"

# mkdir
alias md="mkdir -p"

# pagers
alias less="less --quit-at-eof --no-init --tabs=4 --RAW-CONTROL-CHARS"
alias more="less"

# greps
alias grep="grep --color=auto"
alias egrep="grep -E --color=auto"
alias fgrep="grep -F --color=auto"

# git
alias g="git"
alias ga="git add --verbose"
alias gaa="git add --all --verbose"
alias gb="git branch"
alias gc!="git commit --verbose --amend"
alias gc="git commit --verbose"
alias gcb="git checkout -b"
alias gcl="git clone --recurse-submodules"
alias gco="git checkout"
alias gcp="git cherry-pick"
alias gd="git diff"
alias gf="git fetch"
alias gl="git pull"
alias glog="git log --oneline --decorate --graph"
alias gp="git push"
alias gr="git remote"
alias grb="git rebase"
alias grf="git reflog"
alias grm="git rm"
alias grs="git restore"
alias grst="git restore --staged"
alias gsh="git show"
alias gss="git status --short"
alias gst="git status"
alias gsta="git stash push"

# node
alias cnpm="npm --registry=https://registry.npmmirror.com --cache=$HOME/.npm/.cache/cnpm --disturl=https://npmmirror.com/mirrors/node --userconfig=$HOME/.cnpmrc"

Then, source the file in your ~/.zshrc:

1
[[ -f ~/.zsh_aliases ]] && source ~/.zsh_aliases

Lazyload

As your configuration increases, the terminal may take longer to start. In this case, some lazyload configurations shown in the article below may be helpful:

Q&A

Resources