Last active
August 31, 2025 06:34
-
-
Save C4N4242/bf71ab33cff0aee7e72004b0cb100914 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Windows上でモダンなUNIX風コマンドライン体験を実現するためのPowerShellプロファイルです。 | |
| .DESCRIPTION | |
| oh-my-poshによるプロンプト、fzf/zoxideによる高速なナビゲーション、その他モダンなUNIX代替コマンドを提供します。 | |
| .SETUP | |
| **前提:** このプロファイルはPowerShell 7以降を対象としています。Windowsに標準搭載のWindows PowerShell 5.1では動作しない可能性があります。 | |
| 1. (推奨) Microsoft Storeから最新の「PowerShell」と「Windows Terminal」をインストールします。 | |
| 2. (推奨) **Nerd Font** をインストールし、Windows Terminalのフォント設定に指定します。 | |
| oh-my-poshやlsdのアイコンが正しく表示されるようになります。(例: FiraCode NF, UDEV Gothic NFLG) | |
| wingetでもインストール可能です: `winget install -e --id Microsoft.FiraCodeNF` | |
| 3. このファイルをPowerShellのプロファイルパスに設置します (`notepad $PROFILE`で開いた場所に保存)。 | |
| 4. PowerShell (7) を管理者として新しく起動します。 | |
| 5. `. $PROFILE` を実行して、このプロファイルを現在のセッションに読み込みます。 | |
| 6. **ステップ1:** `Install-RequiredApps` コマンドを実行して、必要なツールをすべてインストールします。 | |
| 7. インストール完了後、**PowerShellを再起動**してください。 | |
| (もしコマンドが見つからない場合、PCを再起動するか、一度サインアウトしてからサインインし直すと解決することがあります) | |
| 8. **ステップ2:** 再起動後、`Setup-Apps` コマンドを実行して、アプリの初期設定を完了します。 | |
| #> | |
| # --- プロンプトの設定 --- | |
| oh-my-posh init pwsh --config 'https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/refs/heads/main/themes/atomic.omp.json' | Invoke-Expression | |
| # --- コア・ヘルパー関数 --- | |
| # UNIXの'touch'コマンドを再現し、空のファイル作成やタイムスタンプの更新を行います。 | |
| function touch { | |
| param( | |
| [string]$Path | |
| ) | |
| if (Test-Path $Path) { | |
| (Get-Item $Path).LastWriteTime = (Get-Date) | |
| } else { | |
| New-Item -Path $Path -ItemType File > $null | |
| } | |
| } | |
| # uenv (Update-Environment): 現在のセッションの環境変数Pathを再読み込みします。 | |
| function uenv { | |
| $userPath = [System.Environment]::GetEnvironmentVariable('Path', 'User') | |
| $machinePath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') | |
| $env:Path = "$userPath;$machinePath" | |
| Write-Host "✅ 環境変数'Path'を更新しました。" -ForegroundColor Green | |
| } | |
| # p-install (PowerShell-Install): 'winget install'を実行し、エラー発生時には解決策を案内します。 | |
| function p-install { | |
| param( | |
| [string[]]$Arguments | |
| ) | |
| Write-Host "wingetでインストール中: $Arguments" -ForegroundColor Cyan | |
| winget install --accept-package-agreements --accept-source-agreements @Arguments | |
| # wingetの終了コードを確認してエラーハンドリング | |
| if ($LASTEXITCODE -ne 0) { | |
| # winget自体のエラーメッセージに加えて、ユーザーへのガイドを表示 | |
| Write-Host " -> 👆 パッケージが見つからない等のエラーが発生しました。" -ForegroundColor Yellow | |
| Write-Host " パッケージIDが変更された可能性があります。`winget search <アプリ名>` で検索するか、" -ForegroundColor Yellow | |
| Write-Host " Webサイト `https://winstall.app` で正しいIDを確認してください。" -ForegroundColor Yellow | |
| } | |
| uenv | |
| } | |
| # --- 初回セットアップ用の関数群 --- | |
| # ステップ1: 新しい環境で一度だけ実行し、必要なアプリをインストールします。 | |
| function Install-RequiredApps { | |
| Write-Host "必要なアプリケーションをwingetでインストールします..." -ForegroundColor Cyan | |
| # インストールするアプリのIDリスト | |
| $apps = @( | |
| "Microsoft.PowerShell", | |
| "JanDeDobbeleer.OhMyPosh", | |
| "junegunn.fzf", | |
| "lsd-rs.lsd", | |
| "sharkdp.bat", | |
| "Neovim.Neovim", | |
| "BurntSushi.ripgrep.MSVC", | |
| "sharkdp.fd", | |
| "ajeetdsouza.zoxide", | |
| "muesli.duf", | |
| "bootandy.dust", | |
| "dalance.procs", | |
| "ClementTsang.bottom", # <-- IDを修正 | |
| "tealdeer-rs.tealdeer", # <-- IDを修正 | |
| "ducaale.xh", | |
| "Volta.Volta" | |
| ) | |
| # 各アプリを順番にインストール | |
| foreach ($appId in $apps) { | |
| p-install -e --id $appId | |
| } | |
| Write-Host "`n✅ インストールが完了しました。PowerShellを再起動してから 'Setup-Apps' を実行してください。" -ForegroundColor Green | |
| } | |
| # ステップ2: PowerShell再起動後に実行し、アプリの初期設定を行います。 | |
| function Setup-Apps { | |
| # PowerShell再起動後なので、コマンドはPATHに通っているはず | |
| Write-Host "tldrのキャッシュを更新中..." -ForegroundColor Cyan | |
| tldr --update | |
| Write-Host "`n✅ 初期セットアップが完了しました。" -ForegroundColor Green | |
| } | |
| # --- UNIX風体験のためのコマンドエイリアスと関数 --- | |
| # ls -> lsd (よく使われるフラグも関数として定義) | |
| Set-Alias -Name ls -Value lsd | |
| function l { lsd $args } | |
| function ll { lsd -l $args } | |
| function la { lsd -la $args } | |
| function lsa { lsd -a $args } | |
| # cat -> bat (シンタックスハイライト機能付き) | |
| Set-Alias -Name cat -Value bat | |
| # vim -> nvim (neovim) | |
| Set-Alias -Name vim -Value nvim | |
| # grep -> rg (ripgrep) | |
| Set-Alias -Name grep -Value rg | |
| # find -> fd | |
| Set-Alias -Name find -Value fd | |
| # df -> duf (ディスク使用量) | |
| Set-Alias -Name df -Value duf | |
| # man/help -> tldr (コマンドの早見表) | |
| function man { tldr $args } | |
| function help { tldr $args } | |
| # which -> Get-Command (コマンドのパスを検索) | |
| function which { | |
| param([string]$Command) | |
| (Get-Command $Command).Source | |
| } | |
| # --- モジュールの初期化 --- | |
| # zoxideを初期化し、賢いディレクトリ移動を有効化 | |
| # fzfがインストールされている場合、`zi`コマンドでインタクティブなディレクトリ検索が利用可能になります。 | |
| Invoke-Expression (& { (zoxide init powershell | Out-String) }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment