Skip to content

Instantly share code, notes, and snippets.

@kritoke
Forked from gregmac/OhMyPosh SetupGuide.md
Created November 16, 2020 17:28
Show Gist options
  • Save kritoke/c0e132478326e931dc4d128adf327a7a to your computer and use it in GitHub Desktop.
Save kritoke/c0e132478326e931dc4d128adf327a7a to your computer and use it in GitHub Desktop.
Oh-my-posh Config

Note: This is written retrospectively, so I might have missed something. Adapted from Scott Hanselman's excellent guide

  1. Install Windows Terminal (from Store)
  2. Install Scoop
  3. Install Powershell stuff:
   Install-Module posh-git -Scope CurrentUser
   Install-Module oh-my-posh -Scope CurrentUser
  1. Install nerd-font
   scoop bucket add nerd-fonts`
   scoop install Delugia-Nerd-Font-Complete`
  1. Create the other files listed here
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"profiles":
[
{
// Make changes here to the powershell.exe profile
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false,
"colorscheme": "Campbell",
"fontFace": "Delugia Nerd Font"
},
{
// Make changes here to the cmd.exe profile
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "cmd",
"commandline": "cmd.exe",
"hidden": false,
"colorscheme": "Campbell",
"fontFace": "Delugia Nerd Font"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure",
"colorscheme": "Campbell",
"fontFace": "Delugia Nerd Font"
}
],
// Add custom color schemes to this array
"schemes": [],
// Add any keybinding overrides to this array.
// To unbind a default keybinding, set the command to "unbound"
"keybindings": []
}
# ensure local modules in path (SQL Server, for example, messes this up)
$env:PSModulePath += ";${env:UserProfile}\Documents\WindowsPowerShell\Modules"
# Proper history etc
Import-Module PSReadLine
# prompt/theme
Import-Module posh-git
Import-Module oh-my-posh
Set-Theme ParadoxGreg
# https://technet.microsoft.com/en-us/magazine/hh241048.aspx
$MaximumHistoryCount = 10000;
# Produce UTF-8 by default
# https://news.ycombinator.com/item?id=12991690
$PSDefaultParameterValues["Out-File:Encoding"] = "utf8"
# fix shift+space key
Set-PSReadLineKeyHandler -chord Shift+Spacebar -Function SelfInsert
# fix aliases
Set-Alias trash Remove-ItemSafely
function settings { start-process ms-setttings: }
function list-aliases { Get-ChildItem alias: }
Remove-Item alias:\curl
Remove-Item alias:\sls
# latest msbuild
Set-Alias MSBuild "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\bin\MSBuild.exe"
# Make $lastObject save the last object output
# From http://get-powershell.com/post/2008/06/25/Stuffing-the-output-of-the-last-command-into-an-automatic-variable.aspx
function out-default {
$input | Tee-Object -var global:lastobject | Microsoft.PowerShell.Core\out-default
}
#requires -Version 2 -Modules posh-git
function Write-Theme {
param(
[bool]
$lastCommandFailed,
[string]
$with
)
# save exit code for use within this prompt, then clear it
# this is because LASTEXITCODE is only changed by executable commands, not cmdlets,
# so displaying it is misleading because it is not necessarily be the actual last thing run
$actualLastExitCode = $LASTEXITCODE
$global:LASTEXITCODE = $null
$lastColor = $sl.Colors.PromptBackgroundColor
$prompt = Write-Prompt -Object $sl.PromptSymbols.StartSymbol -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
#check the last command state and indicate if failed
If ($lastCommandFailed -or ($null -ne $actualLastExitCode -and $actualLastExitCode -ne 0)) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.FailedCommandSymbol)$actualLastExitCode " -ForegroundColor $sl.Colors.CommandFailedIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
#check for elevated prompt
If (Test-Administrator) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.ElevatedSymbol) " -ForegroundColor $sl.Colors.AdminIconForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
$user = $sl.CurrentUser
#$computer = [System.Environment]::MachineName
$path = Get-FullPath -dir $pwd
if (Test-NotDefaultUser($user)) {
$prompt += Write-Prompt -Object "$user " -ForegroundColor $sl.Colors.SessionInfoForegroundColor -BackgroundColor $sl.Colors.SessionInfoBackgroundColor
}
if (Test-VirtualEnv) {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.VirtualEnvSymbol) $(Get-VirtualEnvName) " -ForegroundColor $sl.Colors.VirtualEnvForegroundColor -BackgroundColor $sl.Colors.VirtualEnvBackgroundColor
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.VirtualEnvBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
}
else {
$prompt += Write-Prompt -Object "$($sl.PromptSymbols.SegmentForwardSymbol) " -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
}
# Writes the drive portion
$pathSplit = $path.Split($sl.PromptSymbols.PathSeparator)
if ($pathSplit.length -gt 6) {
$compactPath = ($pathSplit | select -first 2) + @([char]::ConvertFromUtf32(0x2026)) + ($pathSplit | select -last 3) -join $sl.PromptSymbols.PathSeparator
} else {
$compactPath = $path;
}
$prompt += Write-Prompt -Object "$compactPath " -ForegroundColor $sl.Colors.PromptForegroundColor -BackgroundColor $sl.Colors.PromptBackgroundColor
$vcsStatus = Get-VCSStatus
if ($vcsStatus) {
$themeInfo = Get-VcsInfo -status ($vcsStatus)
$lastColor = $themeInfo.BackgroundColor
$prompt += Write-Prompt -Object $($sl.PromptSymbols.SegmentForwardSymbol) -ForegroundColor $sl.Colors.PromptBackgroundColor -BackgroundColor $lastColor
$prompt += Write-Prompt -Object " $($themeInfo.VcInfo) " -BackgroundColor $lastColor -ForegroundColor $sl.Colors.GitForegroundColor
}
# Writes the postfix to the prompt
$prompt += Write-Prompt -Object $sl.PromptSymbols.SegmentForwardSymbol -ForegroundColor $lastColor
$timestamp = "[$(Get-Date -UFormat %T)]"
$prompt += Set-CursorForRightBlockWrite -textLength ($timestamp.Length + 1)
$prompt += Write-Prompt $timeStamp -ForegroundColor $sl.Colors.PromptForegroundColor
$prompt += Set-Newline
if ($with) {
$prompt += Write-Prompt -Object "$($with.ToUpper()) " -BackgroundColor $sl.Colors.WithBackgroundColor -ForegroundColor $sl.Colors.WithForegroundColor
}
$prompt += Write-Prompt -Object ($sl.PromptSymbols.PromptIndicator) -ForegroundColor $sl.Colors.PromptBackgroundColor
$prompt += ' '
# set title
$currentDir = Get-Item $pwd
if ($vcsStatus) {
$host.ui.rawui.WindowTitle = "$($vcsStatus.Branch) ($($currentDir.FullName))"
} else {
$host.ui.rawui.WindowTitle = "$($currentDir.Name) ($($currentDir.Parent.FullName))"
}
$prompt
}
$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.StartSymbol = ''
$sl.PromptSymbols.SegmentForwardSymbol = [char]::ConvertFromUtf32(0xE0B0)
$sl.Colors.PromptForegroundColor = [ConsoleColor]::White
$sl.Colors.PromptSymbolColor = [ConsoleColor]::White
$sl.Colors.PromptHighlightColor = [ConsoleColor]::DarkBlue
$sl.Colors.GitForegroundColor = [ConsoleColor]::Black
$sl.Colors.WithForegroundColor = [ConsoleColor]::DarkRed
$sl.Colors.WithBackgroundColor = [ConsoleColor]::Magenta
$sl.Colors.VirtualEnvBackgroundColor = [System.ConsoleColor]::Red
$sl.Colors.VirtualEnvForegroundColor = [System.ConsoleColor]::White
$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x03BB) # Lambda
$sl.PromptSymbols.FailedCommandSymbol = " $([char]::ConvertFromUtf32(0x20E0)) " # Circled slash
$sl.Options.ConsoleTitle = $false # we do this ourselves
$sl.Options.PreserveLastExitCode = $false # note: for >=2.0.465, requires https://github.com/JanDeDobbeleer/oh-my-posh/pull/307
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment