Skip to content

安装 zsh

安装

sh
apt install zsh

修改 PROMPT

~/.zshrc 中,注释原有 prompt 样式,添加下列内容:

sh
# prompt adam1
PROMPT='%F{yellow}%~ # %f'

添加别名

将下列内容添加到 ~/.zshrc 中:

# aliases
alias gs="git status"
alias gb="git rev-parse --abbrev-ref HEAD"
alias gba="git -P branch"
alias gd="git diff"
alias gdp="git -P diff"
alias gdh="git diff HEAD^ HEAD"
alias gl="git log"
alias gn="git --no-pager log --pretty='format:%Cgreen[%h] %Cblue[%ai] %Creset[%an]%C(Red)%d %n  %Creset%s %n' -n5"
alias ga="git add"
alias gc="git commit"
alias gk="git checkout"
alias gau="git add -u"
alias gcm="git commit -m"
alias gcan="git commit --amend --no-edit"
alias gp="git push"
alias gpf="git push -f"
alias gacp="git add -u && git commit --amend --no-edit && git push -f"

alias ta="tmux a"
alias td="tmux detach"
alias tn="tmux new -s"
alias tl="tmux ls"
alias ts="tmux select-pane -T"

alias k9="kill -9"
alias lt="ls -lt"

绑定按键

用下面的命令显示按键对应的字符:

sh
showkey -a

ctrl + D 退出该界面。

将下列内容添加到 ~/.zshrc 中:

sh
# bind keys
bindkey "^[[1;5C" forward-word
bindkey "^[[1;3C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[1;3D" backward-word
bindkey "^[[1~"   beginning-of-line
bindkey "^[[4~"   end-of-line
bindkey "^[[3~"   delete-char
bindkey "^[^[[3~" delete-word

重启 zsh 或者 source ~/.zshrc 使配置生效。

See: zsh - Ctrl + left/right arrow keys issue - Unix & Linux Stack Exchange

ubuntu - Fix key settings (Home/End/Insert/Delete) in .zshrc when running Zsh in Terminator Terminal Emulator - Stack Overflow

line editor - zsh kill Ctrl + Backspace, Ctrl + Delete - Unix & Linux Stack Exchange

自动建议

sh
cd ~
mkdir .zsh && cd .zsh
touch zsh-autosuggestions.zsh

复制下列脚本内容到 ~/.zsh/zsh-autosuggestions.zsh 中:

将下列内容添加到 ~/.zshrc 中:

sh
# auto suggestions
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff"
source ~/.zsh/zsh-autosuggestions.zsh

自动补全

sh
cd .zsh
git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git

将下列内容添加到 ~/.zshrc 中:

sh
# auto complete
source ~/.zsh/zsh-autocomplete/zsh-autocomplete.plugin.zsh
# style of auto complete
zstyle ':completion:*'  list-colors '=*=96'

并注释掉所有 compinit 相关的语句:

sh
# # Use modern completion system
# autoload -Uz compinit
# compinit

然后创建 ~/.zshenv 文件:

sh
cd ~
touch .zshenv

将下列内容添加到 ~/.zshenv 中:

sh
skip_global_compinit=1

See: How to change zsh-autocomplete color or make text item list under like zsh-autosuggestions

See: ANSI escape code:

设置 zsh 为默认 shell

sh
chsh -s $(which zsh)

.tmux.conf 中添加:

sh
# which zsh
set-option -g default-shell /usr/bin/zsh

See: command line - How to make ZSH the default shell? - Ask Ubuntu

一键配置

sh
wget https://raw.githubusercontent.com/Hansimov/blog/main/docs/notes/scripts/zsh_setup.sh -O ~/zsh_setup.sh && chmod +x ~/zsh_setup.sh && ~/zsh_setup.sh
sh
# backup .zshrc
cp ~/.zshrc ~/.zshrc.bak

# get customized .zshrc
# wget https://raw.githubusercontent.com/Hansimov/blog/main/docs/notes/configs/.zshrc -O ~/.zshrc
wget https://raw.staticdn.net/Hansimov/blog/main/docs/notes/configs/.zshrc -O ~/.zshrc

# install zsh-autosuggestions
mkdir -p ~/.zsh/zsh-autosuggestions
# wget https://raw.githubusercontent.com/zsh-users/zsh-autosuggestions/master/zsh-autosuggestions.zsh -O ~/.zsh/zsh-autosuggestions.zsh
wget https://raw.staticdn.net/zsh-users/zsh-autosuggestions/master/zsh-autosuggestions.zsh -O ~/.zsh/zsh-autosuggestions.zsh

# install zsh-autocomplete
cd ~/.zsh
# git clone --depth 1 -- https://github.com/marlonrichert/zsh-autocomplete.git && cd ~
git clone --depth 1 -- https://githubfast.com/marlonrichert/zsh-autocomplete.git && cd ~
cd ~
touch ~/.zshenv
echo "skip_global_compinit=1" > ~/.zshenv

# # enable changes
# zsh

.zshrc 完整样例

sh
# Set up the prompt

autoload -Uz promptinit
promptinit
# prompt adam1
PROMPT='%F{yellow}%~ # %f'

setopt histignorealldups sharehistory

# Use emacs keybindings even if our EDITOR is set to vi
bindkey -e

# Keep 1000 lines of history within the shell and save it to ~/.zsh_history:
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.zsh_history

# Use modern completion system
# autoload -Uz compinit
# compinit

zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select=2
eval "$(dircolors -b)"
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' menu select=long
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true

zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'

# aliases
alias ls="ls --color"

alias gs="git status"
alias gb="git rev-parse --abbrev-ref HEAD"
alias gba="git -P branch"
alias gd="git diff"
alias gdp="git -P diff"
alias gdh="git diff HEAD^ HEAD"
alias gl="git log"
alias gn="git --no-pager log --pretty='format:%Cgreen[%h] %Cblue[%ai] %Creset[%an]%C(Red)%d %n  %Creset%s %n' -n5"
alias ga="git add"
alias gc="git commit"
alias gk="git checkout"
alias gau="git add -u"
alias gcm="git commit -m"
alias gcan="git commit --amend --no-edit"
alias gp="git push"
alias gpf="git push -f"
alias gacp="git add -u && git commit --amend --no-edit && git push -f"

alias ta="tmux a"
alias td="tmux detach"
alias tn="tmux new -s x"
alias tl="tmux ls"
alias ts="tmux select-pane -T"

alias k9="kill -9"
alias lt="ls -lt"
alias hi="hostname -i"

alias cda="conda activate ai"
alias cdd="conda deactivate"

alias nu="gpustat -cpu -i"

# bind keys
bindkey "^[[1;5C" forward-word
bindkey "^[[1;3C" forward-word
bindkey "^[[1;5D" backward-word
bindkey "^[[1;3D" backward-word
bindkey "^[[1~"   beginning-of-line
bindkey "^[[4~"   end-of-line
bindkey "^[[3~"   delete-char
bindkey "^[^[[3~" delete-word

# auto suggestions
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff"
source ~/.zsh/zsh-autosuggestions.zsh

# auto complete
source ~/.zsh/zsh-autocomplete/zsh-autocomplete.plugin.zsh
# style of auto complete
zstyle ':completion:*'  list-colors '=*=96'

# Environment variables

# huggingface mirror
export HF_ENDPOINT=https://hf-mirror.com
# postgres
# export PATH=/usr/lib/postgresql/16/bin:$PATH
# export PG_CONFIG=/Library/PostgreSQL/16/bin/pg_config