Featured image of post Enable tab completion for the .NET CLI in your terminal

Enable tab completion for the .NET CLI in your terminal

Modify your shell to enable .NET CLI tab completion in PowerShell, Bash, and Zsh for faster command execution.

Have you ever forgotten the name of a .NET command or the necessary arguments to execute it? Even though using the -h parameter can often bail us out by reminding us of the commands, arguments, and descriptions, this process can sometimes feel a bit tedious.

You might not know this, but the .NET CLI includes an autocomplete feature, which can complete or suggest commands and arguments when you press the TAB key. Let’s discover together how to enable it for 3 popular shells: PowerShell, Bash, and Zsh.

The behavior of tab completion varies depending on the shell you use. For instance, in PowerShell, you can cycle through the available options by repeatedly pressing the TAB key, while in zsh, it displays the full list of available options.

Enabling .NET CLI tab completion for PowerShell

First, you must open your PowerShell profile. If you have Visual Studio Code installed, you can do so with the command code $PROFILE ($PROFILE is a special variable that contains the path to your profile file).

Then, add the following lines to your profile file:

# 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', $_)
    }
}

Save the file and restart your PowerShell session. The function Register-ArgumentCompleter will register the completion function for the dotnet command when your session starts.

Enabling .NET CLI tab completion for bash

To enable tab completion for your bash shell for the .NET CLI, add the following code to your .bashrc file:

# 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

Enabling .NET CLI tab completion for zsh

To enable tab completion for your zsh shell for the .NET CLI, add the following code to your .zshrc file:

# 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

.NET CLI tab completion in action

Let’s say you forgot what the available arguments for the dotnet test command are. You can simply type dotnet test --, then press the TAB key, and the .NET CLI will print the following available arguments:

--arch                        --configuration               --no-build                    
--artifacts-path              --diag                        --no-restore                  
--blame                       --disable-build-servers       --nologo                      
--blame-crash                 --environment                 --os                          
--blame-crash-collect-always  --filter                      --output                      
--blame-crash-dump-type       --framework                   --results-directory           
--blame-hang                  --help                        --runtime                     
--blame-hang-dump-type        --interactive                 --settings                    
--blame-hang-timeout          --list-tests                  --test-adapter-path           
--collect                     --logger                      --verbosit

When there’s only one possible completion, the .NET CLI will automatically complete the command for you. For example, if you type dotnet new x, then press the TAB key, the .NET CLI will automatically complete the command to dotnet new xunit.

References

Licensed under CC BY 4.0
Ko-fi donations Buy me a coffee