タイトル通り、 .NET CLI のタブ補完を有効にする方法について記載します。
なお、 Bash 以外は試していないので、出来るかわかりません。
(ほぼ公式資料そのままの情報なので出来ると思いますが。)
設定方法
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
.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
.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
fish
config.fish
に以下のコードを追加します。
config.fish
complete -f -c dotnet -a "(dotnet complete (commandline -cp))"
nushell
config.nu
に以下のコードを追加します。
config.nu
let external_completer = { |spans|
{
dotnet: { ||
dotnet complete (
$spans | skip 1 | str join " "
) | lines
}
} | get $spans.0 | each { || do $in }
}
次に、 config
の completions
内に、先ほど定義した external_computer
を external
を追加します。
config.nu
let-env config = {
# your options here
completions: {
# your options here
external: {
# your options here
completer: $external_completer # add it here
}
}
}
使用感
通常 (ls
など)の補完と比べて遅延を感じるので、微妙に使い辛いです。
というか、 ls
などは長いファイルで補完が便利ですが、 .NET CLI ではそのような機会があんまりないので、ぶっちゃけ補完がなくてもいいです。
と思ったのですが、よく考えてみたら .sln
ファイルに .csproj
を追加するときなど、長い文字を使うこともありました。
設定しておくと便利かも。