|
# Powershell Profile |
|
# 2025-05-11 |
|
# See <https://gist.github.com/rheone/37ce8d64a21b95c0b94911a7c35d58f2> for latest version |
|
|
|
################################################################ |
|
# MODULES # |
|
################################################################ |
|
|
|
# Terminal Icons - A PowerShell module to show file and folder icons in the terminal. |
|
# - <https://github.com/devblackops/Terminal-Icons> |
|
# - requires Nerd Fonts <https://www.nerdfonts.com/>; I like `FiraCode Nerd Font Mono` |
|
if (Get-Module -ListAvailable -Name Terminal-Icons) { |
|
Import-Module -Name Terminal-Icons |
|
Write-Output " ✅ 'Terminal-Icons' present." |
|
} |
|
else { |
|
Write-Warning " 👎 Module 'Terminal-Icons' is not installed." |
|
} |
|
|
|
# PSReadLine - Predictive IntelliSense |
|
# - <https://github.com/PowerShell/PSReadLine> |
|
# - <https://www.powershellgallery.com/packages/Terminal-Icons/> |
|
# - <https://www.hanselman.com/blog/adding-predictive-intellisense-to-my-windows-terminal-powershell-prompt-with-psreadline> |
|
if (Get-Module -ListAvailable -Name PSReadLine) { |
|
Import-Module PSReadLine |
|
Set-PSReadLineOption -PredictionSource History |
|
Set-PSReadLineOption -PredictionViewStyle ListView |
|
Set-PSReadLineOption -EditMode Windows |
|
Write-Output " ✅ 'PSReadLine' present." |
|
} |
|
else { |
|
Write-Warning " 👎 Module 'PSReadLine' is not installed." |
|
} |
|
|
|
# DockerCompletion - Docker command completion for PowerShell. |
|
# - <https://github.com/matt9ucci/DockerCompletion> |
|
# - <https://www.powershellgallery.com/packages/DockerCompletion/> |
|
if (Get-Module -ListAvailable -Name DockerCompletion) { |
|
Import-Module -Name DockerCompletion |
|
Write-Output " ✅ 'DockerCompletion' present." |
|
} |
|
else { |
|
Write-Warning " 👎 Module 'DockerCompletion' is not installed." |
|
} |
|
|
|
# posh-npm-completion - A npm tab completion for PowerShell. |
|
# - <https://github.com/PowerShell-Completion/npm-completion> |
|
# - <https://www.powershellgallery.com/packages/npm-completion/> |
|
if (Get-Module -ListAvailable -Name npm-completion) { |
|
Import-Module -Name npm-completion |
|
Write-Output " ✅ 'npm-completion' present." |
|
} |
|
else { |
|
Write-Warning " 👎 Module 'npm-completion' is not installed." |
|
} |
|
|
|
# posh-git - Provides prompt with Git status summary information and tab completion for Git commands, parameters, remotes and branch names. |
|
# - <https://github.com/dahlbyk/posh-git> |
|
# - <https://www.powershellgallery.com/packages/posh-git/> |
|
if (Get-Module -ListAvailable -Name posh-git) { |
|
Import-Module -Name posh-git |
|
Write-Output " ✅ 'posh-git' present." |
|
} |
|
else { |
|
Write-Warning " 👎 Module 'posh-git' is not installed." |
|
} |
|
|
|
################################################################ |
|
# COMMANDS # |
|
################################################################ |
|
|
|
# Zoxide - A smarter cd command |
|
# fzf - A command-line fuzzy finder |
|
# - <https://github.com/ajeetdsouza/zoxide> |
|
# - <https://github.com/junegunn/fzf> |
|
if (Get-Command zoxide -ErrorAction SilentlyContinue) { |
|
Invoke-Expression (& { (zoxide init powershell | Out-String) }) |
|
Write-Output " ✅ 'zoxide' present." |
|
|
|
if (Get-Command fzf -ErrorAction SilentlyContinue) { |
|
Write-Output " ✅ 'fzf' present." |
|
} |
|
else { |
|
Write-Warning " 👎 'fzf' is not installed or not in PATH." |
|
|
|
} |
|
|
|
} |
|
else { |
|
Write-Warning " 👎 'zoxide' is not installed or not in PATH." |
|
|
|
} |
|
|
|
# Oh My PoSH |
|
# - <https://www.ohmyposh.dev/> |
|
# - <https://github.com/jandedobbeleer/oh-my-posh> |
|
# - Themes located in `$env:POSH_THEMES_PATH` |
|
# - <https://www.hanselman.com/blog/my-ultimate-powershell-prompt-with-oh-my-posh-and-the-windows-terminal> |
|
if (Get-Command oh-my-posh -ErrorAction SilentlyContinue) { |
|
Write-Output " ✅ 'oh-my-posh' present." |
|
if (Test-Path -Path ~\.oh-my-posh\themes\rheone.omp.json) { |
|
oh-my-posh --init --shell pwsh --config ~\.oh-my-posh\themes\rheone.omp.json | Invoke-Expression |
|
} |
|
else { |
|
Write-Warning " 👎 failed to find custom oh-my-posh theme; using default." |
|
oh-my-posh --init --shell pwsh | Invoke-Expression |
|
} |
|
} |
|
else { |
|
Write-Warning " 👎 Module 'oh-my-posh' is not installed." |
|
} |
|
|
|
################################################################ |
|
# Functions # |
|
################################################################ |
|
|
|
# see <https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-9.0> for special paths |
|
|
|
function GoToHomeDirectory { |
|
Set-Location $([System.Environment]::GetFolderPath('UserProfile')) |
|
} |
|
|
|
function GoToCodeDirectory { |
|
Set-Location (Join-Path ([System.Environment]::GetFolderPath('UserProfile')) 'code') |
|
} |
|
|
|
function GoToDirectoryDesktop { |
|
Set-Location $([System.Environment]::GetFolderPath('Desktop')) |
|
} |
|
|
|
function GoToDirectoryDocuments { |
|
Set-Location $([System.Environment]::GetFolderPath('Personal')) |
|
} |
|
|
|
################################################################ |
|
# Argument Completers # |
|
################################################################ |
|
|
|
# PowerShell parameter completion shim for the dotnet CLI |
|
# <https://www.hanselman.com/blog/how-to-use-autocomplete-at-the-command-line-for-dotnet-git-winget-and-more> |
|
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock { |
|
param($commandName, $wordToComplete, $cursorPosition) |
|
dotnet complete --position $cursorPosition "$wordToComplete" | ForEach-Object { |
|
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) |
|
} |
|
} |
|
|
|
# winget parameter completion |
|
# <https://gist.github.com/shanselman/25f5550ad186189e0e68916c6d7f44c3> |
|
Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock { |
|
param($wordToComplete, $commandAst, $cursorPosition) |
|
[Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new() |
|
$Local:word = $wordToComplete.Replace('"', '""') |
|
$Local:ast = $commandAst.ToString().Replace('"', '""') |
|
winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object { |
|
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) |
|
} |
|
} |
|
|
|
################################################################ |
|
# Aliases # |
|
################################################################ |
|
|
|
Set-Alias -Name go-home -Value GoToHomeDirectory # goto home directory |
|
Set-Alias -Name go-code -Value GoToCodeDirectory # goto code directory |
|
Set-Alias -Name go-desk -Value GoToDirectoryDesktop # goto desktop directory |
|
Set-Alias -Name go-docs -Value GoToDirectoryDocuments #goto documents directory |
|
|
|
Set-Alias -Name vscode -Value code #ope vs code with 'code' |
|
|
|
################################################################ |
|
# MACROS # |
|
################################################################ |
|
|
|
# MACRO: Open Current Directory in Visual Studio Code `Ctrl+Shift+c` |
|
Set-PSReadLineKeyHandler -Key Ctrl+Shift+c ` |
|
-BriefDescription OpenCurrentDirectoryInVsCode ` |
|
-LongDescription "Open Current Directory in Visual Studio Code" ` |
|
-ScriptBlock { |
|
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() |
|
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("code .") |
|
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() |
|
Write-Host "Opening in Visual Studio Code..." -ForegroundColor Green |
|
} |
|
|
|
# MACRO: Open Current Directory in Explorer `Ctrl+Shift+e` |
|
Set-PSReadLineKeyHandler -Key Ctrl+Shift+e ` |
|
-BriefDescription OpenCurrentDirectoryInVsExplorer ` |
|
-LongDescription "Open Current Directory in Explorer" ` |
|
-ScriptBlock { |
|
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() |
|
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("start .") |
|
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() |
|
Write-Host "Opening in Explorer..." -ForegroundColor Green |
|
} |
|
|
|
# MACRO: Clear History `Ctrl+Shift+h` |
|
Set-PSReadLineKeyHandler -Key Ctrl+Shift+h ` |
|
-BriefDescription ClearHistory ` |
|
-LongDescription "Clear History" ` |
|
-ScriptBlock { |
|
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() |
|
[Microsoft.PowerShell.PSConsoleReadLine]::Insert("Clear-Content (Get-PSReadlineOption).HistorySavePath") |
|
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() |
|
Write-Host "History cleared." -ForegroundColor Green |
|
} |
|
|
|
# MACRO: Reload profile `Ctrl+Shift+F5` |
|
Set-PSReadLineKeyHandler -Key Ctrl+Shift+F5 ` |
|
-BriefDescription ReloadPowershellProfile ` |
|
-LongDescription "Reload Powershell Profile" ` |
|
-ScriptBlock { |
|
. $PROFILE |
|
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() |
|
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() |
|
Write-Host "PowerShell profile reloaded." -ForegroundColor Green |
|
} |
|
|
|
# MACRO: Follow Git URL `Ctrl+Shift+g` |
|
Set-PSReadLineKeyHandler -Key Ctrl+Shift+g ` |
|
-BriefDescription OpenGitUrlInBrowser ` |
|
-LongDescription "Open Git URL In Browser" ` |
|
-ScriptBlock { |
|
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() |
|
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() |
|
|
|
try { |
|
$gitUrl = git config --get remote.origin.url 2>$null |
|
|
|
if (-not $gitUrl) { |
|
Write-Host "No Git remote URL found in this directory." -ForegroundColor Red |
|
return |
|
} |
|
|
|
# Convert SSH URL to HTTPS if needed |
|
if ($gitUrl -match '^git@([^:]+):(.+?)(\.git)?$') { |
|
$domain = $matches[1] |
|
$path = $matches[2] |
|
$gitUrl = "https://$domain/$path" |
|
} |
|
|
|
# Ensure it looks like a URL |
|
if ($gitUrl -match '^(https?://[^\s]+)$') { |
|
Write-Host "Opening Git URL: $gitUrl" -ForegroundColor Green |
|
Start-Process $gitUrl |
|
} |
|
else { |
|
Write-Host "Unrecognized or unsupported Git remote URL: $gitUrl" -ForegroundColor Red |
|
} |
|
} |
|
catch { |
|
Write-Host "Failed to open Git URL: $_" -ForegroundColor Red |
|
} |
|
} |