mirror of
https://github.com/js0ny/dotfiles.git
synced 2025-12-21 08:43:00 +00:00
chezmoi: powershell
This commit is contained in:
parent
81da252906
commit
298b2290a8
13 changed files with 308 additions and 278 deletions
|
|
@ -1,41 +0,0 @@
|
|||
# $DOTFILES/tools\powershell\Aliases.ps1
|
||||
# Date: 2024-12-01
|
||||
# Author: js0ny
|
||||
# Aliases for PowerShell
|
||||
|
||||
# Unix Shell Equivalents #
|
||||
Set-Alias "touch" "New-Item"
|
||||
${function:l} = { Get-ChildItem -Force }
|
||||
${function:tree} = { lsd --tree $args }
|
||||
Set-Alias "which" "Get-Command"
|
||||
|
||||
# Editors #
|
||||
Set-Alias "v" "nvim"
|
||||
Set-Alias "c" "code"
|
||||
Get-Command neovide -ErrorAction SilentlyContinue > $null && Set-Alias "gvi" "neovide"
|
||||
|
||||
|
||||
|
||||
# File Creation #
|
||||
function mkcd { param ( [string] $dirname) New-Item -ItemType Directory -Name $dirname && Set-Location $dirname }
|
||||
function tc { param ( [string] $filename) New-Item $filename && code $filename }
|
||||
function tv { param ( [string] $filename) New-Item $filename && nvim $filename }
|
||||
function cdls { param( [string] $dirname) Set-Location $dirname && Get-ChildItem }
|
||||
function zls { param( [string] $dirname) z $dirname && Get-ChildItem }
|
||||
function zl { param( [string] $dirname) z $dirname && lsd }
|
||||
function mt { param( [string] $dirname) New-Item -ItemType File -Name $dirname -Force }
|
||||
|
||||
|
||||
### Dev ###
|
||||
|
||||
# Python & Conda #
|
||||
Set-Alias "py" "python"
|
||||
Set-Alias "ipy" "ipython"
|
||||
|
||||
if ($Env:WEZTERM) {
|
||||
# Environment variable injected by wezterm/wezterm.lua
|
||||
${function:icat} = { wezterm imgcat $args }
|
||||
}
|
||||
elseif ($Env:KITTY) {
|
||||
${function:icat} = { kitty +kitten icat $args }
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
function Invoke-Completion {
|
||||
param ([string]$command)
|
||||
switch ($command) {
|
||||
'docker' { docker completion powershell | Out-String | Invoke-Expression }
|
||||
'dotnet' {
|
||||
# https://learn.microsoft.com/en-us/dotnet/core/tools/enable-tab-autocomplete#powershell
|
||||
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock {
|
||||
param($wordToComplete, $commandAst, $cursorPosition)
|
||||
dotnet complete --position $cursorPosition "$commandAst" | ForEach-Object {
|
||||
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
|
||||
}
|
||||
}
|
||||
}
|
||||
'git' { Import-Module Posh-Git }
|
||||
'hugo' { hugo completion powershell | Out-String | Invoke-Expression }
|
||||
'just' { just --completions powershell | Out-String | Invoke-Expression }
|
||||
'pip' { pip completion --powershell | Out-String | Invoke-Expression }
|
||||
'pixi' { pixi completion --shell powershell | Out-String | Invoke-Expression }
|
||||
'rg' { rg --generate complete-powershell | Out-String | Invoke-Expression }
|
||||
'rustup' { rustup completions powershell rustup | Out-String | Invoke-Expression }
|
||||
'uv' { uv generate-shell-completion powershell | Out-String | Invoke-Expression }
|
||||
'wezterm' { wezterm shell-completion --shell power-shell | Out-String | Invoke-Expression }
|
||||
'winget' {
|
||||
# https://learn.microsoft.com/en-us/windows/package-manager/winget/tab-completion
|
||||
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', $_)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Set-Alias "icmp" "Invoke-Completion"
|
||||
|
||||
Register-ArgumentCompleter -CommandName Invoke-Completion -ParameterName 'command' -ScriptBlock {
|
||||
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
|
||||
|
||||
$cmds = @('docker', 'dotnet', 'git', 'hugo', 'just', 'pip', 'pixi', 'rg', 'rustup', 'uv', 'wezterm', 'winget')
|
||||
|
||||
$cmds | Where-Object { $_ -like "$wordToComplete*" }
|
||||
}
|
||||
|
||||
|
||||
if ($isWindows) {
|
||||
Invoke-Completion winget # Enable winget tab completion by default
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
# $DOTFILES/tools\powershell\Environment.ps1
|
||||
# Date: 2024-12-01
|
||||
# Author: js0ny
|
||||
# Sourced by Microsoft.PowerShell_profile.ps1
|
||||
|
||||
|
||||
# This file stores only environment variables that only called by
|
||||
# interactive session.
|
||||
|
||||
$env:IPYTHONDIR = "$env:AppData\ipython"
|
||||
$env:EDITOR = "code --wait"
|
||||
$env:VISUAL = "code --wait"
|
||||
$env:PAGER = "less"
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
# aka PSReadLine
|
||||
|
||||
# read key: [System.Console]::ReadKey()
|
||||
# get current bindings: Get-PSReadLineKeyHandler
|
||||
|
||||
# PSReadLineOptions
|
||||
Set-PSReadLineOption -EditMode Vi # Vi Keybindings
|
||||
Set-PSReadLineOption -PredictionViewStyle InlineView
|
||||
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
|
||||
# Set-PSReadLineOption -ContinuationPrompt "`e[36m CR > " # Use Starship instead
|
||||
# PSReadLineKeyHandlers
|
||||
Set-PSReadLineKeyHandler -Chord "Control+Oem4" -Function ViCommandMode -ViMode Insert # ^[ to Escape
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+a" -Function BeginningOfLine
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+e" -Function EndOfLine
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+p" -Function PreviousHistory
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function AcceptNextSuggestionWord
|
||||
Set-PSReadLineKeyHandler -Chord "Alt+f" -Function AcceptSuggestion
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+n" -Function NextHistory
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+w" -Function BackwardKillWord
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+Backspace" -Function BackwardKillWord
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+Shift+Backspace" -Function BackwardKillLine
|
||||
Set-PSReadLineKeyHandler -Chord "Alt+Backspace" -Function BackwardKillLine
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+Delete" -Function KillWord
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+Shift+Delete" -Function KillLine
|
||||
Set-PSReadLineKeyHandler -Chord "Alt+Delete" -Function KillLine
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+c" -Function CopyOrCancelLine -ViMode Insert
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl+c" -Function CopyOrCancelLine -ViMode Command
|
||||
## Use <Tab> to Invoke MenuComplete
|
||||
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
|
||||
|
||||
. $(Join-Path $DOTFILES "tools" "powershell" "Keymap" "Colemak.ps1")
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
## Colemak hnei
|
||||
Set-PSReadLineKeyHandler -Chord "n" -Function NextHistory -ViMode Command
|
||||
Set-PSReadLineKeyHandler -Chord "e" -Function PreviousHistory -ViMode Command
|
||||
Set-PSReadLineKeyHandler -Chord "i" -Function ViForwardChar -ViMode Command
|
||||
## Similar position to [i] in QWERTY
|
||||
Set-PSReadLineKeyHandler -Chord "l" -Function ViInsertMode -ViMode Command
|
||||
Set-PSReadlineKeyHandler -Chord "L" -Function ViInsertAtBegining -ViMode Command
|
||||
## Ne{[k]s}t
|
||||
Set-PSReadLineKeyHandler -Chord "k" -Function RepeatSearch -ViMode Command
|
||||
Set-PSReadLineKeyHandler -Chord "K" -Function RepeatSearchBackward -ViMode Command
|
||||
## [j]ump
|
||||
Set-PSReadLineKeyHandler -Chord "j" -Function NextWordEnd -ViMode Command
|
||||
Set-PSReadLineKeyHandler -Chord "J" -Function ViEndOfGlob -ViMode Command
|
||||
## Use N to Join
|
||||
Set-PSReadLineKeyHandler -Chord "N" -Function ViJoinLines -ViMode Command
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
# Import-Module -Name Terminal-Icons
|
||||
Import-Module -Name CompletionPredictor
|
||||
if ($IsWindows) {
|
||||
# Chocolatey
|
||||
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
|
||||
if (Test-Path($ChocolateyProfile)) { Import-Module "$ChocolateyProfile" }
|
||||
# WinGet
|
||||
Import-Module -Name Microsoft.WinGet.CommandNotFound #f45873b3-b655-43a6-b217-97c00aa0db58
|
||||
}
|
||||
|
||||
Import-Module -Name PSFzf
|
||||
|
||||
Set-PSReadLineKeyHandler -Chord "Ctrl-r" -ScriptBlock { Invoke-FuzzyHistory }
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
# ${function:~} = { Set-Location -Path ~ } cd is better
|
||||
${function:...} = { Set-Location -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) }
|
||||
${function:....} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
${function:.....} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
${function:......} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
|
||||
# Absolute navigation
|
||||
${function:docs} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "Documents") }
|
||||
${function:down} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "Downloads") }
|
||||
${function:dt} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "Desktop") }
|
||||
${function:one} = { Set-Location -Path (Join-Path -Path $HOME -ChildPath "OneDrive") }
|
||||
|
|
@ -1,144 +0,0 @@
|
|||
# $DOTFILES/tools\powershell\Prompt.ps1
|
||||
# Date: 2024-12-01
|
||||
# Author: js0ny
|
||||
# Use starship to set prompt
|
||||
|
||||
# Invoke-Expression (&starship init powershell)
|
||||
|
||||
|
||||
function formatFG {
|
||||
param(
|
||||
[string]$RGB
|
||||
)
|
||||
if ($RGB -eq "-1") {
|
||||
return "`e[39m"
|
||||
}
|
||||
$R = $RGB.Substring(0, 2)
|
||||
$G = $RGB.Substring(2, 2)
|
||||
$B = $RGB.Substring(4, 2)
|
||||
# Convert hex to RGB
|
||||
$R = [int]::Parse($R, [System.Globalization.NumberStyles]::HexNumber)
|
||||
$G = [int]::Parse($G, [System.Globalization.NumberStyles]::HexNumber)
|
||||
$B = [int]::Parse($B, [System.Globalization.NumberStyles]::HexNumber)
|
||||
return "`e[38;2;$R;$G;${B}m"
|
||||
}
|
||||
|
||||
function formatBG {
|
||||
param(
|
||||
[string]$RGB
|
||||
)
|
||||
if ($RGB -eq "-1") {
|
||||
return "`e[49m"
|
||||
}
|
||||
$R = $RGB.Substring(0, 2)
|
||||
$G = $RGB.Substring(2, 2)
|
||||
$B = $RGB.Substring(4, 2)
|
||||
# Convert hex to RGB
|
||||
$R = [int]::Parse($R, [System.Globalization.NumberStyles]::HexNumber)
|
||||
$G = [int]::Parse($G, [System.Globalization.NumberStyles]::HexNumber)
|
||||
$B = [int]::Parse($B, [System.Globalization.NumberStyles]::HexNumber)
|
||||
return "`e[48;2;$R;$G;${B}m"
|
||||
}
|
||||
function formatPowerlineText {
|
||||
param(
|
||||
[string]$FG,
|
||||
[string]$BG,
|
||||
[string]$PLBG,
|
||||
[string]$Text
|
||||
)
|
||||
$ResumeSequece = "`e[0m"
|
||||
$TextFG = formatFG -RGB $FG
|
||||
$TextBG = formatBG -RGB $BG
|
||||
$PLFG = formatFG -RGB $BG
|
||||
$PLBG = formatBG -RGB $PLBG
|
||||
return "$TextFG$TextBG $Text $PLFG$PLBG$ResumeSequece"
|
||||
}
|
||||
|
||||
function prompt {
|
||||
if ($pwd.Path -eq $HOME) {
|
||||
$cwd = "~"
|
||||
} else {
|
||||
$cwd = $pwd.ProviderPath
|
||||
}
|
||||
$time = Get-Date -Format "HH:mm"
|
||||
$ResumeSequece = "`e[0m"
|
||||
$FG0 = "FFFFFF"
|
||||
$BG0 = "9A348E"
|
||||
$BG1 = "DA627D"
|
||||
$BG2 = "FCA17D"
|
||||
$FG1 = "035E82"
|
||||
$time = formatPowerlineText $FG0 $BG0 $BG1 $time
|
||||
$cwd = formatPowerlineText $FG0 $BG1 $BG2 $cwd
|
||||
$ps = formatPowerlineText $FG1 $BG2 "-1" "PS"
|
||||
|
||||
"$time$cwd$ps$ResumeSequece "
|
||||
|
||||
}
|
||||
|
||||
function Invoke-Starship {
|
||||
Invoke-Expression (&starship init powershell)
|
||||
}
|
||||
|
||||
Import-Module Catppuccin
|
||||
|
||||
$Flavor = $Catppuccin['Mocha']
|
||||
|
||||
# The following colors are used by PowerShell's formatting
|
||||
# Again PS 7.2+ only
|
||||
$PSStyle.Formatting.Debug = $Flavor.Sky.Foreground()
|
||||
$PSStyle.Formatting.Error = $Flavor.Red.Foreground()
|
||||
$PSStyle.Formatting.ErrorAccent = $Flavor.Blue.Foreground()
|
||||
$PSStyle.Formatting.FormatAccent = $Flavor.Teal.Foreground()
|
||||
$PSStyle.Formatting.TableHeader = $Flavor.Rosewater.Foreground()
|
||||
$PSStyle.Formatting.Verbose = $Flavor.Yellow.Foreground()
|
||||
$PSStyle.Formatting.Warning = $Flavor.Peach.Foreground()
|
||||
|
||||
$Colors = @{
|
||||
# Largely based on the Code Editor style guide
|
||||
# Emphasis, ListPrediction and ListPredictionSelected are inspired by the Catppuccin fzf theme
|
||||
|
||||
# Powershell colours
|
||||
Emphasis = $Flavor.Red.Foreground()
|
||||
Selection = $Flavor.Surface0.Background()
|
||||
|
||||
# PSReadLine prediction colours
|
||||
InlinePrediction = $Flavor.Overlay0.Foreground()
|
||||
ListPrediction = $Flavor.Mauve.Foreground()
|
||||
ListPredictionSelected = $Flavor.Surface0.Background()
|
||||
|
||||
# Syntax highlighting
|
||||
Command = $Flavor.Blue.Foreground()
|
||||
Comment = $Flavor.Overlay0.Foreground()
|
||||
Default = $Flavor.Text.Foreground()
|
||||
Error = $Flavor.Red.Foreground()
|
||||
Keyword = $Flavor.Mauve.Foreground()
|
||||
Member = $Flavor.Rosewater.Foreground()
|
||||
Number = $Flavor.Peach.Foreground()
|
||||
Operator = $Flavor.Sky.Foreground()
|
||||
Parameter = $Flavor.Pink.Foreground()
|
||||
String = $Flavor.Green.Foreground()
|
||||
Type = $Flavor.Yellow.Foreground()
|
||||
Variable = $Flavor.Lavender.Foreground()
|
||||
}
|
||||
|
||||
# Set the colours
|
||||
Set-PSReadLineOption -Colors $Colors
|
||||
|
||||
# Smarter cd
|
||||
# Init zoxide apter starship to avoid conflicts
|
||||
# https://github.com/ajeetdsouza/zoxide/issues/723
|
||||
if (Get-Command zoxide -ErrorAction SilentlyContinue) {
|
||||
Invoke-Expression (& { (zoxide init powershell | Out-String) })
|
||||
${function:...} = { z -Path (Join-Path -Path .. -ChildPath ..) }
|
||||
${function:....} = { z -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) }
|
||||
${function:.....} = { z -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
${function:......} = { z -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
${function:z-} = { z - }
|
||||
}
|
||||
else {
|
||||
${function:...} = { Set-Location -Path (Join-Path -Path .. -ChildPath ..) }
|
||||
${function:....} = { Set-Location -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) }
|
||||
${function:.....} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
${function:......} = { Set-Location -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path .. -ChildPath ..) -ChildPath ..) -ChildPath ..) -ChildPath ..) }
|
||||
${function:z-} = { Set-Location - }
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
# PowerShell Profile
|
||||
|
||||
This is the cross-platform PowerShell profile for PowerShell Core
|
||||
|
||||
```text
|
||||
.
|
||||
├── Aliases.ps1
|
||||
├── Keymap.ps1 # keymaps, optimize for Vi-Mode and Colemak
|
||||
├── Modules.ps1
|
||||
├── Navigation.ps1
|
||||
├── Prompt.ps1
|
||||
├── readme.md
|
||||
└── Scripts.ps1
|
||||
|
||||
1 directory, 7 files
|
||||
```
|
||||
|
||||
| Keymap | Action | Mode |
|
||||
| ------ | -------------------- | ------ |
|
||||
| `^a` | To Beginning of Line | All |
|
||||
| `^e` | To End of Line | All |
|
||||
| `^[` | To Normal Mode | Insert |
|
||||
|
||||
## `Get-Command` vs `which.exe` under Windows
|
||||
|
||||
```powershell
|
||||
PS > hyperfine "pwsh.exe -NoProfile -Command 'Get-Command which'" "pwsh.exe -NoProfile -Command 'which which'" --warmup 10
|
||||
Benchmark 1: pwsh.exe -NoProfile -Command 'Get-Command which'
|
||||
Time (mean ± σ): 152.1 ms ± 1.3 ms [User: 112.2 ms, System: 89.3 ms]
|
||||
Range (min … max): 150.0 ms … 155.3 ms 18 runs
|
||||
|
||||
Benchmark 2: pwsh.exe -NoProfile -Command 'which which'
|
||||
Time (mean ± σ): 153.7 ms ± 6.4 ms [User: 126.7 ms, System: 101.9 ms]
|
||||
Range (min … max): 147.8 ms … 169.5 ms 19 runs
|
||||
|
||||
Summary
|
||||
pwsh.exe -NoProfile -Command 'Get-Command which' ran
|
||||
1.01 ± 0.04 times faster than pwsh.exe -NoProfile -Command 'which which'
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue