VSCode 开发,PowerShell 高效开发完整配置

1. 配置 VSCode 使用 PowerShell 7

VSCode 快捷键打开用户配置文件 (Json)Ctrl+Shift+P,输入 open user settings,选择打开 settings.json 文件,添加以下内容:

"terminal.integrated.profiles.windows": {
    "PowerShell7": {
        "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
        "args": [],
        "icon": "terminal-powershell"
    },
    "Command Prompt": {
        "path": [
            "${env:windir}\\Sysnative\\cmd.exe",
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": [],
        "icon": "terminal-cmd"
    },
    "Git Bash": {
        "source": "Git Bash"
    }
},
"terminal.integrated.defaultProfile.windows": "PowerShell7",

2. PowerShell 执行策略设置

Windows 默认不允许自动运行脚本,需要以管理员身份执行:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm

3. 安装 posh-git

posh-git 为 PowerShell 提供 Git 状态信息(branch、暂存区、工作区变更数量等),并支持 Git 命令 Tab 补全。

# 更新 PowerShellGet(如有必要)
Install-Module PowerShellGet -Force

# 全新安装
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force

# 升级已有版本
PowerShellGet\Update-Module posh-git

3.1. posh-git 状态符号说明

Prompt 格式为 [{branch} S +A ~B -C | +E ~F -G !H]

  • S:与远端的同步状态( 同步、↑n 领先、↓n 落后、× 远端分支已删除)
  • | 左侧:暂存区(Index)变更(+ 新增、~ 修改、- 删除、! 冲突)
  • | 右侧:工作区(Working Tree)未暂存变更

3.2. 配置 posh-git 到 Profile

安装完成后,执行一次以下命令(仅执行一次,它会自动将 Import-Module posh-git 写入 profile 文件):

# 仅对当前 host(推荐)
Add-PoshGitToProfile

# 对所有 host(Console、ISE 等)
Add-PoshGitToProfile -AllHosts

4. 安装配置 oh-my-posh

oh-my-posh 提供更美观的 PowerShell Prompt 主题。

4.1. 安装

# 安装 PSReadLine(命令行增强,支持历史预测等)
Install-Module -Name PSReadLine -Scope CurrentUser

# 安装 oh-my-posh
winget install JanDeDobbeleer.OhMyPosh -s winget

4.2. 配置 Profile

打开 profile 文件:

notepad $PROFILE

添加以下内容(注意替换 Administrator 为你的用户名):

# oh-my-posh 主题(选择一个)
# hotstick.minimal.omp.json -- 单行,Prompt 简短
# agnoster.minimal.omp.json -- 经典单行风格
oh-my-posh init pwsh --config 'C:\Users\Administrator\AppData\Local\Programs\oh-my-posh\themes\hotstick.minimal.omp.json' | Invoke-Expression

可用主题参考:ohmyposh.dev/docs/themes

4.3. 乱码处理(字体设置)

oh-my-posh Prompt 使用特殊符号,需安装支持 Box-drawing 的字体,推荐 Fira Code 或 Cascadia Code。

安装字体后,在 Windows Terminal 的默认值 → 外观 → 字体中选择对应字体。

5. PSReadLine 高效命令行设置

以下配置让 PowerShell 命令历史记录更易用,加入 $PROFILE 文件中:

# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History

# Tab 键显示可选菜单(类似 zsh)
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

# 上下方向键:按已输入前缀搜索历史记录,并将光标移到行尾
Set-PSReadLineKeyHandler -Key UpArrow -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchBackward()
    [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
Set-PSReadLineKeyHandler -Key DownArrow -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchForward()
    [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}

说明:使用 ScriptBlock 方式(而非直接 -Function HistorySearchBackward)的好处是:搜索后光标自动跳到行尾,直接可以继续补充参数。

6. Git 快捷命令设置

$PROFILE 中添加以下别名函数,需放在 Import-Module posh-git 之前(posh-git 会识别已定义的 git 别名用于 Tab 补全):

# 美化的 git log 图形视图
function gl {
    git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
}

# 简洁 git status
function g { git status -sb }

7. 清屏快捷键(VSCode 终端)

VSCode 中 cls 命令仅滚动缓冲区,建议用快捷键清屏:

Ctrl+Shift+P → 搜索 Terminal: Clear → 绑定快捷键 Ctrl+L

8. 设置终端编码为 UTF-8

# 在 $PROFILE 中添加,防止中文乱码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

同时需要将终端字体改为支持 Box-drawing 的字体(如 Cascadia Code、Fira Code)。点阵字体(如新宋体)不支持。

附:完整 Profile 示例

# git 别名函数需在 Import-Module posh-git 之前定义,以便 posh-git Tab 补全识别
# git log 图形视图
function gl { git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit }

# git status 简洁视图
function g { git status -sb }

Import-Module posh-git
# 若同时使用 oh-my-posh,需开启此变量以让 oh-my-posh 读取 posh-git 状态
$env:POSH_GIT_ENABLED = $true

Import-Module PSReadLine

# 设置预测文本来源为历史记录
Set-PSReadLineOption -PredictionSource History

# Tab 键显示可选补全菜单
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

# 上下方向键:按已输入前缀搜索历史记录,并将光标移至行尾
Set-PSReadLineKeyHandler -Key UpArrow -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchBackward()
    [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}
Set-PSReadLineKeyHandler -Key DownArrow -ScriptBlock {
    [Microsoft.PowerShell.PSConsoleReadLine]::HistorySearchForward()
    [Microsoft.PowerShell.PSConsoleReadLine]::EndOfLine()
}

# 设置 PowerShell 输出编码为 UTF-8,还需要将字体改为支持 Box-drawing 的字体
# 比如 Cascadia Code、Consolas。新宋体这种点阵字体不行。
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

参考资料




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • AI工具大全
  • Fast DDS入门(On-Going)
  • NVIDIA GPU 架构:SP、SM 与 LSU 工作原理详解
  • al-folio 模板定制修改总结
  • al-folio 部署记录(Ubuntu 24.04)