Wednesday, March 30, 2022

Enabling Tab Completion for .NET CLI in PowerShell

In this post, let's see how we can enable Tab Completion for .NET CLI commands in PowerShell. By default, Tab completion doesn't work for .NET CLI commands.

As you can see in the below image, I am trying tab-completion after typing some letters and it doesn't resolve me the available commands in .NET CLI. Not so much of a friendly experience.
PowerShell: .NET CLI Tab Completion Does Not Work
But we can get this feature enabled in like 2 steps.

First, run the following command.
# Assuming you have VS Code
code $PROFILE
 
# If you don't have VS Code
notepad $PROFILE

Now update your PowerShell profile by adding the following code snippet.

Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
    dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
        [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue'$_)
    }
}
Save the file, close PowerShell, and open it back up. Now as you can see here, tab completion is working nicely.
PowerShell: .NET CLI Tab Completion in Action
If this doesn't work, try running the following command and ensure it works.
dotnet complete "dotnet *"

dotnet complete
If this doesn't work, make sure that .NET Core 2.0 SDK or above is installed and dotnet --version command resolves to a version of .NET Core 2.0 SDK and above.

If you want to enable .NET CLI tab completion for other Shells like bash, read more here.

Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment