Soyez Feignant sur Windows avec $PROFILE !

Windows
311 mots

Introduction

Le $PROFILE c'est le même type de fichier que .zshrc ou .bashrc, il est fait pour pouvoir configurer un profil de terminal qui se chargera sur chaque nouvelle session. On va faire la même chose ici que dans l'article précédent qui lui était pour Linux.

Config Sympa du Fichier

Ouvrez-le

notepad $PROFILE

Si il existe pas :

New-Item -ItemType File -Path $PROFILE -Force

Puis :

# =====================================================
#    PowerShell Profile — Edition Feignant Pro 3000™
# =====================================================

Set-Alias ll Get-ChildItem
Set-Alias la "Get-ChildItem -Force"
Set-Alias grep Select-String
Set-Alias cat Get-Content
Set-Alias .. Set-Location ..
Set-Alias ... { Set-Location ../.. }
Set-Alias update Update-Module
Set-Alias hist Get-History
Set-Alias clr Clear-Host


function reload {
    "🔄 Reloading profile..." | Write-Host -ForegroundColor Cyan
    . $PROFILE
}

function please {
    Start-Process powershell -Verb RunAs -ArgumentList $args
}

function fuck {
    if ($LASTEXITCODE -ne 0 -and $history.Count -gt 0) {
        Write-Host " Retrying: $($history[-1].CommandLine)" -ForegroundColor Red
        Invoke-Expression $history[-1].CommandLine
    } else {
        Write-Host " Rien à corriger" -ForegroundColor Green
    }
}

function timer {
    param([ScriptBlock]$code)
    $sw = [System.Diagnostics.Stopwatch]::StartNew()
    & $code
    $sw.Stop()
    Write-Host " Temps écoulé: $($sw.Elapsed.TotalSeconds) sec" -ForegroundColor Yellow
}

function cleanup {
    Write-Host "Suppression des fichiers inutiles..." -ForegroundColor Cyan
    Get-ChildItem -Recurse -Include *.pyc, .DS_Store, Thumbs.db -ErrorAction SilentlyContinue | Remove-Item -Force
    Write-Host " Nettoyage terminé."
}

function mkcd {
    param([string]$name)
    New-Item -ItemType Directory -Path $name -Force | Out-Null
    Set-Location -Path $name
}

function go {
    param([string]$name)
    $dir = Get-ChildItem -Directory -Recurse | Where-Object { $_.Name -like "*$name*" } | Select-Object -First 1
    if ($dir) {
        Set-Location $dir.FullName
    } else {
        Write-Host " Aucun dossier trouvé contenant '$name'" -ForegroundColor Red
    }
}

function extract {
    param([string]$archive)
    if (-not (Test-Path $archive)) {
        Write-Host " Fichier introuvable: $archive" -ForegroundColor Red
        return
    }
    switch -Wildcard ($archive) {
        "*.zip"      { Expand-Archive -Path $archive -DestinationPath . }
        "*.tar.gz"   { tar -xzf $archive }
        "*.tar.bz2"  { tar -xjf $archive }
        "*.rar"      { & "C:\Program Files\WinRAR\UnRAR.exe" x $archive . }
        default      { Write-Host " Format non reconnu." -ForegroundColor DarkYellow }
    }
}

#  Prompt stylé
function global:prompt {
    $user = [System.Environment]::UserName
    $path = $(Get-Location)
    $git = $(if (Test-Path .git) { "" + (git branch --show-current 2>$null) })
    $ok = if ($?) { "✔" } else { "" }
    "$user $ok [$path] $git> "
}

Write-Host " PowerShell prêt." -ForegroundColor Green
Write-Host " $(Get-Date -Format 'dddd dd MMM yyyy HH:mm')" -ForegroundColor DarkGray
Write-Host ""

function edit-profile {
    code $PROFILE  # utilise VS Code
}

Ensuite tu charges les modifications avec reload puis tout est prêt.