本文适用于:✔️ .NET 6 SDK 及更高版本
本文介绍如何为五种 shell 配置 Tab 补全功能:PowerShell(pwsh)、Bash、zsh、fish 和 nushell。 对于其他 shell,请参阅其有关如何配置选项卡完成的文档。
本机 Shell 补全脚本(支持 .NET 10+)
从 .NET 10 开始,.NET CLI 包括本机 shell 完成脚本,这些脚本的运行速度比早期版本中提供的动态完成要快得多。 本机完成会生成特定于 shell 的脚本,用于直接在 shell 中处理 CLI 语法的静态部分,从而显著提高性能。
生成完成脚本
使用dotnet completions script命令为 shell 生成完成脚本:
dotnet completions script [SHELL]
该 [SHELL] 参数接受以下值之一:
bashfishnushellpwshzsh
如果未指定 shell,该命令会从环境中推断出正确的 shell。 在 Windows 上,默认为 PowerShell(pwsh)。 在其他系统上,它会检查环境变量的 SHELL 文件名是否与任何受支持的 shell 值匹配。
完成能力
原生自动补全根据 shell 提供不同级别的支持。
| 壳牌 | 完成类型 | 选项卡完成中的说明 |
|---|---|---|
| bash | 混合 | 否 |
| 鱼 | dynamic | 否 |
| nushell | dynamic | 否 |
| PowerShell | 混合 | 是的 |
| zsh | 混合 | 是的 |
完成类型:
-
混合:生成特定于 shell 的代码,用于快速处理 CLI 语法的静态部分,但在处理动态内容时(例如 NuGet 包 ID),则会回退到
dotnet complete命令。 -
动态:所有完成作都经过
dotnet complete命令,该命令可能较慢,但可确保全面覆盖。
将 shell 配置为使用原生补全功能
将适当的命令添加到 shell 的配置文件中,以启用原生补全功能:
PowerShell
将以下行添加到 PowerShell 配置文件($PROFILE):
dotnet completions script pwsh | Out-String | Invoke-Expression
Bash
将以下行添加到 .bashrc 文件:
eval "$(dotnet completions script bash)"
Zsh
将以下行添加到 .zshrc 文件:
eval "$(dotnet completions script zsh)"
Fish
将以下行添加到 config.fish 文件:
dotnet completions script fish | source
Nushell
将以下内容添加到文件的开头 config.nu :
dotnet completions script nushell | save -f ~/.local/share/nushell/completions/dotnet.nu
use ~/.local/share/nushell/completions/dotnet.nu *
动态完成脚本(所有版本)
对于 .NET 10 之前的 .NET 版本,或者想要使用动态完成,可以将 shell 配置为使用 dotnet complete 命令。 动态完成工作,方法是将当前命令行发送到 dotnet complete 命令并处理 shell 中的结果。
设置后,在 shell 中输入 dotnet 命令并按下 Tab 键即可触发 .NET CLI 的标签补全功能。 当前命令行将发送到 dotnet complete 命令,结果由 shell 处理。 可以通过将某些内容直接发送到 dotnet complete 命令来测试结果,而无需启用选项卡完成。 例如:
> dotnet complete "dotnet a"
add
clean
--diagnostics
migrate
pack
如果该命令不起作用,请确保已安装 .NET Core 2.0 SDK 或更高版本。 如果它已安装,但该命令仍然不起作用,请确保命令 dotnet 解析为 .NET Core 2.0 SDK 或更高版本。 使用dotnet --version命令查看dotnet解析到的当前路径版本。 有关详细信息,请参阅 选择要使用的 .NET 版本。
例子
下面是选项卡完成提供的一些示例:
| Input | 成为 | 因为 |
|---|---|---|
dotnet a⇥ |
dotnet add |
add 是第一个子命令,按字母顺序排列。 |
dotnet add p⇥ |
dotnet add --help |
Tab 补全匹配子字符串,而 --help 在字母顺序中排在最前面。 |
dotnet add p⇥⇥ |
dotnet add package |
再次按 Tab 会显示下一个建议。 |
dotnet package add Microsoft⇥ |
dotnet package add Microsoft.ApplicationInsights.Web |
结果按字母顺序返回。 |
dotnet reference remove ⇥ |
dotnet reference remove ..\..\src\OmniSharp.DotNet\OmniSharp.DotNet.csproj |
选项卡自动补全可以识别项目文件。 |
PowerShell
若要在 PowerShell 中为 .NET CLI 添加自动补全功能,请创建或编辑存储在变量 $PROFILE 中的配置文件。 有关详细信息,请参阅如何创建配置文件和配置文件和执行策略。
将以下代码添加到您的配置文件:
# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
dotnet complete --position $cursorPosition "$commandAst" | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
bash
若要为 .NET CLI 的 bash shell 添加命令补全,请将以下代码添加到 .bashrc 文件中:
# bash parameter completion for the dotnet CLI
function _dotnet_bash_complete()
{
local cur="${COMP_WORDS[COMP_CWORD]}" IFS=$'\n' # On Windows you may need to use use IFS=$'\r\n'
local candidates
read -d '' -ra candidates < <(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)
read -d '' -ra COMPREPLY < <(compgen -W "${candidates[*]:-}" -- "$cur")
}
complete -f -F _dotnet_bash_complete dotnet
zsh
若要将选项卡完成添加到 .NET CLI 的 zsh shell,请将以下代码添加到 .zshrc 文件中:
# zsh parameter completion for the dotnet CLI
_dotnet_zsh_complete()
{
local completions=("$(dotnet complete "$words")")
# If the completion list is empty, just continue with filename selection
if [ -z "$completions" ]
then
_arguments '*::arguments: _normal'
return
fi
# This is not a variable assignment, don't remove spaces!
_values = "${(ps:\n:)completions}"
}
compdef _dotnet_zsh_complete dotnet
鱼
若要将 tab 补全添加到 .NET CLI 的 fish shell,请将以下代码添加到 config.fish 文件中:
complete -f -c dotnet -a "(dotnet complete (commandline -cp))"
nushell
若要在您的 nushell 中为 .NET CLI 添加命令行自动补全功能,请将以下内容添加到您的config.nu 文件开头:
let external_completer = { |spans|
{
dotnet: { ||
dotnet complete (
$spans | skip 1 | str join " "
) | lines
}
} | get $spans.0 | each { || do $in }
}
然后在config记录中,找到completions部分,并将external_completer之前定义的内容添加到external:
let-env config = {
# your options here
completions: {
# your options here
external: {
# your options here
completer: $external_completer # add it here
}
}
}