Enable tab completion for the .NET CLI in your terminal

asimmon

Anthony Simmon

Posted on April 1, 2024

Enable tab completion for the .NET CLI in your terminal

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.

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', $_)
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

.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
Enter fullscreen mode Exit fullscreen mode

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

💖 💪 🙅 🚩
asimmon
Anthony Simmon

Posted on April 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related