记一次配置Windows终端terminal

立即安装Windows终端terminal

Git发布页面

发布地址 https://github.com/microsoft/terminal/releases

应用商店

安装地址 https://aka.ms/terminal

winget

winget install --id=Microsoft.WindowsTerminal -e

如果提示winget : 无法将“winget”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

如未安装winget,安装地址 https://www.microsoft.com/p/app-installer/9nblggh4nns1#activetab=pivot:overviewtab

如已安装,可能是因为没有将winget添加到环境变量中,可以添加下,或者试试下面这条命令

%LOCALAPPDATA%\Microsoft\WindowsApps\winget install --id=Microsoft.WindowsTerminal -e

安装 Oh My Posh

安装前的注意事项

Windows PowerShell 执行策略
在执行 winget install 命令之前,先查看下,Windows PowerShell 的执行策略

执行策略 是 Windows PowerShell安全策略中的一部分,它将决定你是否可以载入配置文件(包括你的 Windows PowerShell profile文件)和运行脚本,它将会在运行前确定哪些文件必须具有数字签名(digitally signed)

get-ExecutionPolicy 获取 Windows PowerShell 当前执行策略

set-ExecutionPolicy 修改 Windows PowerShell 中执行策略的用户首选项(preference)

执行策略状态说明

`Restricted` 表示状态是禁止的 不载入配置文件, 不执行脚本. Restricted 是默认值.
`RemoteSigned` 所有从互联网上下载的脚本必须通过信任的出版商签名(trusted publisher)
`AllSigned` 所有的配置文件和脚本必须通过信任的出版商签名(trusted publisher),这里所指的脚本页包括在本地计算机上创建的脚本
`Unrestricted` 载入所有的配置文件和脚本,如果运行了一个从互联网上下载且没有数字签名的脚本,在执行前都会被提示是否执行
// 获取 Windows PowerShell 当前执行策略
get-ExecutionPolicy

// 修改 Windows PowerShell 中执行策略
set-ExecutionPolicy RemoteSigned

开始安装

官网链接 https://ohmyposh.dev

发布页下载 https://github.com/JanDeDobbeleer/oh-my-posh/releases/

或者终端执行命令

// oh-my-posh 安装命令
winget install JanDeDobbeleer.OhMyPosh

// oh-my-posh 更新命令
winget upgrade JanDeDobbeleer.OhMyPosh

//可在命令后追加-s winget --location D:\OhMyPosh来设置路径
//如 winget install JanDeDobbeleer.OhMyPosh -s winget --location D:\OhMyPosh

PS:安装成功后需要重新打开终端,才能继续后续的操作

安装 Nerd Fonts 字体解决乱码

Nerd Fonts字体中包含了大量的Icon图标

官网地址 https://www.nerdfonts.com/

releases页面 https://github.com/ryanoasis/nerd-fonts/releases/tag/v2.3.1

官方推荐使用的是Meslo,我使用的是MesloLGM NF

下载后解压丢到C:\Windows\Fonts目录即可完成安装过

打开 终端->设置->外观->字体 进行修改

智能补全/智能提示

效果类似于mac的iterm2

image

PowerShell 7.2 随 PSReadLine 2.1.0 一起提供。 有较新版本可用。 可以在 Windows PowerShell 5.1 及更新版本上安装和使用 PSReadLine 的当前版本。 对于某些功能,需要运行 PowerShell 7.2 或更高版本。

PSReadLine 为 PowerShell 控制台提供强大的命令行编辑体验。 提供以下功能:

  • 命令行的语法着色
  • 语法错误的直观指示
  • 编辑和历史记录) (更好的多行体验
  • 可自定义的键绑定
  • Cmd 和 Emacs 模式
  • 许多配置选项
  • Bash 样式完成在 Cmd 模式下 (可选,在 Emacs 模式下默认)
  • 埃马克扬克/杀环
  • 基于 PowerShell 令牌的“word”移动和删除
  • 预测 IntelliSense
  • 在控制台中动态显示“帮助”,而不会丢失命令行上的位置

PSReadLine 需要 PowerShell 5.1 或更高版本。 PSReadLine 适用于默认的 Windows 控制台主机、窗口终端和Visual Studio Code。 它在Windows PowerShell ISE 中不起作用。

可以从PowerShell 库安装 PSReadLine。 若要在受支持的 PowerShell 版本中安装 PSReadLine,请运行以下命令。

Install-Module -Name PSReadLine -AllowClobber -Force

预测 IntelliSense
预测 IntelliSense 是选项卡完成概念的补充,可帮助用户成功完成命令。 它使用户能够基于用户历史记录和其他特定于域的插件的匹配预测发现、编辑和执行完整命令。

启用预测 IntelliSense
预测性 IntelliSense 默认禁用。 若要启用预测,只需运行以下命令:

Set-PSReadLineOption -PredictionSource History

PredictionSource 参数还可以接受特定于域和自定义要求的插件。

若要禁用预测 IntelliSense,只需运行:

Set-PSReadLineOption -PredictionSource None

更多配置,可以放在配置文件中,让其每次都生效

# 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

配置文件

终端执行

code $PROFILE

首次使用,可能不存在该文件,保存即可

# 去除 Window PowerShell 版权所有提示
clear

# 配置 oh-my-posh 其中kali为主题名,可以执行get-Poshthemes获取主题列表
oh-my-posh init pwsh --config 'C:\Users\Administrator\AppData\Local\Programs\oh-my-posh\themes\kali.omp.json' | Invoke-Expression

# 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

自定义命令

您可以在配置文件中写一些常用的命令来快捷操作,如

# 使用 vscode 打开 $PROFILE 配置文件
function vscodeOpen {
 Code $PROFILE
}

# 使用 nodepad++ 打开 $PROFILE 配置文件
function notepadOpen {
 noetepad $PROFILE
}

Git 查看差异和打包差异

# git
#对比某个版本与当前版本的差异
function git-diff ([string]$commitid) {
    if ($commitid -eq "")
    {
        return Write-Warning "No CommitId"
    }
    $result=git diff $commitid head --stat
    if($? -ne $true)
    {
        return Write-Warning "Error CommitId"
    }
    Write-Output $result
}

#将某个版本与当前版本的差异打包到桌面
function git-archive ([string]$commitid) {
    if ($commitid -eq "")
    {
        return Write-Warning "No CommitId"
    }
    $result=git diff --name-only $commitid head
    if($? -ne $true)
    {
        return Write-Warning "Error CommitId"
    }
    Write-Output "git archive $commitid to C:\Users\Administrator\Desktop\up.zip"
    git archive -o C:\Users\Administrator\Desktop\up.zip head $($result)
}

#将某个版本与当前版本的差异复制到桌面ver文件夹
function git-ver ([string]$commitid) {
    if ($commitid -eq "")
    {
        return Write-Warning "No CommitId"
    }
    $result=git diff --name-only $commitid head
    if($? -ne $true)
    {
        return Write-Warning "Error CommitId"
    }
    $path="C:\Users\Administrator\Desktop\ver\"
    if (Test-Path($path))
    {
        Remove-Item $path -Recurse -Force
    }
    $c=(New-Item -ItemType Directory -Path $path)
    ForEach($file in $result)
    {
        $f=Split-Path $file -Parent
        $d=$path + $f
        if (Test-Path($d))
        {
            Copy-Item $file $d
        }
        else
        {
            $c=(New-Item -ItemType Directory -Path $d)
            Copy-Item $file $d
        }
    }
    Write-Host "OK" -foreground Green
}

其它说明

使用Windows Powershell的话,会提示尝试新的跨平台 PowerShell https://aka.ms/pscore6

可以下载新版本来去掉这个提示,下载地址:https://github.com/PowerShell/PowerShell/releases

我使用的是最后一个正式版本,下载完成后一路下一步

image

安装完成后需要重新配置一下配置文件

另外我安装后帮助语言成了英文

image

我使用下面的方法,未能成功将Powershell改成中文提示

Windows Powershell英文改成中文的Windows Powershell

修改Windows Powershell的提示语言

Update-Help -UICulture zh-CN

整个执行过程时间比较长,耐心等待一下...

image

posted @ 2023-01-19 16:40  野生程序员  阅读(1250)  评论(0编辑  收藏  举报