ZSH 配置指南

This guide covers the installation of Oh My Zsh, popular plugins, and the Powerlevel10k theme.
1. Installation
Section titled “1. Installation”Install Oh My Zsh
Section titled “Install Oh My Zsh”sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"Install Plugins & Theme
Section titled “Install Plugins & Theme”Clone the repositories into your custom directory:
# zsh-completionsgit clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-completions
# zsh-syntax-highlightinggit clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# Powerlevel10k Themegit clone https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/themes/powerlevel10k
# macOS specific completions (Optional)git clone https://github.com/scriptingosx/mac-zsh-completions.git ${ZSH_CUSTOM:=~/.oh-my-zsh/custom}/plugins/mac-zsh-completions2. Configuration
Section titled “2. Configuration”Edit your ~/.zshrc file to apply the theme and plugins:
# Set ThemeZSH_THEME="powerlevel10k/powerlevel10k"
# Enable Pluginsplugins=( git zsh-completions zsh-syntax-highlighting docker-compose extract # Add other plugins here)
# For macOS completions, add 'mac-zsh-completions' to the plugins listAfter saving, restart Zsh or run source ~/.zshrc.
To configure Powerlevel10k, run p10k configure.
3. Reference Configuration (Manjaro)
Section titled “3. Reference Configuration (Manjaro)”The following is the default Zsh configuration from Manjaro Architect, useful for reference.
/etc/zsh/zprofile
Section titled “/etc/zsh/zprofile”emulate sh -c 'source /etc/profile'~/.zshrc
Section titled “~/.zshrc”## Options sectionsetopt correct # Auto correct mistakessetopt extendedglob # Extended globbing. Allows using regular expressions with *setopt nocaseglob # Case insensitive globbingsetopt rcexpandparam # Array expension with parameterssetopt nocheckjobs # Don't warn about running processes when exitingsetopt numericglobsort # Sort filenames numerically when it makes sensesetopt nobeep # No beepsetopt appendhistory # Immediately append history instead of overwritingsetopt histignorealldups # If a new command is a duplicate, remove the older onesetopt autocd # if only directory path is entered, cd there.
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # Case insensitive tab completionzstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" # Colored completion (different colors for dirs/files/etc)zstyle ':completion:*' rehash true # automatically find new executables in path# Speed up completionszstyle ':completion:*' accept-exact '*(N)'zstyle ':completion:*' use-cache onzstyle ':completion:*' cache-path ~/.zsh/cacheHISTFILE=~/.zhistoryHISTSIZE=1000SAVEHIST=500WORDCHARS=${WORDCHARS//\/[&.;]} # Don't consider certain characters part of the word
## Keybindings sectionbindkey -ebindkey '^[[7~' beginning-of-line # Home keybindkey '^[[H' beginning-of-line # Home keyif [[ "${terminfo[khome]}" != "" ]]; then bindkey "${terminfo[khome]}" beginning-of-line # [Home] - Go to beginning of linefibindkey '^[[8~' end-of-line # End keybindkey '^[[F' end-of-line # End keyif [[ "${terminfo[kend]}" != "" ]]; then bindkey "${terminfo[kend]}" end-of-line # [End] - Go to end of linefibindkey '^[[2~' overwrite-mode # Insert keybindkey '^[[3~' delete-char # Delete keybindkey '^[[C' forward-char # Right keybindkey '^[[D' backward-char # Left keybindkey '^[[5~' history-beginning-search-backward # Page up keybindkey '^[[6~' history-beginning-search-forward # Page down key
# Navigate words with ctrl+arrow keysbindkey '^[Oc' forward-wordbindkey '^[Od' backward-wordbindkey '^[[1;5D' backward-wordbindkey '^[[1;5C' forward-wordbindkey '^H' backward-kill-word # delete previous word with ctrl+backspacebindkey '^[[Z' undo # Shift+tab undo last action
## Alias sectionalias cp="cp -i" # Confirm before overwriting somethingalias df='df -h' # Human-readable sizesalias free='free -m' # Show sizes in MB
# Theming sectionautoload -U compinit colors zcalccompinit -dcolors
# enable substitution for promptsetopt prompt_subst
# Maia promptPROMPT="%B%{$fg[cyan]%}%(4~|%-1~/.../%2~|%~)%u%b >%{$fg[cyan]%}>%B%(?.%{$fg[cyan]%}.%{$fg[red]%})>%{$reset_color%}%b "echo $USER@$HOST $(uname -srm) $(lsb_release -rcs)
# Git Prompt ConfigurationGIT_PROMPT_SYMBOL="%{$fg[blue]%}±"GIT_PROMPT_PREFIX="%{$fg[green]%}[%{$reset_color%}"GIT_PROMPT_SUFFIX="%{$fg[green]%}]%{$reset_color%}"GIT_PROMPT_AHEAD="%{$fg[red]%}ANUM%{$reset_color%}"GIT_PROMPT_BEHIND="%{$fg[cyan]%}BNUM%{$reset_color%}"GIT_PROMPT_MERGING="%{$fg_bold[magenta]%}⚡︎%{$reset_color%}"GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}●%{$reset_color%}"GIT_PROMPT_MODIFIED="%{$fg_bold[yellow]%}●%{$reset_color%}"GIT_PROMPT_STAGED="%{$fg_bold[green]%}●%{$reset_color%}"
parse_git_branch() { ( git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD ) 2> /dev/null}
parse_git_state() { local GIT_STATE="" local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')" if [ "$NUM_AHEAD" -gt 0 ]; then GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD} fi local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')" if [ "$NUM_BEHIND" -gt 0 ]; then GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND} fi local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)" if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING fi if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED fi if ! git diff --quiet 2> /dev/null; then GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED fi if ! git diff --cached --quiet 2> /dev/null; then GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED fi if [[ -n $GIT_STATE ]]; then echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX" fi}
git_prompt_string() { local git_where="$(parse_git_branch)" [ -n "$git_where" ] && echo "$GIT_PROMPT_SYMBOL$(parse_git_state)$GIT_PROMPT_PREFIX%{$fg[yellow]%}${git_where#(refs/heads/|tags/)}$GIT_PROMPT_SUFFIX" [ ! -n "$git_where" ] && echo "%{$fg[red]%} %(?..[%?])"}
# Color man pagesexport LESS_TERMCAP_mb=$'\E[01;32m'export LESS_TERMCAP_md=$'\E[01;32m'export LESS_TERMCAP_me=$'\E[0m'export LESS_TERMCAP_se=$'\E[0m'export LESS_TERMCAP_so=$'\E[01;47;34m'export LESS_TERMCAP_ue=$'\E[0m'export LESS_TERMCAP_us=$'\E[01;36m'export LESS=-r
## Plugins sectionsource /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zshsource /usr/share/zsh/plugins/zsh-history-substring-search/zsh-history-substring-search.zshzmodload zsh/terminfobindkey "$terminfo[kcuu1]" history-substring-search-upbindkey "$terminfo[kcud1]" history-substring-search-downbindkey '^[[A' history-substring-search-upbindkey '^[[B' history-substring-search-down
case $(basename "$(cat "/proc/$PPID/comm")") in login) RPROMPT="%{$fg[red]%} %(?..[%?])" alias x='startx ~/.xinitrc' ;; *) RPROMPT='$(git_prompt_string)' source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20 ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8' ;;esac