閱讀英文

共用方式為


如何啟用 .NET CLI 的自動補全功能

本文適用於: ✔️.NET Core 2.1 SDK 和更新版本

本文說明如何設定五個殼層的索引標籤自動完成:PowerShell、Bash、zsh、fish 和 nushell。 如需其他 shell,請參閱其文檔以了解如何設定自動完成功能。

設定之後,在殼層中輸入 dotnet 命令,然後按下 Tab 鍵,即可啟動 .NET CLI 的命令補全功能。 目前的命令行會傳送至 dotnet complete 命令,而結果會由殼層處理。 您可以直接將某項目傳送至 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 版本

範例

以下是索引標籤自動完成提供的一些範例:

輸入 變成 因為
dotnet a⇥ dotnet add add 是第一個子命令,依字母順序排列。
dotnet add p⇥ dotnet add --help Tab 鍵自動完成會比對子字串,並以 --help 字母順序排列。
dotnet add p⇥⇥ dotnet add package 第二次按索引標籤會顯示下一個建議。
dotnet package add Microsoft⇥ dotnet package add Microsoft.ApplicationInsights.Web 結果會依字母順序傳回。
dotnet reference remove ⇥ dotnet reference remove ..\..\src\OmniSharp.DotNet\OmniSharp.DotNet.csproj Tab 鍵自動完成對專案檔案敏感。

PowerShell

若要將標籤完成功能新增至 .NET CLI 的 PowerShell,請建立或編輯儲存在變數 $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 殼層,請將下列程式代碼新增至檔案 .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

若要將 Tab 鍵自動完成功能新增至您的 .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
        }
    }
}

其他資源