Improve window powershell promt with oh-my-posh and more
Tags: powershell
Published: 2021-12-09 17:35:20

**All the content of this article referring to this awesome [video](https://www.youtube.com/watch?v=5-aK2_WwrmM&t=2105s) by [devaslife](https://www.youtube.com/c/devaslife), check out his channel for detail steps and find more cool videos**

{% youtube 5-aK2_WwrmM %}
 

### Neccessary tools
1> WSL - Window terminal (from Microsoft store)
2> Powershell (from Microsoft store)
3> Scoop installer - check [this](https://scoop.sh)

### Install scoop
Make sure PowerShell 5 (or later, include PowerShell Core) and .NET Framework 4.5 (or later) are installed. Then run:
```PowerShell
iwr -useb get.scoop.sh | iex
```
Note: if you get an error you might need to change the execution policy (i.e. enable Powershell) with

```PowerShell
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
```
Set PATH for `scoop` command in `System Properties` > `Advanced` > `Environment Variables`.

Add new directory path in `System variables` > `Path`, choose scoop directory `C:\Users\<USERNAME>\scoop\shims`

Close and reload terminal.


### Install some neccessary packages via scoop
`jq` is optional
Run
```PowerShell
scoop install curl sudo jq
```

### Install Nerd Fonts
- Got to this [link](https://github.com/ryanoasis/nerd-fonts) and download `FuraCode` font from [Nerd Fonts](https://www.nerdfonts.com)
- Extract and right click >> Install the font.


### Setup Window terminal
- Choose Powershell as default.
- Checkout appearance tab of Powershell and choose `FuraCode Nerd Font Retina` as default font.
- Select Color scheme `One Half Dark` and enable Acrylic option with opacity 50% to have a better Window terminal display.


### Install Neovim (optional)
Run
```PowerShell
scoop install neovim gcc
```


### Install some nice modules to improve powershell
1> posh-git

Provides prompt with Git status summary information and tab completion for Git commands, parameters, remotes and branch names.

```PowerShell
Install-Module posh-git -Scope CurrentUser -Force
```
To import module run
```PowerShell
Import-Module posh-git
```
(To import module automatically when shell starting need to set in User Profile, this can be found at the end of article, same for all modules)

2> oh-my-posh

Theming capabilities for the PowerShell prompt in ConEmu

```PowerShell
Install-Module oh-my-posh -Scope CurrentUser -Force
```
To import module run
```PowerShell
Import-Module oh-my-posh
```
Set Promt theme using
```PowerShell
Set-PoshPromt -Theme <THEME_NAME>
```
THEME_NAME can be found [here](https://ohmyposh.dev/docs/themes)

3> Terminal [Icons](https://github.com/devblackops/Terminal-Icons) (display icons of folder/files)

Run
```PowerShell
Install-Module -Name Terminal-Icons -Repository PSGallery -Force
```
To import module run
```PowerShell
Import-Module Terminal-Icons
```
Result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5enzz9jndbjgxc3a2br.png)
 
4> Z directory jumper
Jump easily between to recent directories with alias
Run
```PowerShell
Install-Module -Name Z -Force
```
To jump just type `z` + any `word` contain in recent directory used in `cd` command, check the module for more detail.

5> PSReadLine - Autocompletion recent command on typing
run
```PowerShell
Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck
```
Set PSREadLine option
```PowerShell
Set-PSReadLineOption -PredictionSource History
```

6> Fuzzy finder
Run 
```PowerShell
scoop install fzf
```
And

```PowerShell
Install-Module -Name PSFzf -Scope CurrentUser -Force
```
To import module run
```PowerShell
Import-Module PSFzf
```
and
```PowerShell
Set-PsFzfOption -PsReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'
```

Now time to enjoy the result.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb9dibpx3mpdaua0v2e0.png)

### Custom oh-my-posh theme
You can customize the json config for oh-my-posh prompt, detail documentation can be found [here](https://ohmyposh.dev/docs/config-overview), the json below was modified from [M365Princess theme](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/themes/M365Princess.omp.json) to personalize the colors & add new line command.

1 >> Create oh-my-post json file at `~/.config/powershell/<file-name>.omp.json`

```json
{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "final_space": true,
  "blocks": [
    {
      "type": "prompt",
      "alignment": "left",
      "segments": [
        {
          "type": "session",
          "style": "diamond",
          "foreground": "#ffffff",
          "background": "#031d44",
          "leading_diamond": "\uE0B6",
          "trailing_diamond": "",
          "properties": {
            "prefix": "",
            "template": "{{ .UserName }}"
          }
        },
        {
          "type": "path",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#ffffff",
          "background": "#04395e",
          "properties": {
            "style": "folder"
          }
        },
        {
          "type": "git",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#ffffff",
          "background": "#70a288",
          "properties": {
            "fetch_stash_count": true,
            "fetch_upstream_icon": true,
            "branch_icon": "",
            "fetch_status": false,
            "template": "{{ .UpstreamIcon }}{{ .HEAD }}{{ if gt .StashCount 0 }} \uF692 {{ .StashCount }}{{ end }}",
            "prefix": " \u279C (",
            "postfix": ") "
          }
        },
	{
	  "type": "php",
	  "style": "powerline",
	  "powerline_symbol": "\uE0B0",
	  "foreground": "#ffffff",
	  "background": "#4063D8",
	  "properties": {
	    "prefix": " \ue73d ",
	    "enable_hyperlink": false
	  }
	},
        {
          "type": "node",
          "style": "powerline",
          "powerline_symbol": "\uE0B0",
          "foreground": "#ffffff",
          "background": "#dab785",
          "properties": {
            "prefix": " \uE718 "
          }
        },
        {
          "type": "time",
          "style": "diamond",
          "trailing_diamond": "\uE0B0",
          "foreground": "#ffffff",
          "background": "#d5896f",
          "properties": {
            "prefix": " \u2665 ",
            "time_format": "15:04"
          }
        }
      ]
    },
    {
      "type": "prompt",
      "alignment": "left",
      "newline": true,
      "segments": [
        {
          "type": "text",
          "style": "plain",
          "foreground": "#007ACC",
          "properties": {
            "prefix": "",
            "text": "\uE602"
          }
        }
      ]
    }
  ],
  "final_space": true
}
```

2 >> Load prompt config in user profile

With neovim installed run
```powershell
nvim $PROFILE.CurrentsUserAllHosts
```

or open with any text edit

Add this line below to the file

```powershell
oh-my-posh --init --shell pwsh --config 'C:\Users\<your-username>\.config\powershell\<you-file-name>.omp.json' | Invoke-Expression
```

3 >> Restart powershell and see result

Before: 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hb9dibpx3mpdaua0v2e0.png)

After:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2spjkevolsew8cmdsex.png)
 
### Wrapping up all the import and option set in user profile
With neovim installed run
```PowerShell
nvim $PROFILE.CurrentsUserAllHosts
```

Or open the file in any text editor, file path can be found by running:

```PowerShell
echo $PROFILE.CurrentsUserAllHosts
```

Finalize user profile with some cool alias and function

```PowerShell
Import-Module posh-git
Import-Module oh-my-posh

# PSReadLine
Set-PSREadLineOption -PredictionSource History

# Terminal-Icons
Import-Module Terminal-Icons

# Fzf
Import-Module PSFzf
Set-PsFzfOption -PSReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'

# Set theme
Set-PoshPrompt -Theme M365Princess

# Load prompt configs
oh-my-posh --init --shell pwsh --config 'C:\Users\<your-username>\.config\powershell\<you-file-name>.omp.json' | Invoke-Expression

# Alias (Optional)
Set-Alias vim nvim
Set-Alias g git
Set-Alias ll ls
Set-Alias grep findstr
Set-Alias tig 'C:\Program Files\Git\usr\bin\tig.exe'
Set-Alias less 'C:\Program Files\Git\usr\bin\less.exe'

# Ultilities (Optional)
function which ($command) {
    Get-Command -Name $command -ErrorAction SilentlyContinue |
        Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}
```