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
[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
@@ -43,166 +40,79 @@ $EDITOR = if (Test-CommandExists nvim) { 'nvim' }
elseif (Test-CommandExists notepad++) { 'notepad++' }
elseif (Test-CommandExists sublime_text) { 'sublime_text' }
else { 'notepad' }
Set-Alias -Name edit -Value $EDITOR
# 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 head {
function Get-InitialRows {
param($Path, $n = 10)
Get-Content $Path -Head $n
}
function tail {
function Get-LastRows {
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
function Get-Paths {
$delim = ':'
if ( $IsWindows ) {
$delim = ';'
}
$env:PATH -split ($delim)
}
$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."
}
}
# 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 = @{
@@ -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,7 +176,8 @@ $scriptblock = {
}
}
}
Register-ArgumentCompleter -Native -CommandName git, npm, deno -ScriptBlock $scriptblock
Register-ArgumentCompleter -Native -CommandName git, mvn -ScriptBlock $scriptblock
$scriptblock = {
param($wordToComplete, $commandAst, $cursorPosition)
@@ -276,19 +186,23 @@ $scriptblock = {
[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'