vscode中使用powershell显示分支名

由于我的电脑打开git bash很慢很慢,在vscode中我想使用默认的powershell,但又想看到分支名称,于是倒腾了一下

效果图
windows powershell(或windows terminal)启动前会先执行一个.ps1的配置文件,通过修改此文件可以对powershell做一些预设置。

  1. 打开powershell窗口,输入命令$Profile查看ps1文件位置。
$Profile
C:\Users\hnxyc\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

变量$Profile保存了ps1文件的绝对路径。

  1. ps1文件默认不存在,按路径手动创建文件即可。也可以敲下面的命令快速创建。
New-Item –Path $Profile –Type File –Force
  1. 编辑ps1文件
notepad $Profile

在弹出的记事本里将下面的代码拷进去,即可。

function Write-BranchName () {
    try {
        $branch = git rev-parse --abbrev-ref HEAD
        if ($branch -eq "HEAD") {
            # we're probably in detached HEAD state, so print the SHA
            $branch = git rev-parse --short HEAD
            Write-Host " ($branch)" -ForegroundColor "red"
        }
        else {
            # we're on an actual branch, so print it
            Write-Host " ($branch)" -ForegroundColor "blue"
        }
    } catch {
        # we'll end up here if we're in a newly initiated git repo
        Write-Host " (no branches yet)" -ForegroundColor "yellow"
    }
}

function prompt {
    $base = "PS "
    $path = "$($executionContext.SessionState.Path.CurrentLocation)"
    $userPrompt = "$('>' * ($nestedPromptLevel + 1)) "
    Write-Host "`n$base" -NoNewline
    if (Test-Path .git) {
        Write-Host $path -NoNewline -ForegroundColor "green"
        Write-BranchName
    }
    else {
        # we're not in a repo so don't bother displaying branch name/sha
        Write-Host $path -ForegroundColor "green"
    }
    return $userPrompt
}

$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

$OutputEncoding是为了支持分支名为中文的情况。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值