You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在oh-my-zsh环境下加载修改后的系统bash-completion文件

问题:Zsh(Oh-My-Zsh)无法加载自定义的apt-get/apt-fast补全定义

我修改了/etc/bash_completion(它是/usr/share/bash-completion/bash_completion的包装器,负责加载/usr/share/bash-completion/completions/*下的单个补全脚本),原因是最新版bash-completion自带的apt-get和apt-fast补全没有包含autopurge选项的定义。但我的Zsh(使用Oh-My-Zsh)始终无法加载这个自定义的补全规则。

我试过以下方法,全部无效:

  • 先进入Bash加载/etc/bash_completion,再切换回Zsh,但切换后已加载的补全规则会被覆盖(该方法在Bash中可以正常生效)
  • 在~/.zshrc开头添加autoload -U +X compinit && compinit和autoload -U +X bashcompinit && bashcompinit
  • 使用Oh-My-Zsh默认的gitfast插件(该插件会尝试加载系统bash-completion文件)
  • 使用zsh-bash-completions-fallback插件
  • 直接加载/etc/bash_completion,但Zsh无法处理其中Bash专属的shopt内置命令,导致加载失败

另外,zsh-completions仅包含部分补全定义,缺少apt、apt-get和apt-fast的完整补全规则。

以下是移除默认注释后的~/.zshrc内容:

# Workaround: `bashcompinit` first, right after `compinit`
autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="powerlevel10k/powerlevel10k"

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
    gitfast
    sudo
    web-search
    autojump
    zsh-autosuggestions
    zsh-syntax-highlighting
)

# `sudo -E` workaround
# You must place this before sourcing $ZSH/oh-my-zsh.sh.
ZSH_DISABLE_COMPFIX=true

source $ZSH/oh-my-zsh.sh

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# Workaround: Append local alias with sudo
alias sudo="sudo "

# command-not-found
if [[ -s '/etc/zsh_command_not_found' ]]; then
  source '/etc/zsh_command_not_found'
fi

# apt-fast
alias apt="apt-fast"
alias apt-get="apt-fast"

# zsh-autosuggestions
bindkey '^I' complete-word # tab => complete
bindkey '^[[Z' autosuggest-accept # shift + tab => autosuggest

附:我的用户账户中已删除所有~/.bash*和~/.profile文件。

内容的提问来源于stack exchange,提问作者hurryman2212

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 14:03:12