Clean Powershell profile

This commit is contained in:
Fabio Scotto di Santolo
2025-01-02 21:32:06 +01:00
parent 0c4d4de041
commit 1c6093d4a4

View File

@@ -1,9 +1,6 @@
# set PowerShell to UTF-8 # set PowerShell to UTF-8
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding [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 { function Clear-Cache {
# add clear cache logic here # add clear cache logic here
Write-Host "Clearing cache..." -ForegroundColor Cyan Write-Host "Clearing cache..." -ForegroundColor Cyan
@@ -36,173 +33,86 @@ function Test-CommandExists {
# Editor Configuration # Editor Configuration
$EDITOR = if (Test-CommandExists nvim) { 'nvim' } $EDITOR = if (Test-CommandExists nvim) { 'nvim' }
elseif (Test-CommandExists pvim) { 'pvim' } elseif (Test-CommandExists pvim) { 'pvim' }
elseif (Test-CommandExists vim) { 'vim' } elseif (Test-CommandExists vim) { 'vim' }
elseif (Test-CommandExists vi) { 'vi' } elseif (Test-CommandExists vi) { 'vi' }
elseif (Test-CommandExists code) { 'code' } elseif (Test-CommandExists code) { 'code' }
elseif (Test-CommandExists notepad++) { 'notepad++' } elseif (Test-CommandExists notepad++) { 'notepad++' }
elseif (Test-CommandExists sublime_text) { 'sublime_text' } elseif (Test-CommandExists sublime_text) { 'sublime_text' }
else { 'notepad' } else { 'notepad' }
Set-Alias -Name edit -Value $EDITOR
# Quick Access to Editing the Profile # Quick Access to Editing the Profile
function Edit-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 { Get-ChildItem -recurse -filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object {
Write-Output "$($_.FullName)" Write-Output "$($_.FullName)"
} }
} }
# Network Utilities function Get-PubIP {
function Get-PubIP { (Invoke-WebRequest http://ifconfig.me/ip).Content } (Invoke-WebRequest http://ifconfig.me/ip).Content
function reload-profile {
& $profile
} }
function unzip ($file) { function Update-Profile {
& $PROFILE
}
function Expand-Zip ($file) {
Write-Output("Extracting", $file, "to", $pwd) Write-Output("Extracting", $file, "to", $pwd)
$fullFile = Get-ChildItem -Path $pwd -Filter $file | ForEach-Object { $_.FullName } $fullFile = Get-ChildItem -Path $pwd -Filter $file | ForEach-Object { $_.FullName }
Expand-Archive -Path $fullFile -DestinationPath $pwd Expand-Archive -Path $fullFile -DestinationPath $pwd
} }
function grep($regex, $dir) { function Find-String($regex, $dir) {
if ( $dir ) { if ( $dir ) {
Get-ChildItem $dir | select-string $regex Get-ChildItem $dir | Select-String $regex
return return
} }
$input | select-string $regex $input | Select-String $regex
} }
function df { function Set-String($file, $find, $replace) {
get-volume
}
function sed($file, $find, $replace) {
(Get-Content $file).replace("$find", $replace) | Set-Content $file (Get-Content $file).replace("$find", $replace) | Set-Content $file
} }
function which($name) { function Stop-ProcessByName($name) {
Get-Command $name | Select-Object -ExpandProperty Definition
}
function export($name, $value) {
set-item -force -path "env:$name" -value $value;
}
function pkill($name) {
Get-Process $name -ErrorAction SilentlyContinue | Stop-Process Get-Process $name -ErrorAction SilentlyContinue | Stop-Process
} }
function pgrep($name) { function Get-InitialRows {
Get-Process $name
}
function head {
param($Path, $n = 10) param($Path, $n = 10)
Get-Content $Path -Head $n Get-Content $Path -Head $n
} }
function tail { function Get-LastRows {
param($Path, $n = 10, [switch]$f = $false) param($Path, $n = 10, [switch]$f = $false)
Get-Content $Path -Tail $n -Wait:$f Get-Content $Path -Tail $n -Wait:$f
} }
# Quick File Creation function Get-Paths {
function nf { param($name) New-Item -ItemType "file" -Path . -Name $name } $delim = ':'
if ( $IsWindows ) {
# Directory Management $delim = ';'
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."
} }
$env:PATH -split ($delim)
} }
# Simplified Process Management function New-Link ($target, $link) {
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) {
New-Item -Path $link -ItemType SymbolicLink -Value $target 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 PowerShell Experience
# Enhanced PSReadLine Configuration # Enhanced PSReadLine Configuration
$PSReadLineOptions = @{ $PSReadLineOptions = @{
@@ -256,8 +166,7 @@ $scriptblock = {
param($wordToComplete, $commandAst, $cursorPosition) param($wordToComplete, $commandAst, $cursorPosition)
$customCompletions = @{ $customCompletions = @{
'git' = @('status', 'add', 'commit', 'push', 'pull', 'clone', 'checkout') 'git' = @('status', 'add', 'commit', 'push', 'pull', 'clone', 'checkout')
'npm' = @('install', 'start', 'run', 'test', 'build') 'mvn' = @('compile', 'package', 'test', 'install', 'verify', 'deploy')
'deno' = @('run', 'compile', 'bundle', 'test', 'lint', 'fmt', 'cache', 'info', 'doc', 'upgrade')
} }
$command = $commandAst.CommandElements[0].Value $command = $commandAst.CommandElements[0].Value
@@ -267,7 +176,8 @@ $scriptblock = {
} }
} }
} }
Register-ArgumentCompleter -Native -CommandName git, npm, deno -ScriptBlock $scriptblock
Register-ArgumentCompleter -Native -CommandName git, mvn -ScriptBlock $scriptblock
$scriptblock = { $scriptblock = {
param($wordToComplete, $commandAst, $cursorPosition) param($wordToComplete, $commandAst, $cursorPosition)
@@ -276,19 +186,23 @@ $scriptblock = {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
} }
} }
Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock $scriptblock Register-ArgumentCompleter -Native -CommandName dotnet -ScriptBlock $scriptblock
# Setting Prompt with Oh My Posh # Setting Prompt with Oh My Posh
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\peru.omp.json" | Invoke-Expression oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\peru.omp.json" | Invoke-Expression
if (Get-Command zoxide -ErrorAction SilentlyContinue) { if (Get-Command zoxide -ErrorAction SilentlyContinue) {
Invoke-Expression (& { (zoxide init --cmd cd powershell | Out-String) }) Invoke-Expression (& { (zoxide init --cmd cd powershell | Out-String) })
} else { }
else {
Write-Host "zoxide command not found. Attempting to install via winget..." Write-Host "zoxide command not found. Attempting to install via winget..."
try { try {
winget install -e --id ajeetdsouza.zoxide winget install -e --id ajeetdsouza.zoxide
Write-Host "zoxide installed successfully. Initializing..." Write-Host "zoxide installed successfully. Initializing..."
Invoke-Expression (& { (zoxide init powershell | Out-String) }) Invoke-Expression (& { (zoxide init powershell | Out-String) })
} catch { }
catch {
Write-Error "Failed to install zoxide. Error: $_" 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 z -Value __zoxide_z -Option AllScope -Scope Global -Force
Set-Alias -Name zi -Value __zoxide_zi -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 $IsWindows) {
if (-not (Get-Module -ListAvailable -Name Microsoft.WinGet.CommandNotFound) -and $isWin) {
Install-Module -Name Microsoft.WinGet.CommandNotFound -Scope CurrentUser -Force -SkipPublisherCheck Install-Module -Name Microsoft.WinGet.CommandNotFound -Scope CurrentUser -Force -SkipPublisherCheck
} }
if ($isWin) { if ($IsWindows) {
Import-Module -Name Microsoft.WinGet.CommandNotFound Import-Module -Name Microsoft.WinGet.CommandNotFound
} }
# Import Modules and External Profiles function Add-Module ($moduleName) {
# Ensure Terminal-Icons module is installed before importing if (-not (Get-Module -ListAvailable -Name $moduleName)) {
if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { Install-Module -Name $moduleName -Scope CurrentUser -Force -SkipPublisherCheck
Install-Module -Name Terminal-Icons -Scope CurrentUser -Force -SkipPublisherCheck }
} }
# Import Modules and External Profiles
Add-Module Terminal-Icons
Import-Module -Name Terminal-Icons Import-Module -Name Terminal-Icons
if (-not (Get-Module -ListAvailable -Name cd-extras)) { Add-Module cd-extras
Install-Module -Name cd-extras -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name cd-extras Import-Module -Name cd-extras
if (-not (Get-Module -ListAvailable -Name PsHosts)) { Add-Module PsHosts
Install-Module -Name PsHosts -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name PsHosts Import-Module -Name PsHosts
if (-not (Get-Module -ListAvailable -Name z)) { Add-Module z
Install-Module -Name z -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name z Import-Module -Name z
# PowerShell FZF integration # PowerShell FZF integration
if (-not (Get-Module -ListAvailable -Name PSFzf)) { Add-Module PSFzf
Install-Module -Name PSFzf -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name PSFzf Import-Module -Name PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PsReadlineChordReverseHistory 'Ctrl+r' Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PsReadlineChordReverseHistory 'Ctrl+r'