DEV Community

Cover image for Powershell: Git & .Net Tab Autocomplete Custom CLI
colin-williams-dev
colin-williams-dev

Posted on

Powershell: Git & .Net Tab Autocomplete Custom CLI

Coming from WSL and Bash to a full Microsoft stack has had it's ups and downs. One of the more demoralizing moments was losing my really cute Oh My Zsh configured terminal migrating to *Powershell *:'(

(tl;dr - prompt theme engine for Powershell or my custom profile templates for more granular customization with less bloat)

On top of that neither Git nor .Net have tab-key autocomplete built into their CLIs...

BUT THERE IS A SOLUTION!

Enter: $PROFILE

By default, your system will not have the file that is linked to this variable. If the Profile doesn't exist then create an empty text file named Microsoft.PowerShell_profile.ps1 (note: file extension should be .ps1) at your PowerShell installation location e.g. C:\Program Files\PowerShell\

I suggest running this command inside Powershell so that your customization will persist wherever you want to use it:

code $PROFILE.AllUsersAllHosts
Enter fullscreen mode Exit fullscreen mode

use your desired text editor (e.g. notepad if you're a psychopath)

There are different profiles for different scopes in PowerShell, and they are executed in a specific order. The common profiles include:

$PROFILE.AllUsersAllHosts: Applies to all users and all hosts (global profile, console, ISE, etc).
$PROFILE.AllUsersCurrentHost: Applies to all users but only for the current host.
$PROFILE.CurrentUserAllHosts: Applies to the current user but for all hosts.
$PROFILE: Applies only to the current user and the current host.

Inside one of these profile scripts is where you can overwrite your default Powershell prompt function. Without any modification it behaves like this:

function prompt {
    "$PWD> "
}
Enter fullscreen mode Exit fullscreen mode

Here is an example of a very simple prompt function I wrote. With this very bare bones set up you can customize the colors of the command line as well as the ASCII characters that are used:

function prompt {
  $color = 3, 11, 13 | Get-Random
  Write-Host ("::" + $(Get-Location) + "\:: ~>") -NoNewLine `
   -ForegroundColor $Color
  return " "
}
Enter fullscreen mode Exit fullscreen mode

the only available colors come from the .Net ConsoleColor enumeration that I will list below

0:  Black
1:  DarkBlue
2:  DarkGreen
3:  DarkCyan
4:  DarkRed
5:  DarkMagenta
6:  DarkYellow
7:  Gray
8:  DarkGray
9:  Blue
10: Green
11: Cyan
12: Red
13: Magenta
14: Yellow
15: White
Enter fullscreen mode Exit fullscreen mode

Within the profile.ps1 script that you create you can also Import-Module posh-git to leverage their really intuitive Git status for the command line. See the Repo Docs/API link below in the References section for full description of how to install and customize. (I also include a step by step walkthrough in my Github repository for PS profile templates)

If you wish to explore this further, I made a public repo with some examples, comments, and links to further API and Documentation regarding everything custom PS Profile Scripts! 🐱‍🏍


References:

Top comments (0)