I like to think I’m a loyal member of the CLI TILL I DIE team. I’m a macOS user, so naturally, I use a combination of iTerm2 and Oh-My-Zsh.

In this short post, we’ll see how to enable the dotnet command-line interface’s (CLI) tab completion feature, along with the ability to run any installed .NET tools globally.

The Dotnet CLI

The dotnet CLI comes with many embedded actions. The tool can create new projects, add NuGet packages, run solutions, and so much more. While we can learn these methods over time, we don’t have to memorize them all.

The dotnet CLI has a complete action that lists all the possible actions we could perform.

dotnet cli complete action

Updating .Zshrc

Getting Tab Completion Working

To get tab completion, we need to update our .zshrc file using the output from the dotnet complete command.

First, we need to open our .zshrc file in our favorite editor. I’ll use Rider in this example.

rider ~/.zshrc

Next, we’ll need to add the following text block to the end of our .zshrc file.

_dotnet_zsh_complete()
{
  local completions=("$(dotnet complete "$words")")

  reply=( "${(ps:\n:)completions}" )
}

compctl -K _dotnet_zsh_complete dotnet

We need to save the file, then reload our profile.

source ~/.zshrc

Now we get tab completion as we can see in the following video.

dotnet CLI tab completion

Getting Global Tools Working

If we utilize any dotnet global tools, we’ll also want to register our .dotnet/tools folder into the global PATH. We can add the following line while we still have our .zshrc file open.

export PATH="$PATH:$HOME/.dotnet/tools"

When we’re all done, we should have a .NET section in our .zshrc file that look like the following.

# .NET Core
export PATH="$PATH:$HOME/.dotnet/tools"
_dotnet_zsh_complete()
{
  local completions=("$(dotnet complete "$words")")

  reply=( "${(ps:\n:)completions}" )
}

compctl -K _dotnet_zsh_complete dotnet

Conclusion

There we have it. We now have dotnet tab completion, and can run our dotnet global tools from anywhere. I hope you found this blog post helpful, and please leave a comment below. I also want to thank the Microsoft Documentation team for their useful documentation.