Command line “tab” completion for .NET Core CLI in PowerShell or bash

Dev Tips



Lots of people are using open source .NET Core and the “dotnet” command line, but few know that the .NET CLI supports command “tab” completion!

You can ensure you have it on .NET Core 2.0 with this test:

C:Usersscott>  dotnet complete "dotnet add pac"
package

You can see I do, as it proposed “package” as the completion for “pac”

Now, just go into PowerShell and run:

notepad $PROFILE

And add this code to the bottom to register “dotnet complete” as the “argument completer” for the dotnet command.

# PowerShell parameter completion shim for the dotnet CLI 
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
    param($commandName, $wordToComplete, $cursorPosition)
        dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object {
           [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
}

Then just use it! You can do the same not only in PowerShell, but in bash, or zsh as well!

It’s super useful for “dotnet add package” because it’ll make smart completions like this:

It also works for adding/removing local project preferences as it is project file aware. Go set it up NOW, it’ll take you 3 minutes.

RANDOM BUT ALSO USEFUL: “dotnet serve” – A simple command-line HTTP server.

Here’s a useful little global tool – dotnet serve. It launches a server in the current working directory and serves all files in it. It’s not kestrel, the .NET Application/Web Server. It’s just a static webserver for development.

The latest release of dotnet-serve requires the 2.1.300-preview1 .NET Core SDK or newer. Once installed, run this command:

dotnet install tool --global dotnet-serve 

Then whenever I’m in a folder where I want to server something static (CSS, JS, PNGs, whatever) I can just

dotnet serve

It can also optionally open a web browser navigated to that localhost URL.


Sponsor: Get the latest JetBrains Rider for debugging third-party .NET code, Smart Step Into, more debugger improvements, C# Interactive, new project wizard, and formatting code in columns.do




















Source link

Leave a Reply