From 1c6093d4a409c143b858721dafdc8ebea2239421 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Thu, 2 Jan 2025 21:32:06 +0100 Subject: [PATCH] Clean Powershell profile --- .../Microsoft.PowerShell_profile.ps1 | 254 ++++++------------ 1 file changed, 81 insertions(+), 173 deletions(-) diff --git a/powershell/.config/powershell/Microsoft.PowerShell_profile.ps1 b/powershell/.config/powershell/Microsoft.PowerShell_profile.ps1 index ca59846..ac41eef 100644 --- a/powershell/.config/powershell/Microsoft.PowerShell_profile.ps1 +++ b/powershell/.config/powershell/Microsoft.PowerShell_profile.ps1 @@ -1,9 +1,6 @@ # set PowerShell to UTF-8 [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding -Set-Alias tig 'C:\Program Files\Git\usr\bin\tig.exe' -Set-Alias less 'C:\Program Files\Git\usr\bin\less.exe' - function Clear-Cache { # add clear cache logic here Write-Host "Clearing cache..." -ForegroundColor Cyan @@ -36,193 +33,106 @@ function Test-CommandExists { # Editor Configuration $EDITOR = if (Test-CommandExists nvim) { 'nvim' } - elseif (Test-CommandExists pvim) { 'pvim' } - elseif (Test-CommandExists vim) { 'vim' } - elseif (Test-CommandExists vi) { 'vi' } - elseif (Test-CommandExists code) { 'code' } - elseif (Test-CommandExists notepad++) { 'notepad++' } - elseif (Test-CommandExists sublime_text) { 'sublime_text' } - else { 'notepad' } -Set-Alias -Name edit -Value $EDITOR +elseif (Test-CommandExists pvim) { 'pvim' } +elseif (Test-CommandExists vim) { 'vim' } +elseif (Test-CommandExists vi) { 'vi' } +elseif (Test-CommandExists code) { 'code' } +elseif (Test-CommandExists notepad++) { 'notepad++' } +elseif (Test-CommandExists sublime_text) { 'sublime_text' } +else { 'notepad' } # Quick Access to Editing the Profile function Edit-Profile { - edit $PROFILE.CurrentUserAllHosts + edit $PROFILE.CurrentUserCurrentHost } -Set-Alias -Name ep -Value Edit-Profile -function touch($file) { "" | Out-File $file -Encoding UTF8 } +function New-File($file) { + "" | Out-File $file -Encoding UTF8 +} -function ff($name) { +function Get-RecursiveItems($name) { Get-ChildItem -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object { Write-Output "$($_.FullName)" } } -# Network Utilities -function Get-PubIP { (Invoke-WebRequest http://ifconfig.me/ip).Content } - -function reload-profile { - & $profile +function Get-PubIP { + (Invoke-WebRequest http://ifconfig.me/ip).Content } -function unzip ($file) { +function Update-Profile { + & $PROFILE +} + +function Expand-Zip ($file) { Write-Output("Extracting", $file, "to", $pwd) $fullFile = Get-ChildItem -Path $pwd -Filter $file | ForEach-Object { $_.FullName } Expand-Archive -Path $fullFile -DestinationPath $pwd } -function grep($regex, $dir) { +function Find-String($regex, $dir) { if ( $dir ) { - Get-ChildItem $dir | select-string $regex + Get-ChildItem $dir | Select-String $regex return } - $input | select-string $regex + $input | Select-String $regex } -function df { - get-volume -} - -function sed($file, $find, $replace) { +function Set-String($file, $find, $replace) { (Get-Content $file).replace("$find", $replace) | Set-Content $file } -function which($name) { - Get-Command $name | Select-Object -ExpandProperty Definition -} - -function export($name, $value) { - set-item -force -path "env:$name" -value $value; -} - -function pkill($name) { +function Stop-ProcessByName($name) { Get-Process $name -ErrorAction SilentlyContinue | Stop-Process } -function pgrep($name) { - Get-Process $name +function Get-InitialRows { + param($Path, $n = 10) + Get-Content $Path -Head $n } -function head { - param($Path, $n = 10) - Get-Content $Path -Head $n +function Get-LastRows { + param($Path, $n = 10, [switch]$f = $false) + Get-Content $Path -Tail $n -Wait:$f } -function tail { - param($Path, $n = 10, [switch]$f = $false) - Get-Content $Path -Tail $n -Wait:$f -} - -# Quick File Creation -function nf { param($name) New-Item -ItemType "file" -Path . -Name $name } - -# Directory Management -function mkcd { param($dir) mkdir $dir -Force; Set-Location $dir } - -function trash($path) { - $fullPath = (Resolve-Path -Path $path).Path - - if (Test-Path $fullPath) { - $item = Get-Item $fullPath - - if ($item.PSIsContainer) { - # Handle directory - $parentPath = $item.Parent.FullName - } else { - # Handle file - $parentPath = $item.DirectoryName - } - - $shell = New-Object -ComObject 'Shell.Application' - $shellItem = $shell.NameSpace($parentPath).ParseName($item.Name) - - if ($item) { - $shellItem.InvokeVerb('delete') - Write-Host "Item '$fullPath' has been moved to the Recycle Bin." - } else { - Write-Host "Error: Could not find the item '$fullPath' to trash." - } - } else { - Write-Host "Error: Item '$fullPath' does not exist." +function Get-Paths { + $delim = ':' + if ( $IsWindows ) { + $delim = ';' } + $env:PATH -split ($delim) } -# Simplified Process Management -function k9 { Stop-Process -Name $args[0] } - -# Enhanced Listing -function la { Get-ChildItem -Path . -Force | Format-Table -AutoSize } -function ll { Get-ChildItem -Path . -Force -Hidden | Format-Table -AutoSize } - -# Git Shortcuts -function gs { git status } - -function ga { git add . } - -function gc { param($m) git commit -m "$m" } - -function gp { git push } - -function gcl { git clone "$args" } - -function gcom { - git add . - git commit -m "$args" -} - -function lzg { - git add . - git commit -m "$args" - git push -} - -# Quick Access to System Information -function sysinfo { Get-ComputerInfo } - -# Networking Utilities -function flushdns { - Clear-DnsClientCache - Write-Host "DNS has been flushed" -} - -# Utilities -function which ($command) { - Get-Command -Name $command -ErrorAction SilentlyContinue | - Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue -} - -# Clipboard Utilities -function cpy { Set-Clipboard $args[0] } - -function pst { Get-Clipboard } - -function paths { $env:Path -split (';') } - -function make-link ($target, $link) { +function New-Link ($target, $link) { New-Item -Path $link -ItemType SymbolicLink -Value $target } +Set-Alias -Name edit -Value $EDITOR +Set-Alias -Name ep -Value Edit-Profile +Set-Alias tig 'C:\Program Files\Git\usr\bin\tig.exe' +Set-Alias less 'C:\Program Files\Git\usr\bin\less.exe' + # Enhanced PowerShell Experience # Enhanced PSReadLine Configuration $PSReadLineOptions = @{ - EditMode = 'Emacs' - HistoryNoDuplicates = $true + EditMode = 'Emacs' + HistoryNoDuplicates = $true HistorySearchCursorMovesToEnd = $true - PredictionSource = 'History' - PredictionViewStyle = 'ListView' - BellStyle = 'None' - Colors = @{ - Command = '#87CEEB' # SkyBlue (pastel) + PredictionSource = 'History' + PredictionViewStyle = 'ListView' + BellStyle = 'None' + Colors = @{ + Command = '#87CEEB' # SkyBlue (pastel) Parameter = '#98FB98' # PaleGreen (pastel) - Operator = '#FFB6C1' # LightPink (pastel) - Variable = '#DDA0DD' # Plum (pastel) - String = '#FFDAB9' # PeachPuff (pastel) - Number = '#B0E0E6' # PowderBlue (pastel) - Type = '#F0E68C' # Khaki (pastel) - Comment = '#D3D3D3' # LightGray (pastel) - Keyword = '#8367c7' # Violet (pastel) - Error = '#FF6347' # Tomato (keeping it close to red for visibility) + Operator = '#FFB6C1' # LightPink (pastel) + Variable = '#DDA0DD' # Plum (pastel) + String = '#FFDAB9' # PeachPuff (pastel) + Number = '#B0E0E6' # PowderBlue (pastel) + Type = '#F0E68C' # Khaki (pastel) + Comment = '#D3D3D3' # LightGray (pastel) + Keyword = '#8367c7' # Violet (pastel) + Error = '#FF6347' # Tomato (keeping it close to red for visibility) } } Set-PSReadLineOption @PSReadLineOptions @@ -256,8 +166,7 @@ $scriptblock = { param($wordToComplete, $commandAst, $cursorPosition) $customCompletions = @{ 'git' = @('status', 'add', 'commit', 'push', 'pull', 'clone', 'checkout') - 'npm' = @('install', 'start', 'run', 'test', 'build') - 'deno' = @('run', 'compile', 'bundle', 'test', 'lint', 'fmt', 'cache', 'info', 'doc', 'upgrade') + 'mvn' = @('compile', 'package', 'test', 'install', 'verify', 'deploy') } $command = $commandAst.CommandElements[0].Value @@ -267,28 +176,33 @@ $scriptblock = { } } } -Register-ArgumentCompleter -Native -CommandName git, npm, deno -ScriptBlock $scriptblock + +Register-ArgumentCompleter -Native -CommandName git, mvn -ScriptBlock $scriptblock $scriptblock = { param($wordToComplete, $commandAst, $cursorPosition) dotnet complete --position $cursorPosition $commandAst.ToString() | - ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) - } + ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } } + Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock $scriptblock # Setting Prompt with Oh My Posh oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\peru.omp.json" | Invoke-Expression + if (Get-Command zoxide -ErrorAction SilentlyContinue) { Invoke-Expression (& { (zoxide init --cmd cd powershell | Out-String) }) -} else { +} +else { Write-Host "zoxide command not found. Attempting to install via winget..." try { winget install -e --id ajeetdsouza.zoxide Write-Host "zoxide installed successfully. Initializing..." Invoke-Expression (& { (zoxide init powershell | Out-String) }) - } catch { + } + catch { Write-Error "Failed to install zoxide. Error: $_" } } @@ -296,40 +210,34 @@ if (Get-Command zoxide -ErrorAction SilentlyContinue) { Set-Alias -Name z -Value __zoxide_z -Option AllScope -Scope Global -Force Set-Alias -Name zi -Value __zoxide_zi -Option AllScope -Scope Global -Force -$isWin = $PSVersionTable.OS -match "Windows" -if (-not (Get-Module -ListAvailable -Name Microsoft.WinGet.CommandNotFound) -and $isWin) { +if (-not (Get-Module -ListAvailable -Name Microsoft.WinGet.CommandNotFound) -and $IsWindows) { Install-Module -Name Microsoft.WinGet.CommandNotFound -Scope CurrentUser -Force -SkipPublisherCheck } -if ($isWin) { +if ($IsWindows) { Import-Module -Name Microsoft.WinGet.CommandNotFound } -# Import Modules and External Profiles -# Ensure Terminal-Icons module is installed before importing -if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { - Install-Module -Name Terminal-Icons -Scope CurrentUser -Force -SkipPublisherCheck +function Add-Module ($moduleName) { + if (-not (Get-Module -ListAvailable -Name $moduleName)) { + Install-Module -Name $moduleName -Scope CurrentUser -Force -SkipPublisherCheck + } } + +# Import Modules and External Profiles +Add-Module Terminal-Icons Import-Module -Name Terminal-Icons -if (-not (Get-Module -ListAvailable -Name cd-extras)) { - Install-Module -Name cd-extras -Scope CurrentUser -Force -SkipPublisherCheck -} +Add-Module cd-extras Import-Module -Name cd-extras -if (-not (Get-Module -ListAvailable -Name PsHosts)) { - Install-Module -Name PsHosts -Scope CurrentUser -Force -SkipPublisherCheck -} +Add-Module PsHosts Import-Module -Name PsHosts -if (-not (Get-Module -ListAvailable -Name z)) { - Install-Module -Name z -Scope CurrentUser -Force -SkipPublisherCheck -} +Add-Module z Import-Module -Name z # PowerShell FZF integration -if (-not (Get-Module -ListAvailable -Name PSFzf)) { - Install-Module -Name PSFzf -Scope CurrentUser -Force -SkipPublisherCheck -} +Add-Module PSFzf Import-Module -Name PSFzf Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PsReadlineChordReverseHistory 'Ctrl+r'