Skip to content

Instantly share code, notes, and snippets.

@mh-firouzjah
Last active October 30, 2024 01:38
Show Gist options
  • 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"
}
]
}
@xornandor
Copy 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

Here is the new link
https://github.com/ryanoasis/nerd-fonts/releases/download/v3.2.1/Hack.zip

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