Scott Hanselman

How to use autocomplete at the command line for dotnet, git, winget, and more!

October 01, 2020 Comment on this post [12] Posted in Open Source
Sponsored By

Many years ago .NET Core added Command line "tab" completion for .NET Core CLI in PowerShell or bash but few folks have taken the moment it takes to set up.

I enjoy setting up and making my prompt/command line/shell/terminal experience as useful (and pretty) as possible. You have lots of command line shells to choose from on Windows!

Keep in mind these are SHELLs not terminals or alternative consoles. You can run all of these in the Windows Terminal.

  • Classic cmd.exe command prompt (fake DOS!) - Still useful and Clink can make it more bash-y without going full bash.
  • Yori from Malcolm Smith
  • Starship - more on this later, it's somewhat unique
  • Windows PowerShell - a classic because...
  • PowerShell 7 is out and runs literally anywhere, including ARM machines!

I tend to use PowerShell 7 (formerly PowerShell Core) as my main prompt because it's a cross-OS prompt. I can use the same prompt, same functions, same everything on Windows and Linux.

But it's command-line autocompletion that brings me the most joy!

  • git ch<TAB> -> git checkout st<TAB> -> git checkout staging
  • dotnet bu<TAB> -> dotnet build
  • dotnet --list-s<TAB> -> dotnet --list-sdks
  • winget in<TAB> -> winget install -> winget install WinDi<TAB> -> winget install WinDirStat

Once you have successfully tab'ed you way to glory it's hard to stop. With PowerShell and its cousins this is made possible with Register-ArgumentCompleter. Here's what it looks like for the dotnet CLI.

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

Looks like a lot, but the only part that matters is that when it sees the command "dotnet" and some partial text and the user presses TAB, it will call "dotnet complete" passing in the cursorPosition and the wordToComplete.

NOTE: If you understand how this works, you can easily make your own Argument Completer for those utilities that you use all the time at work! You can make them for the folks at work who use your utilities!

You never actually see this call to "dotnet complete." You just see yourself typing dotnet bui<TAB> and getting a series of choices to tab through!

Here's what happens behind the scenes:

>dotnet complete --position 3 bui
build
build-server
msbuild

You can add these to your $profile. Usually I run 'notepad $profile" at the command line and it will autocreate the correct file in the correct location.

This is a super powerful pattern! You can get autocomplete in Git in PowerShell with PoshGit as well as in WinGet!

What are some more obscure autocompletes that you have added to your PowerShell profile?

ACTION: Finally, please take a moment and subscribe to my YouTube or head over to http://computerstufftheydidntteachyou.com and explore! I'd love to hit 100k subs over there. I heard they give snacks.


Sponsor: Suffering from a lack of clarity around software bugs? Give your customers the experience they deserve and expect with error monitoring from Raygun.com. Installs in minutes, try it today!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
October 07, 2020 6:56
Hi,
I've been wanting to start using Powershell and new Windows Terminal more but I'm kind of stuck on cmd.exe because it is generally 'easier' to access (and muscle memory is always an issue). Do you have any tips/instructions on how to holistically replace using cmd.exe with Terminal + Powershell (7).
Also in VS2019, you have an option to right click on project and launch Command line / Powershell. However this launches the Powershell which comes with windows (5 I think). Is there a way to replace this with PS7.

Thanks.

P.S. I love your blog.
October 07, 2020 7:01
Wow this is awesome!.. I just copy pasted it into my profile and it works... marvellous!


October 07, 2020 8:27
What do you use to get the nice colored visualisation of the current git branch and the current directory? I would love to use it on my dev machine and impress my developer friends!
October 07, 2020 8:53
The problem with dotnet CLI completion is that it is incomplete (Pun intended).

It only completes internal commands - global commands, even if bundled with the dotnet CLI, does not get completed.

So, for example,

PS> dotnet us<TAB>
does not complete to dotnet user-secrets, even though the dotnet CLI clearly knows about the user-secrets command. You can see this if you run dotnet help - in the list of commands user-secrets is displayed.

So not as useful as it could be.
October 07, 2020 13:11
@gyurisc, the link is right there... read his blog post again.
October 07, 2020 13:54
Fancy shell add-ins or higher level scripting languages may save you a few seconds once in a while, or every day, but the installation time and often debugging time when it eventually breaks far outweighs the trivial day to day time saving.

It's a long time lesson learned via untangling someone else's mess of build scripts, ETL scripts, or other mess multiple times on UNIX, Windows, Bash, PowerShell.

"How much time will it save me today?" and "Is a 1 hour maybe time saving worth the risk of spending many hours in the future troubleshooting it?" are pertinent questions.
DD
October 07, 2020 14:22
Specifically for git, I think a better solution is to use aliases. For the example Scott gave of checking out staging, you could have "cos" (or whatever you want), so that you could just do "git cos". Not only that, aliases themselves also get autocompleted! Personally this isn't as important since my aliases are 1-4 letters anyway.

I have about 100 aliases. Granted I don't use all of them often but about 20 or 30 are getting used all the time.

Strongly recommended.
October 07, 2020 15:01
Here you can find awesome pwsh docker completion https://github.com/matt9ucci/DockerCompletion
October 07, 2020 15:40
I use Scoop to install CLI tools like gh, nuget and the like. It doesn't have completion built in, but there's an add-on called scoop-completion with instructions on how to enable it in PowerShell.
October 10, 2020 18:31
Thanks for that. It's really useful. Just downloaded PowerShell 7.0
October 25, 2020 16:46
I'd love to be able to tab-autocomplete gut checkout [remote branch]. Has anyone implemented this kind of helper?
October 25, 2020 16:49
Let me Google that for me (with Bing, of course):

https://davidwalsh.name/git-branch-autocompletion

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.