DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Improve window powershell promt with oh-my-posh and more
Dannyvpz
Dannyvpz

Posted on • Updated on

Improve window powershell promt with oh-my-posh and more

All the content of this article referring to this awesome video by devaslife, check out his channel for detail steps and find more cool videos

Neccessary tools

1> WSL - Window terminal (from Microsoft store)
2> Powershell (from Microsoft store)
3> Scoop installer - check this

Install scoop

Make sure PowerShell 5 (or later, include PowerShell Core) and .NET Framework 4.5 (or later) are installed. Then run:

iwr -useb get.scoop.sh | iex
Enter fullscreen mode Exit fullscreen mode

Note: if you get an error you might need to change the execution policy (i.e. enable Powershell) with

Set-ExecutionPolicy RemoteSigned -scope CurrentUser
Enter fullscreen mode Exit fullscreen mode

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

scoop install curl sudo jq
Enter fullscreen mode Exit fullscreen mode

Install Nerd Fonts

  • Got to this link and download FuraCode font from Nerd Fonts
  • 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

scoop install neovim gcc
Enter fullscreen mode Exit fullscreen mode

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.

Install-Module posh-git -Scope CurrentUser -Force
Enter fullscreen mode Exit fullscreen mode

To import module run

Import-Module posh-git
Enter fullscreen mode Exit fullscreen mode

(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

Install-Module oh-my-posh -Scope CurrentUser -Force
Enter fullscreen mode Exit fullscreen mode

To import module run

Import-Module oh-my-posh
Enter fullscreen mode Exit fullscreen mode

Set Promt theme using

Set-PoshPromt -Theme <THEME_NAME>
Enter fullscreen mode Exit fullscreen mode

THEME_NAME can be found here

3> Terminal Icons (display icons of folder/files)

Run

Install-Module -Name Terminal-Icons -Repository PSGallery -Force
Enter fullscreen mode Exit fullscreen mode

To import module run

Import-Module Terminal-Icons
Enter fullscreen mode Exit fullscreen mode

Result:
Image description

4> Z directory jumper
Jump easily between to recent directories with alias
Run

Install-Module -Name Z -Force
Enter fullscreen mode Exit fullscreen mode

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

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck
Enter fullscreen mode Exit fullscreen mode

Set PSREadLine option

Set-PSReadLineOption -PredictionSource History
Enter fullscreen mode Exit fullscreen mode

6> Fuzzy finder
Run

scoop install fzf
Enter fullscreen mode Exit fullscreen mode

And

Install-Module -Name PSFzf -Scope CurrentUser -Force
Enter fullscreen mode Exit fullscreen mode

To import module run

Import-Module PSFzf
Enter fullscreen mode Exit fullscreen mode

and

Set-PsFzfOption -PsReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'
Enter fullscreen mode Exit fullscreen mode

Now time to enjoy the result.
Image description

Custom oh-my-posh theme

You can customize the json config for oh-my-posh prompt, detail documentation can be found here, the json below was modified from M365Princess theme to personalize the colors & add new line command.

1 >> Create oh-my-post json file at ~/.config/powershell/<file-name>.omp.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
}
Enter fullscreen mode Exit fullscreen mode

2 >> Load prompt config in user profile

With neovim installed run

nvim $PROFILE.CurrentsUserAllHosts
Enter fullscreen mode Exit fullscreen mode

or open with any text edit

Add this line below to the file

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

3 >> Restart powershell and see result

Before:
Image description

After:
Image description

Wrapping up all the import and option set in user profile

With neovim installed run

nvim $PROFILE.CurrentsUserAllHosts
Enter fullscreen mode Exit fullscreen mode

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

echo $PROFILE.CurrentsUserAllHosts
Enter fullscreen mode Exit fullscreen mode

Finalize user profile with some cool alias and function

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
}
Enter fullscreen mode Exit fullscreen mode

Top comments (6)

Collapse
 
anderssonpeter profile image
Peter

I have started a project that I think would be a great fit (it's not done yet but it is functional)

github.com/AnderssonPeter/PowerType it gives you autocomplete for cli tools, currently it only supports a subset of git.

Would love feedback!

 
dannyviiprus profile image
Dannyvpz

Thanks for the your detail explanation, honestly I’m not an expert with these kind of things so its nice to hear that, my post just try to keep everything as simple as it should so that everyone can start following and getting new things (just like me, I haven’t try powershell before till now)

Collapse
 
dannyviiprus profile image
Dannyvpz • Edited on

WSL stand for "Windows Subsystem for Linux", it's for window terminal which you can install it via Microsoft Store, see below link for detail.

docs.microsoft.com/en-us/windows/w...

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git