Skip to content

Instantly share code, notes, and snippets.

@charlesroper
Last active April 29, 2022 08:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlesroper/7c516d6a5e5d7434f8d950bb6ea5fb55 to your computer and use it in GitHub Desktop.
Save charlesroper/7c516d6a5e5d7434f8d950bb6ea5fb55 to your computer and use it in GitHub Desktop.
PROFILE
# PowerShell 7 Profile
# Load Scripts
$env:Path += ";$(Split-Path $profile)\Scripts"
# Load posh-git
Import-Module posh-git
$env:POSH_GIT_ENABLED = $true
# Load posh-sshell
Import-Module posh-sshell
# Load oh-my-posh
# $DefaultUser = 'Charles'
Import-Module oh-my-posh
Set-PoshPrompt -Theme ~/.poshthemes/custom/pwsh.omp.json
# Load ZLocation https://github.com/vors/ZLocation
Import-Module ZLocation
# PoshColor https://github.com/justabearoz/poshcolor
Import-Module PoshColor
# Load PSReadline
# https://docs.microsoft.com/en-us/powershell/module/psreadline/
If ($host.Name -eq 'ConsoleHost') { Import-Module -Name PSReadline }
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# https://devblogs.microsoft.com/powershell/announcing-psreadline-2-1-with-predictive-intellisense/
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineOption -PredictionViewStyle InlineView
# Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
# Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
## ALIASES
Set-Alias sudo gsudo
Import-Module 'C:\Users\Charles\scoop\apps\gsudo\1.1.0\gsudoModule.psm1'
Set-Alias e explorer.exe
Set-Alias np notepad.exe
function Get-Ls {
busybox.exe ls $args
}
Set-Alias ll Get-Ls
# Setup a Which alias
function Get-Path {
param ($cmd)
Get-Command -Name $cmd | Select-Object Path
}
Set-Alias which Get-Path
# Setup remove directory with recurse and force (rimraf)
function Remove-Directory {
param ($cmd)
Remove-Item -Recurse -Force $cmd
}
Set-Alias rmrf Remove-Directory
function Get-ChildDirectories {
Get-ChildItem -Directory
}
Set-Alias lsd Get-ChildDirectories
function Get-ConsoleColors {
[enum]::GetValues([System.ConsoleColor]) | Foreach-Object { Write-Host $_ -ForegroundColor $_ }
}
function Get-DirSize {
param ($dir)
$bytes = 0
$count = 0
Get-Childitem $dir | Foreach-Object {
if ($_ -is [System.IO.FileInfo]) {
$bytes += $_.Length
$count++
}
}
Write-Host "`n " -NoNewline
if ($bytes -ge 1KB -and $bytes -lt 1MB) {
Write-Host ("" + [Math]::Round(($bytes / 1KB), 2) + " KB") -ForegroundColor "White" -NoNewLine
} elseif ($bytes -ge 1MB -and $bytes -lt 1GB) {
Write-Host ("" + [Math]::Round(($bytes / 1MB), 2) + " MB") -ForegroundColor "White" -NoNewLine
} elseif ($bytes -ge 1GB) {
Write-Host ("" + [Math]::Round(($bytes / 1GB), 2) + " GB") -ForegroundColor "White" -NoNewLine
} else {
Write-Host ("" + $bytes + " bytes") -ForegroundColor "White" -NoNewLine
}
Write-Host " in " -NoNewline
Write-Host $count -ForegroundColor "White" -NoNewline
Write-Host " files"
}
function Get-DirWithSize {
param ($dir)
Get-Childitem $dir
Get-DirSize $dir
}
Remove-Item alias:dir
Remove-Item alias:ls
Set-Alias dir Get-DirWithSize
Set-Alias ls Get-DirWithSize
Remove-Item alias:cat
Set-Alias cat bat
#Set-Alias find fd
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
# Get External IP function
function Get-ExternalIP {
(Invoke-WebRequest api.ipify.org).Content
}
# WinGet tab completion
# https://github.com/microsoft/winget-cli/blob/master/doc/Completion.md
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', $_)
}
}
# Github completions
Get-ChildItem "$PROFILE\..\Completions\" | ForEach-Object {
. $_.FullName
}
# PSFzf https://github.com/kelleyma49/PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
Set-PsFzfOption -TabExpansion
# Override default tab completion
Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }
Enable-PsFzfAliases
function Invoke-FzfDirectory {
[CmdletBinding()]
param (
[Parameter()]
[String]$SearchTerm = "*"
)
# Write-Host "es $SearchTerm -path . folder: !path:node_modules !*AppData*\** !attrib:h"
es $SearchTerm -path . folder: !path:node_modules !*AppData*\** !attrib:h -sort date-recently-changed |
Invoke-Fzf |
Set-Location
}
Set-Alias ff Invoke-FzfDirectory
# For zoxide v0.8.0+
# Invoke-Expression (& {
# $hook = if ($PSVersionTable.PSVersion.Major -lt 6) { 'prompt' } else { 'pwd' }
# (zoxide init --hook $hook powershell | Out-String)
# })
function updt {
$modules = Get-InstalledModule
foreach ($module in $modules) {
Write-Host ([String]::Join(' ', 'Updating', $module.Name, $module.Version))
$moduleName = $module | Select-Object -ExpandProperty Name
Update-Module -Name $moduleName
$newModule = Get-InstalledModule -Name $module.Name
# $newModuleName = $newModule | Select-Object -ExpandProperty Name
if ($module.Version -ne $newModule.Version) {
Write-Host ([String]::Join(' ', 'Updated to', $newModule.Name, $newModule.Version)) -ForegroundColor Green
} else {
Write-Host "No update necessary" -ForegroundColor Gray
}
Get-InstalledModule $moduleName -AllVersions |
Where-Object Version -lt $newModule.Version |
Uninstall-Module
}
. $PROFILE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment