Skip to content

Instantly share code, notes, and snippets.

@mh-firouzjah
Last active May 1, 2024 20:30
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save mh-firouzjah/2548513a8a842d532bfb10346f8f5db0 to your computer and use it in GitHub Desktop.
Save mh-firouzjah/2548513a8a842d532bfb10346f8f5db0 to your computer and use it in GitHub Desktop.
how to add zsh-like theme and feature to windows powershell

Better PowerShell

Theme | History | KeyBinding

MyPowershell

in order to make windows powershell more comfortable and add a some theme and features like history binding, auto complete on keypress and so on, if you have used linux teminl wiht zsh for example, we are going to make powershell like that as much as we can.

What we will achieve

  • Git status information: Posh-Git adds Git status information to your prompt as well as tab-completion for Git commands, parameters, remotes, and branch names.
  • Theme: Oh-My-Posh provides theme capabilities for your PowerShell prompt. Oh-My-Posh comes with several built-in themes.
  • History and History Based Prediction: Predictive IntelliSense is implemented in the PowerShell engine and presented through the PSReadLine module.
  • Key Bindings for Predictions: Key bindings control cursor movement and additional features within the prediction. When you type a character the prediction suggestes you last command started with typed character(s). by using Up/Down Arrows you can switch between commands with same start-characters.

theme-and-git-info Prediction

How we will achieve

  • Run powershell as an administrator and execute following command to disable the execution policy, otherwise it wont let you follow next steps
    • Set-ExecutionPolicy RemoteSigned
    • Register-PSRepository -Default # this registers the default repository for PowerShell modules, so we can install packages thats are in next steps
    • Install-Module posh-git -Scope CurrentUser
    • Install-Module oh-my-posh -Scope CurrentUser
    • Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck
  • Now you need to create a profile to set custom settings for powershell, run the following caommand:
    • notepad $PROFILE # this would create/edit file as: "C:\Users<YOURUSERNAME>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
    • Now copy the following code and past it in the notepad and save the file.
  • You may need to install a font that supports the emojis!, I suggest to use Hack Nerd Font Mono
    • after downloading the font and install it, right click on powershells top bar and go to properties, click on Font tab and select the font.

if youre using Windows Terminal:

  • open the app, hit Ctrl and , then click on gear button at down-left side, inside the open jason file search for "profiles":, now edit/add "defaults": the fontFace so that finally sould looks:
...
"profiles":
    {
        "defaults":
        {
            "fontFace": "Hack Nerd Font Mono",
            ...
        },
        ...
    ...

Read more at:

If you prefer to create your custom theme

It is possible to create your own theme for powershell, in order to do that:

  • run Set-PoshPrompt -Theme jandedobbeleer to set an initial theme, then run export-PoshTheme -FilePath ~/.oh-my-posh.omp.json to export the theme at "C:\Users\<YOURUSERNAME\.oh-my-posh.omp.json".
  • now open that json file C:\Users\<YOURUSERNAME\.oh-my-posh.omp.json and customize as you want
  • run Set-PoshPrompt -Theme C:\Users\<YOURUSERNAME\.oh-my-posh.omp.json so your edited theme would be used.
  • have look at Nerd Fonts, to find more icons

The json file attached is an example, which I've edited for test perpose.

Profile Code

Import-Module posh-git
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\star.omp.json" | Invoke-Expression
Set-PSReadLineOption -PredictionSource History
Set-PSReadlineKeyHandler -Key "Tab" -Function MenuComplete
Set-PSReadlineKeyHandler -Key "UpArrow" -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key "DownArrow" -Function HistorySearchForward
Set-PSReadLineOption -Colors @{ InlinePrediction = '#898c5b'}
Set-PSReadlineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key "RightArrow" -ScriptBlock {
       param($key, $arg)

       $line = $null
       $cursor = $null
       [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

       if ($cursor -lt $line.Length) {
           [Microsoft.PowerShell.PSConsoleReadLine]::ForwardChar($key, $arg)
       } else {
           [Microsoft.PowerShell.PSConsoleReadLine]::AcceptNextSuggestionWord($key, $arg)
       }
}

Set-PSReadLineKeyHandler -Key End -ScriptBlock {
       param($key, $arg)

       $line = $null
       $cursor = $null
       [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

       if ($cursor -lt $line.Length) {
           [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine($key, $arg)
       } else {
           [Microsoft.PowerShell.PSConsoleReadLine]::AcceptSuggestion($key, $arg)
       }
}
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"segments": [
{
"foreground": "#4063D8",
"properties": {
"postfix": "",
"prefix": "",
"windows": "",
"wsl": "",
"wsl_separator": " - "
},
"style": "diamond",
"type": "os"
},
{
"foreground": "#f3863d",
"properties": {
"display_host": false,
"postfix": "",
"prefix": " "
},
"style": "diamond",
"type": "session"
},
{
"foreground": "#9FD356",
"properties": {
"ahead_color": "#f2b25c",
"behind_color": "#f06d65",
"branch_max_length": 30,
"color_background": false,
"display_stash_count": true,
"display_status": true,
"display_upstream_icon": true,
"local_changes_color": "#9e81b6",
"postfix": "]",
"prefix": " [",
"staging_color": "#9e81b6",
"status_colors_enabled": true,
"working_color": "#BD6200"
},
"style": "diamond",
"type": "git"
},
{
"foreground": "#5598df",
"properties": {
"folder_icon": "",
"home_icon": "",
"max_depth": 3,
"prefix": " ",
"style": "agnoster_short"
},
"style": "diamond",
"type": "path"
}
],
"type": "prompt"
},
{
"alignment": "right",
"segments": [
{
"properties": {
"postfix": "",
"time_format": "15:04:05"
},
"style": "diamond",
"type": "time"
},
{
"properties": {
"charged_color": "#4caf50",
"charged_icon": "",
"charging_color": "#40c4ff",
"charging_icon": "",
"color_background": false,
"discharging_color": "#ff5722",
"discharging_icon": "",
"postfix": "",
"prefix": " "
},
"style": "diamond",
"type": "battery"
}
],
"type": "prompt"
},
{
"alignment": "left",
"newline": true,
"segments": [
{
"foreground": "#5598df",
"properties": {
"postfix": "s",
"prefix": "",
"style": "dallas",
"threshold": 0
},
"style": "diamond",
"type": "executiontime"
},
{
"foreground": "#9FD356",
"properties": {
"always_enabled": true,
"always_numeric": false,
"color_background": false,
"display_exit_code": true,
"error_color": "#E84855",
"prefix": ""
},
"style": "diamond",
"type": "exit"
}
],
"type": "prompt"
}
]
}
@Kyu
Copy link

Kyu commented May 1, 2024

Oh-My-Posh has new Install instructions:

> Import-Module oh-my-posh
Hey friend

In an effort to grow oh-my-posh, the decision was made to no
longer support the PowerShell module. Over the past year, the
added benefit of the module disappeared, while the burden of
maintaining it increased.

However, this doesn't mean oh-my-posh disappears from your
terminal, it just means that you'll have to use a different
tool to install it.

All you need to do, is follow the migration guide here:

https://ohmyposh.dev/docs/migrating

I used winget install JanDeDobbeleer.OhMyPosh -s winget to install, from the link

edit: Also the link in

You may need to install a font that supports the emojis!, I suggest to use Hack Nerd Font Mono

is dead

@mh-firouzjah
Copy link
Author

@Kyu Hi
This gist is an old one, I'm currently using Linux for about +3 years as my main OS and as a result of not using windows anymore I did not use the context of this gist for a long time.
You should look at the official websites and documents of oh-my-push and nerd-fonts so to be able to install and configure anything required, also still part of this gist works (as I can remember that we have used for one of my friends) but I'm sorry that due to changes made to oh-my-push package the instruction about it's installation does not work.

@Kyu
Copy link

Kyu commented May 1, 2024

@mh-firouzjah no problem, thank you for hosting this doc, it was a very good jump off point for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment