Added Powershell configuration

This commit is contained in:
Fabio Scotto di Santolo
2025-01-02 11:20:56 +01:00
parent 33156f0edb
commit e27790595a
2 changed files with 329 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ Below is a list of the software:
- Maven - Maven
- Moar - Moar
- Neovim/LazyVim - Neovim/LazyVim
- PowerShell
- Starship - Starship
- Tmux - Tmux
- Yazi - Yazi

View File

@@ -0,0 +1,328 @@
# 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
# Clear Windows Prefetch
Write-Host "Clearing Windows Prefetch..." -ForegroundColor Yellow
Remove-Item -Path "$env:SystemRoot\Prefetch\*" -Force -ErrorAction SilentlyContinue
# Clear Windows Temp
Write-Host "Clearing Windows Temp..." -ForegroundColor Yellow
Remove-Item -Path "$env:SystemRoot\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
# Clear User Temp
Write-Host "Clearing User Temp..." -ForegroundColor Yellow
Remove-Item -Path "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
# Clear Internet Explorer Cache
Write-Host "Clearing Internet Explorer Cache..." -ForegroundColor Yellow
Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Windows\INetCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Cache clearing completed." -ForegroundColor Green
}
# Utility Functions
function Test-CommandExists {
param($command)
$exists = $null -ne (Get-Command $command -ErrorAction SilentlyContinue)
return $exists
}
# 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
# Quick Access to Editing the Profile
function Edit-Profile {
edit $PROFILE.CurrentUserAllHosts
}
Set-Alias -Name ep -Value Edit-Profile
function touch($file) { "" | Out-File $file -Encoding UTF8 }
function ff($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 unzip ($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) {
if ( $dir ) {
Get-ChildItem $dir | select-string $regex
return
}
$input | select-string $regex
}
function df {
get-volume
}
function sed($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) {
Get-Process $name -ErrorAction SilentlyContinue | Stop-Process
}
function pgrep($name) {
Get-Process $name
}
function head {
param($Path, $n = 10)
Get-Content $Path -Head $n
}
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."
}
}
# 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) {
New-Item -Path $link -ItemType SymbolicLink -Value $target
}
# Enhanced PowerShell Experience
# Enhanced PSReadLine Configuration
$PSReadLineOptions = @{
EditMode = 'Emacs'
HistoryNoDuplicates = $true
HistorySearchCursorMovesToEnd = $true
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)
}
}
Set-PSReadLineOption @PSReadLineOptions
# Custom key handlers
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineKeyHandler -Chord 'Ctrl+d' -Function DeleteChar
Set-PSReadLineKeyHandler -Chord 'Ctrl+w' -Function BackwardDeleteWord
Set-PSReadLineKeyHandler -Chord 'Alt+d' -Function DeleteWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+LeftArrow' -Function BackwardWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+RightArrow' -Function ForwardWord
Set-PSReadLineKeyHandler -Chord 'Ctrl+z' -Function Undo
Set-PSReadLineKeyHandler -Chord 'Ctrl+y' -Function Redo
# Custom functions for PSReadLine
Set-PSReadLineOption -AddToHistoryHandler {
param($line)
$sensitive = @('password', 'secret', 'token', 'apikey', 'connectionstring')
$hasSensitive = $sensitive | Where-Object { $line -match $_ }
return ($null -eq $hasSensitive)
}
# Improved prediction settings
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadLineOption -MaximumHistoryCount 10000
# Custom completion for common commands
$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')
}
$command = $commandAst.CommandElements[0].Value
if ($customCompletions.ContainsKey($command)) {
$customCompletions[$command] | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
}
Register-ArgumentCompleter -Native -CommandName git, npm, deno -ScriptBlock $scriptblock
$scriptblock = {
param($wordToComplete, $commandAst, $cursorPosition)
dotnet complete --position $cursorPosition $commandAst.ToString() |
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 {
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 {
Write-Error "Failed to install zoxide. Error: $_"
}
}
Set-Alias -Name z -Value __zoxide_z -Option AllScope -Scope Global -Force
Set-Alias -Name zi -Value __zoxide_zi -Option AllScope -Scope Global -Force
#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
}
Import-Module -Name Terminal-Icons
if (-not (Get-Module -ListAvailable -Name cd-extras)) {
Install-Module -Name cd-extras -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name cd-extras
if (-not (Get-Module -ListAvailable -Name PsHosts)) {
Install-Module -Name PsHosts -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name PsHosts
if (-not (Get-Module -ListAvailable -Name z)) {
Install-Module -Name z -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name z
# PowerShell FZF integration
if (-not (Get-Module -ListAvailable -Name PSFzf)) {
Install-Module -Name PSFzf -Scope CurrentUser -Force -SkipPublisherCheck
}
Import-Module -Name PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+f' -PsReadlineChordReverseHistory 'Ctrl+r'