Last active
October 21, 2025 06:22
-
-
Save bakamake/38f4f2c1ddaa35595eca67c6ff714344 to your computer and use it in GitHub Desktop.
profile for pwsh 7.xx
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
| #消除一个微软的pwsh启动bug,使在开始菜单启动pwsh时会进入home作为工作目录 | |
| if(($PWD).Path -eq "C:\Program Files\WindowsApps\Microsoft.PowerShell_7.5.3.0_x64__8wekyb3d8bbwe" -or | |
| ($PWD).Path -eq "C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy" ){ | |
| Set-Location $HOME | |
| } | |
| #挂载WSL virtual network drive file system to PSdrive | |
| #这样做可以使用命令ls wsl: | |
| if($null -eq (get-psdrive wsl -ErrorAction SilentlyContinue).name){new-psdrive -Name "WSL" -PSProvider FileSystem -Root "\\WSL$\Debian"} | |
| #环境设置 | |
| #scoop 方便安装scoop,也可以设置在其他位置比如 C:\users\bakam\onedrive\scoop | |
| $env:scoop = 'C:\users\bakam\scoop' | |
| $env:http_proxy = 'http://127.0.0.1:10808' | |
| $env:https_proxy = 'http://127.0.0.1:10808' | |
| $env:all_proxy = 'socks5://127.0.0.1:10808' | |
| # 定义self变量为个人scoop bucket git仓库路径 | |
| $self = "C:\Users\bakam\oneDrive\Desktop\self-scoop-bucket" | |
| #所有配置,使用mklink创建链接让OneDrive同步 | |
| $config = "C:\Users\bakam\OneDrive\Desktop\all_config\" | |
| #windows开发目录 | |
| $dev = "C:\Users\bakam\OneDrive\Dev" | |
| # 设置PowerShell默认编码为UTF-8,确保脚本和输出文件使用UTF-8编码, 消除一些奇怪的中文输入bug | |
| $PSDefaultParameterValues['Out-File:Encoding'] = 'utf8' | |
| # 设置控制台输出编码为UTF-8,确保在控制台中正确显示UTF-8字符。 | |
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
| ##定义快捷键Ctrl+w,快速输入exit并执行,退出当前PowerShell会话 | |
| Set-PSReadLineKeyHandler -Key Ctrl+w -LongDescription "exit" -ScriptBlock { | |
| [Microsoft.PowerShell.PSConsoleReadLine]::RevertLine() | |
| [Microsoft.PowerShell.PSConsoleReadLine]::Insert("exit") | |
| [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine() | |
| } | |
| # 定义exp进入本工作目录下的explorer快捷命令 | |
| function exp {explorer $pwd} | |
| function py {uv run $args } | |
| function wsl {wsl.exe ~ } | |
| # 定义 claude 函数,调用 WSL 中的 claude 命令,切换到 /home/bakamake 目录后执行 | |
| # 使用 wsl.exe -e /bin/bash -c "cd /home/bakamake && claude" 确保正确的目录上下文 | |
| # 确保 WSL 已安装,claude CLI (@anthropic-ai/claude-code) 位于 PATH 中 | |
| function claude { | |
| param ( | |
| # 接受所有剩余参数,传递给 claude 命令 | |
| [Parameter(ValueFromRemainingArguments=$true)] | |
| [string[]]$Arguments | |
| ) | |
| try { | |
| # 构建 WSL 命令,切换到 /home/bakamake 后运行 claude | |
| if ($Arguments) { | |
| # 转义参数以防止 shell 注入 | |
| $escapedArgs = $Arguments | ForEach-Object { "'$($_ -replace "'", "'\\''")'" } | |
| $command = "cd /home/bakamake && claude $escapedArgs" | |
| wsl.exe -e /bin/bash --login -i -c $command 2>&1 | Out-String | |
| } else { | |
| wsl.exe -e /bin/bash --login -i -c "cd /home/bakamake && claude" | |
| } | |
| } | |
| catch { | |
| # 捕获并显示错误信息,提供具体诊断 | |
| Write-Error "Failed to execute claude command: $_" | |
| Write-Host "Troubleshooting steps:" -ForegroundColor Cyan | |
| Write-Host "- Verify WSL is installed and configured: Run 'wsl --list --verbose'" -ForegroundColor Cyan | |
| Write-Host "- Ensure claude CLI is installed: Run 'wsl.exe -e which claude'" -ForegroundColor Cyan | |
| Write-Host "- Check claude usage: Run 'wsl.exe -e claude --help'" -ForegroundColor Cyan | |
| Write-Host "- 如果还没有在wsl安装claude和npm" | |
| Write-Host " sudo apt update && sudo apt install nodejs npm -y" -ForegroundColor Cyan | |
| Write-Host " npm install -g @anthropic-ai/claude-code" -ForegroundColor Cyan | |
| } | |
| } | |
| ## scoop-search工具替代scoop search原生命令 | |
| . ([ScriptBlock]::Create((& scoop-search --hook | Out-String))) | |
| # 导入Visual Studio开发者命令行环境模块,并启用模块 | |
| #可选命令:cl,link,lib | |
| $vsEnvSetState = $env:VSINSTALLDIR -and $env:VCINSTALLDIR | |
| if (-not $vsEnvSetState) { | |
| Write-Host "初始化 Visual Studio 开发环境..." -ForegroundColor Yellow | |
| Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" | |
| Enter-VsDevShell -InstallPath "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools" -DevCmdArguments "-arch=x64 -host_arch=x64" | |
| } else { | |
| Write-Host "Visual Studio 环境已就绪" -ForegroundColor Green | |
| } | |
| #不知道为什么我的Microsoft.VisualStudio.DevShell模块没添加dotnet执行相关到path用来调用dotnet命令 | |
| if ($env:Path -notmatch "C:\\Program Files\\dotnet\\") { | |
| $env:Path += ";C:\Program Files\dotnet\" | |
| } | |
| Write-Output "查看环境变量请使用以下命令:" | |
| Write-Output "tip : ls env:" | |
| Write-Output "tip : ls HKLM::" | |
| Write-Output "tip : Get-PSDrive 查看可以和上面一样的虚拟驱动器," | |
| Write-Output "psdrive抽象了一些东西用来帮助pwsh 命令更好的集成他们" | |
| Write-Output "tip : `$env:PATH -split ';'" | |
| Write-Output "tip : rr 命令将文件夹/文件转移到`$Recycle" | |
| set-Alias la Format-List | |
| Write-Output "tip : 加上管道 | la 或者 | fl 展开被 ... 折叠的pwsh命令对象" | |
| # 加载必要的程序集 | |
| Add-Type -AssemblyName Microsoft.VisualBasic | |
| # 定义函数:将文件/文件夹移到回收站 | |
| # 该函数使用了 Microsoft.VisualBasic.FileIO 命名空间的方法 | |
| # 来实现将指定路径的文件或文件夹移动到回收站的功能 | |
| # 这个命名空间的年龄有点大,而且不够安全,似乎不会认可文件和文件夹权限,不要用这个命令删除高权限文件和文件夹 | |
| # 未来微软可能会淘汰掉这个命名空间,届时需要寻找替代方案 | |
| function Remove-toRecycle { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] | |
| [string[]]$Path | |
| ) | |
| process { | |
| foreach ($item in $Path) { | |
| $fullpath = (Get-Item -Path $item -ErrorAction SilentlyContinue).FullName | |
| if ($fullpath) { | |
| if (Test-Path -Path $fullpath -PathType Container) { | |
| # 处理文件夹 | |
| [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory( | |
| $fullpath, | |
| [Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs, | |
| [Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin, | |
| [Microsoft.VisualBasic.FileIO.UICancelOption]::DoNothing | |
| ) | |
| } else { | |
| # 处理文件 | |
| [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile( | |
| $fullpath, | |
| [Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs, | |
| [Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin, | |
| [Microsoft.VisualBasic.FileIO.UICancelOption]::DoNothing | |
| ) | |
| } | |
| } else { | |
| Write-Error "'$item' 未找到" | |
| } | |
| } | |
| } | |
| } | |
| # 设置 rr 别名为该函数(AllScope 确保在所有作用域生效) | |
| Set-Alias Rr Remove-toRecycle -Option AllScope |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment