(精华)2020年12月23日 .NET Core CLI启用TAB自动补全功能

174 阅读1分钟

一:windows

Powershell

要将 tab 自动补全添加到适用于 .NET Core CLI 的 PowerShell,请创建或编辑存储在变量 $PROFILE 中的配置文件。 有关详细信息,请参阅如何创建配置文件和配置文件和执行策略。

在 Powershell 中执行 notepad $profile 命令,会打开 $profile 配置文件,如果不能保存请尝试用管理员模式的powershell中执行。
没有文件也可以新建文件:命令

if (!(Test-Path -Path $PROFILE)) {
  New-Item -ItemType File -Path $PROFILE -Force
}

将以下代码添加到配置文件中:

# PowerShell parameter completion shim for the dotnet CLI
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
     param($commandName, $wordToComplete, $cursorPosition)
         dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
         }
 }

执行更新

set-ExecutionPolicy RemoteSigned

如果想看 $profile 配置文件,可以打开 %USERPROFILE%\Documents\WindowsPowerShell 目录,
在这个目录下会有一个 Microsoft.PowerShellISE_profile.ps1 的文件,这就是上面的配置文件

二:liunx

Bash

要将 tab 自动补全添加到适用于 .NET Core CLI 的 bash shell,请将以下代码添加到 .bashrc 文件:

# bash parameter completion for the dotnet CLI

_dotnet_bash_complete()
{
  local word=${COMP_WORDS[COMP_CWORD]}

  local completions
  completions="$(dotnet complete --position "${COMP_POINT}" "${COMP_LINE}" 2>/dev/null)"
  if [ $? -ne 0 ]; then
    completions=""
  fi

  COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}

complete -f -F _dotnet_bash_complete dotnet