让vscode终端powershell拥有自动提示和补全

1,868 阅读1分钟

让vscode终端拥有像copilot那样的代码提示

首先先上效果图: image.png

前面的p是我输入的,后面的是给我提示的,当我按下table的时候他会把所有的结果渲染出来让我选择 image.png

首先需要一个新版本的终端,可以去微软商店去搜终端两个字,然后下载那个终端。

然后以管理员打开vscode 按 ctrl + ~ 打开终端

然后在终端执行这个命令

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

然后在终端输入

Test-path $profile

# 如果没有就创建一个
New-item –type file –force $profile

# 如果有就输入
$profile
# 然后会出现一个url,按住ctrl点击他
# 然后会在vscode打开他
# 把下面的复制进去

# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

# Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

# auto suggestions
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History

然后点那个垃圾桶关闭终端,然后再按快捷键打开一个终端, 这个时候应该就有了

主题也可以下载一下

# powershell执行下面的
winget install JanDeDobbeleer.OhMyPosh -s winget

#然后在$profile中复制下面的内容

oh-my-posh init pwsh --config 你的主题路径 | Invoke-Expression
Import-Module posh-git

# 你的主题路径可以通过这个命令获取
Get-PoshThemes

# 然后重启终端

最后$profile配置这样的

oh-my-posh init pwsh --config C:\xxx\oh-my-posh\themes\catppuccin.omp.json | Invoke-Expression
Import-Module posh-git
# Shows navigable menu of all options when hitting Tab
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete

# Autocompletion for arrow keys
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

# auto suggestions
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History

如果主题出现乱码是字体问题,当前字体不支持这个图标,在vscode设置里搜索终端字体,然后换一下就行了。

主题官网 ohmyposh.dev/docs/instal…

自动提示 dev.to/animo/fish-…