How to use autocomplete at the command line for dotnet, git, winget, and more!
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.
About Newsletter
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.
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.
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.
https://davidwalsh.name/git-branch-autocompletion
Comments are closed.
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.