在 GitHub 上编辑

Shell Tab 补全

享受更快、更少输入错误的 DVC 使用体验!

当 DVC 在 macOS 上通过 Homebrew 安装,或在 Linux 上通过 debrpm 软件源安装,或通过 snap 安装时,Shell 补全功能会自动启用。

其他 DVC 安装方式请参见下文。

工作原理

命令补全通常通过在 Shell 中按下 <tab> 键触发。你的 Shell 将列出可能的参数。例如:

$ dvc r<tab>
Completing dvc commands
remote  -- Set up and manage data remotes.
remove  -- Remove stages or .dvc files, unprotect their outputs, ...
repro   -- Reproduce complete or partial pipelines by executing ...
root    -- Return the relative path to the root of the DVC project.
run     -- Generate a stage file from a command and execute the command

根据你在命令行中已输入的内容,它会补全:

  • 可用的 DVC 命令和子命令。例如,dvc plots <tab> 会提示 diffmodifyshowtemplates
  • 有效选项(--flags)。例如,dvc add --r<tab> 会提示 --recursive--remote
  • 在特定上下文中合理的参数。例如,dvc repro <tab> 会提示现有的 DVC 文件

你使用的是什么 Shell?

使用命令 echo $0 检查你正在使用的 Shell(bashzshtcsh),然后按照下方说明安装并配置 Shell 补全。

  1. 首先,确保已安装 Bash 补全支持:

    $ brew install bash-completion

    然后编辑 ~/.bash_profile,确保包含以下行:

    if [ -f "$(brew --prefix)"/etc/bash_completion ]; then
        . "$(brew --prefix)"/etc/bash_completion
    fi
  2. 运行以下命令安装 DVC 补全功能:

    $ dvc completion -s bash \
      | sudo tee "$(brew --prefix)"/etc/bash_completion.d/dvc
  3. 最后,打开一个新终端以激活补全功能。

  1. 首先,确保已安装 Bash 补全支持:

    $ sudo apt install --reinstall bash-completion

    然后编辑 ~/.bashrc,确保包含以下行:

    # enable bash completion in interactive shells
    if ! shopt -oq posix; then
      if [ -f /usr/share/bash-completion/bash_completion ]; then
        . /usr/share/bash-completion/bash_completion
      elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
      fi
    fi
  2. 运行以下命令安装 DVC 补全功能:

    $ dvc completion -s bash | sudo tee /etc/bash_completion.d/dvc
  3. 最后,打开一个新终端以激活补全功能。

将补全脚本放置在 $fpath 包含的目录中。Zsh 要求文件名为 _dvc

  1. 首先,检查是否已安装 Zsh 补全支持。确保 ~/.zshrc 中包含以下行以加载 compinit

    # Use modern completion system
    autoload -Uz compinit
    compinit
  2. 运行以下命令安装 DVC 补全功能:

    $ dvc completion -s zsh | sudo tee /usr/local/share/zsh/site-functions/_dvc
  3. 最后,打开一个新终端以激活补全功能。

  4. 可选:通过添加颜色提示使补全输出更加美观。将以下内容添加到 ~/.zshrc

# Case insensitive match
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

# Group matches and describe.
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:options' auto-description '%d'
zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' format ' %F{yellow}-- %d --%f'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' verbose yes
  1. 运行以下命令安装 DVC 补全功能:

    $ dvc completion -s tcsh | sudo tee /etc/profile.d/dvc.completion.csh
  2. 打开一个新终端以激活补全功能。

内容

🐛 发现问题?告诉我们!或者修复它:

在 GitHub 上编辑

有疑问?加入我们的聊天,我们会为您提供帮助:

Discord 聊天