Skip to content

WSL 优化教程

WSL 中运行 Claude Code 最常见的卡顿问题来自:Windows PATH 继承、跨文件系统访问慢、Claude 内部调用 PowerShell 的 bug。以下配置可以显著提升流畅度。

禁用 Windows 路径互操作

在 WSL 终端中执行以下命令,禁用 Windows PATH 继承以提升性能:

bash
config="/etc/wsl.conf";content="[interop]\nappendWindowsPath=false";echo "=== 配置前 ===";if [ -f "$config" ];then cat "$config";else echo "(文件不存在)";fi;echo "";if [ ! -f "$config" ];then echo -e "$content"|sudo tee "$config">/dev/null;echo "=== 配置后 ===";cat "$config";echo -e "\n已创建";elif ! grep -q "appendWindowsPath.*=.*false" "$config";then echo -e "\n$content"|sudo tee -a "$config">/dev/null;echo "=== 配置后 ===";cat "$config";echo -e "\n已添加";else echo "=== 配置后 ===";cat "$config";echo -e "\n无需修改";fi

缓存 USERPROFILE

避免 Claude Code 频繁调用 PowerShell 获取 Windows 用户目录:

bash
shell_rc="$HOME/.$(basename $SHELL)rc";winuser=$(cmd.exe /c "echo %USERNAME%" 2>/dev/null|tr -d '\r');content="export USERPROFILE=\"/mnt/c/Users/$winuser\"";echo "=== 当前 Shell: $SHELL ===";echo "=== 配置文件: $shell_rc ===";echo "";echo "=== 配置前 ===";grep -n "USERPROFILE" "$shell_rc" 2>/dev/null||echo "(未找到)";echo "";if grep -q "export USERPROFILE=" "$shell_rc" 2>/dev/null;then echo "=== 配置后 ===";grep -n "USERPROFILE" "$shell_rc";echo -e "\n已存在";else echo "$content">>"$shell_rc";echo "=== 配置后 ===";grep -n "USERPROFILE" "$shell_rc";echo -e "\n已添加: $content\n请执行: source $shell_rc";fi

Windows Terminal 滚动优化

Claude Code 输出长内容时,Windows Terminal 默认的 9001 行历史记录容易溢出,导致滚动条跳回顶部。将 historySize 设为最大值 32767 可显著改善。

方法一:图形界面设置

  1. 打开 Windows Terminal → 下拉箭头 → 设置
  2. 左侧选择 配置文件 → 默认值
  3. 找到 高级 → 历史记录大小,改为 32767
  4. 点击 保存,重启 Windows Terminal

方法二:一键配置(推荐)

在 PowerShell 中执行:

powershell
$settingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"; if (!(Test-Path $settingsPath)) { $settingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json" }; Write-Host "=== 配置文件: $settingsPath ===" -ForegroundColor Yellow; if (Test-Path $settingsPath) { $json = Get-Content $settingsPath -Raw | ConvertFrom-Json; $current = $json.profiles.defaults.historySize; Write-Host "=== 配置前: historySize = $current ==="; $json.profiles.defaults | Add-Member -NotePropertyName "historySize" -NotePropertyValue 32767 -Force; $json | ConvertTo-Json -Depth 100 | Set-Content $settingsPath -Encoding UTF8; Write-Host "=== 配置后: historySize = 32767 ===" -ForegroundColor Green; Write-Host "`n请重启 Windows Terminal" } else { Write-Host "未找到 Windows Terminal 配置文件" -ForegroundColor Red }

重启 WSL

配置完成后需要重启 WSL 使配置生效:

powershell
wsl --shutdown