admin 管理员组

文章数量: 887019

感谢大佬批评指正,现已更新

preview

Target:致力打造最赏心悦目Window下的终端,同时能够很接近Linux的使用习惯
key word:windows终端美化 windows terminal windows powershell 类似Linux下的Window终端 Window也能用ll windows terminal一键直连ssh

目的是在Windows上构建一个和Linux类似的Terminal,让Windows炼丹和Linux一样舒适,同是让Terminal取代Xshell完成远程链接。
预览如下图。按照教程,你也可以实现下面的效果。


在Linux下我们使用zsh和oh-my-zsh结合,Windows下我们使用powershell7和oh-my-posh结合,并将结果显示在Window Terminal上。
前提是机器已经安装了ssh、sshd、conda、git

安装Window Terminal

如果已经安装好Window Terminal,或者本身就是win11系统_[系统自带Window Terminal]_,就请跳过本步骤。

这里是Window Terminal官网下载链接
或者Window Store中直接搜索terminal ,就会出现。个人建议安装预览版。

安装powershell7

从这里https://github/PowerShell/PowerShell/releases下载绿色版(避免重装系统消失),放置到D:\Program Files\PowerShell-7.4.1-win-x64目录下

并这个文件夹里面创建一个 Profile.ps1文件。pwsh启动会先执行这个文件。这里讲解了pwsh启动时,执行profile的顺序。内容后面会提及,先新建一个空白文件。

设置环境变量

PSHOME=D:\Program Files\PowerShell-7.4.1-win-x64\
# 将这些env加入path

这样才能命令行启动

参考命令
$PROFILE | Select-Object * # 查看PROFILE的位置
Test-Path -Path $PROFILE.AllUsersAllHosts # 测试脚本存在
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # 解除严格模式
set-ExecutionPolicy RemoteSigned # 解除严格模式

Install-Module -Name PowerShellGet -Force # 安装get模块
Get-InstalledModule # 获取已经安装的模块

安装oh-my-posh

打开Window Terminal,将pwsh7添加到Window Terminal中,

然后打开一个pwsh窗口,执行以下命令,安装posh-git、oh-my-posh、ZLocation…

Install-Module posh-git -Scope CurrentUser # posh-git
Install-Module oh-my-posh -Scope CurrentUser -RequiredVersion 2.0.496 # oh-my-posh
Install-Module ZLocation -Scope CurrentUser # ZLocation (opens new window)和 autojump 差不多效果。快速 cd 到历史去过的目录。

安装字体

oh-my-posh font install # 需要管理员权限的终端)
# 如果总是有 各种字体问题。我建议直接使用JetBrainsMono Nerd Font

这里是把字体安装在操作系统中,如果发现Terminal中字体还是不正确,原因是Terminal中的字体没有调整,建议直接在这里https://www.nerdfonts/font-downloads下载。我推荐使用’JetBrainsMono Nerd Font’。原因是图形齐全,字体简洁。

编辑profile

这里类似于Linux上的.zshrc的文件。

Linux上的zsh和这里的powershell7一样,都是shell程序。所以这里的$PROFILE就是在shell启动时前运行的脚本配置文件。

还记的前面让你在程序工作目录下创建的Profile.ps1文件吗,他就会每一次pwsh图像展示前先运行的脚本。

在pwsh7运行

$PROFILE | Select-Object *


这里就是显示的所有的配置文件和默认的位置,当然他可以是不存在的。这个列表从上到下,显示pwsh运行前,脚本的执行顺序。当然看名字也能猜出来,它其实还有用户和实例区分的,这里就先不管那么多。红色框起来的就是我们今天需要编辑的profile

AllUsersAllHosts文件编辑

这个文件就程序工作目录下创建的Profile.ps1文件了。$PSHOME 就可以查看程序所在的绝对路径。
这里我们主要是来为所有用户激活一下conda环境,毕竟这个是炼丹刚需。

$global:profiles = "$PSHOME\Profile.ps1"
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "D:\ProgramData\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion
CurrentUserCurrentHost文件编辑

这个文件就是su、ll、vim等常见工具的设定、还有oh-my-posh的主题等设置

# sudo命令
function _sudo {
    $ss = "$args ; pause"
    Start-Process wt -Verb runAs -ArgumentList $ss
}

Function _su {
Start-Process -verb runas "wt"
}

# setup alias 设置命令的别名
set-alias -name sudo -value _sudo
Set-Alias ll ls
Set-Alias su _su
Set-Alias vi "D:\Program Files\vim\vim91\vim.exe"
Set-Alias vim "D:\Program Files\vim\vim91\vim.exe"

#oh-my-posh init
Import-Module posh-git # 引入 posh-git
Import-Module oh-my-posh # 引入 oh-my-posh

Set-Theme PowerlinePlus # 设置主题为 Paradox, Emodipt, Honukai ,PowerlinePlus ,qwerty ,Sorin ,Zash 
Set-PSReadLineOption -PredictionSource History # 设置预测文本来源为历史记录 
Set-PSReadlineKeyHandler -Key Tab -Function Complete # 设置 Tab 键补全
Set-PSReadLineKeyHandler -Key "Ctrl+d" -Function MenuComplete # 设置 Ctrl+d 为菜单补全和 Intellisense
Set-PSReadLineKeyHandler -Key "Ctrl+z" -Function Undo # 设置 Ctrl+z 为撤销
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward # 设置向上键为后向搜索历史记录
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward # 设置向下键为前向搜索历史纪录

快捷美化

Window Terminal的json文件

这里的json可以直接控制Windows Terminal的显示,还是十分友好的。
就在这里

"backgroundImage": "E:\\VSWallPaper\\VS\u80cc\u666f\u4f7f\u7528.png",
"backgroundImageAlignment": "bottomRight",
"backgroundImageOpacity": 0.73,
"backgroundImageStretchMode": "none",

这里就是背景图显示的设置,请自行修改。背景我放在下文中,请自行获取到本地,以便于让自己每天有个好心情。**[**都看到这里了,**别忘了点赞啊] **……
这里是我个人的Window Terminal的json文件,其中很多是绝对路径,如果要使用请先修改。如背景图,pwsh的启动路径等等…

{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema-preview",
    "actions": 
    [
        {
            "command": 
            {
                "action": "copy",
                "singleLine": false
            },
            "keys": "ctrl+c"
        },
        {
            "command": "paste",
            "keys": "ctrl+v"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        },
        {
            "command": 
            {
                "action": "splitPane",
                "split": "auto",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+d"
        }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{51a1d3c2-4ee8-4a49-b09b-0e4f8c4cf2bd}",
    "newTabMenu": 
    [
        {
            "type": "remainingProfiles"
        }
    ],
    "profiles": 
    {
        "defaults": 
        {
            "backgroundImage": "E:\\VSWallPaper\\VS\u80cc\u666f\u4f7f\u7528.png",
            "backgroundImageAlignment": "bottomRight",
            "backgroundImageOpacity": 0.73,
            "backgroundImageStretchMode": "none",
            "colorScheme": "Solarized Dark",
            "elevate": false,
            "font": 
            {
                "face": "JetBrainsMono Nerd Font"
            },
            "suppressApplicationTitle": true
        },
        "list": 
        [
            {
                "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "commandline": "%SystemRoot%\\System32\\cmd.exe",
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "\u547d\u4ee4\u63d0\u793a\u7b26"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "altGrAliasing": true,
                "antialiasingMode": "grayscale",
                "closeOnExit": "automatic",
                "colorScheme": "Solarized Dark",
                "commandline": "%pwsh7%\\pwsh.exe",
                "cursorShape": "bar",
                "font": 
                {
                    "face": "JetBrainsMonoNL Nerd Font Propo",
                    "size": 12.0
                },
                "guid": "{51a1d3c2-4ee8-4a49-b09b-0e4f8c4cf2bd}",
                "hidden": false,
                "historySize": 9001,
                "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
                "name": "pwsh7",
                "padding": "8, 8, 8, 8",
                "snapOnInput": true,
                "startingDirectory": "%USERPROFILE%",
                "tabTitle": "pwsh",
                "useAcrylic": false
            },
            {
                "guid": "{2a9350cf-37b9-5bfa-b5b3-564b3864eb96}",
                "hidden": false,
                "name": "Developer Command Prompt for VS 2019",
                "source": "Windows.Terminal.VisualStudio"
            },
            {
                "guid": "{19d39df3-1eed-58d6-a707-099b09917476}",
                "hidden": false,
                "name": "Developer PowerShell for VS 2019",
                "source": "Windows.Terminal.VisualStudio"
            },
            {
                "altGrAliasing": true,
                "antialiasingMode": "grayscale",
                "closeOnExit": "automatic",
                "colorScheme": "Solarized Dark",
                "commandline": "%pwsh7%\\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"ssh icml@fr1p -p 36166\"",
                "cursorShape": "bar",
                "font": 
                {
                    "face": "Cascadia Code",
                    "size": 12.0
                },
                "guid": "{f92f3e12-e2ac-4cd9-b054-8a5e56b1446b}",
                "hidden": false,
                "historySize": 9001,
                "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
                "name": "online_DELL_FrpcSakura",
                "padding": "8, 8, 8, 8",
                "snapOnInput": true,
                "startingDirectory": "%USERPROFILE%",
                "tabTitle": "online_DELL_FrpcSakura",
                "useAcrylic": false
            },
            {
                "altGrAliasing": true,
                "antialiasingMode": "grayscale",
                "closeOnExit": "automatic",
                "colorScheme": "Solarized Dark",
                "commandline": "%pwsh7%\\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"ssh icml@ganzhou-eb7c5293c01.shop -p 53919\"",
                "cursorShape": "bar",
                "font": 
                {
                    "face": "Cascadia Code",
                    "size": 12.0
                },
                "guid": "{d2e40cc6-4b49-48dd-851c-29a6917059bc}",
                "hidden": false,
                "historySize": 9001,
                "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
                "name": "online_DELL_FrpcOpen",
                "padding": "8, 8, 8, 8",
                "snapOnInput": true,
                "startingDirectory": "%USERPROFILE%",
                "tabTitle": "online_DELL_FrpcOpen",
                "useAcrylic": false
            },
            {
                "altGrAliasing": true,
                "antialiasingMode": "grayscale",
                "closeOnExit": "automatic",
                "colorScheme": "Solarized Dark",
                "commandline": "%pwsh7%\\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"ssh grozta@fr1bus.top -p 36777\"",
                "cursorShape": "bar",
                "font": 
                {
                    "face": "Cascadia Code",
                    "size": 12.0
                },
                "guid": "{ba0d2d74-61aa-4754-9097-a483ad39c491}",
                "hidden": false,
                "historySize": 9001,
                "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
                "name": "online_ONDA_FrpcSakura",
                "padding": "8, 8, 8, 8",
                "snapOnInput": true,
                "startingDirectory": "%USERPROFILE%",
                "tabTitle": "online_ONDA_FrpcSakura",
                "useAcrylic": false
            },
            {
                "altGrAliasing": true,
                "antialiasingMode": "grayscale",
                "closeOnExit": "automatic",
                "colorScheme": "Solarized Dark",
                "commandline": "%pwsh7%\\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"ssh grozta@ga17c59a5.of-7af93c01.shop -p 53215\"",
                "cursorShape": "bar",
                "font": 
                {
                    "face": "Cascadia Code",
                    "size": 12.0
                },
                "guid": "{42e91b42-dcb1-466c-b6e6-8e8276c437b2}",
                "hidden": false,
                "historySize": 9001,
                "icon": "ms-appx:///ProfileIcons/{61c54bbd-c2c6-5271-96e7-009a87ff44bf}.png",
                "name": "online_ONDA_FrpcOpen",
                "padding": "8, 8, 8, 8",
                "snapOnInput": true,
                "startingDirectory": "%USERPROFILE%",
                "tabTitle": "online_ONDA_FrpcOpen",
                "useAcrylic": false
            }
        ]
    },
    "schemes": 
    [
        {
            "background": "#0C0C0C",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#012456",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell Powershell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#282C34",
            "black": "#282C34",
            "blue": "#61AFEF",
            "brightBlack": "#5A6374",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B6C2",
            "brightGreen": "#98C379",
            "brightPurple": "#C678DD",
            "brightRed": "#E06C75",
            "brightWhite": "#DCDFE4",
            "brightYellow": "#E5C07B",
            "cursorColor": "#FFFFFF",
            "cyan": "#56B6C2",
            "foreground": "#DCDFE4",
            "green": "#98C379",
            "name": "One Half Dark",
            "purple": "#C678DD",
            "red": "#E06C75",
            "selectionBackground": "#FFFFFF",
            "white": "#DCDFE4",
            "yellow": "#E5C07B"
        },
        {
            "background": "#FAFAFA",
            "black": "#383A42",
            "blue": "#0184BC",
            "brightBlack": "#4F525D",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B5C1",
            "brightGreen": "#98C379",
            "brightPurple": "#C577DD",
            "brightRed": "#DF6C75",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#E4C07A",
            "cursorColor": "#4F525D",
            "cyan": "#0997B3",
            "foreground": "#383A42",
            "green": "#50A14F",
            "name": "One Half Light",
            "purple": "#A626A4",
            "red": "#E45649",
            "selectionBackground": "#4F525D",
            "white": "#FAFAFA",
            "yellow": "#C18301"
        },
        {
            "background": "#002B36",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#FFFFFF",
            "cyan": "#2AA198",
            "foreground": "#839496",
            "green": "#859900",
            "name": "Solarized Dark",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#FDF6E3",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#002B36",
            "cyan": "#2AA198",
            "foreground": "#657B83",
            "green": "#859900",
            "name": "Solarized Light",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#073642",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#FFFFFF",
            "cyan": "#06989A",
            "foreground": "#D3D7CF",
            "green": "#4E9A06",
            "name": "Tango Dark",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#FFFFFF",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#000000",
            "cyan": "#06989A",
            "foreground": "#555753",
            "green": "#4E9A06",
            "name": "Tango Light",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#555753",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#000080",
            "brightBlack": "#808080",
            "brightBlue": "#0000FF",
            "brightCyan": "#00FFFF",
            "brightGreen": "#00FF00",
            "brightPurple": "#FF00FF",
            "brightRed": "#FF0000",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFFF00",
            "cursorColor": "#FFFFFF",
            "cyan": "#008080",
            "foreground": "#C0C0C0",
            "green": "#008000",
            "name": "Vintage",
            "purple": "#800080",
            "red": "#800000",
            "selectionBackground": "#FFFFFF",
            "white": "#C0C0C0",
            "yellow": "#808000"
        }
    ],
    "themes": [],
    "windowingBehavior": "useAnyExisting"
}

Window Terminal上的背景图

**[**都看到这里了,**别忘了点赞啊] **

最终预览

Terminal中进行ssh快捷登录,从而代替Xshell

思路

想要在Terminal中实现ssh快捷链接,就是需要再pwsh中引导ssh工作,也就是说只要把启动命令中加入ssh登录语句就可以实现在Terminal中进行ssh,从而代替Xshell

ssh grozta@192.168.199.128 就是我们通常使用的ssh登录语句

在conda的启动快捷方式中,它的启动命令是

pwsh.exe -ExecutionPolicy ByPass -NoExit -Command "&'D:\code\miniconda\shell\condabin\conda-hook'"

所以我们想要快速启动ssh进行远程链接,只需要把-Command 后的参数替换成ssh登录语句就可以了

方法

因此我们只需要新建一个配置文件,要从前面的我们pwsh配置中复制一份,命令行修改为
%PSHOME%\pwsh.exe -ExecutionPolicy ByPass -NoExit -Command "ssh grozta@192.168.199.128"

这里的配置,在上述的Windows Terminal的json文件中有具体的写入,保证一定能用。

预览

然后我们开启新的标签,输入密码

参考

打造好用的PowerShell媲美oh-my-zsh | Exploring
给 PowerShell 带来 zsh 的体验
Nerd Fonts - Iconic font aggregator, glyphs/icons collection, & fonts patcher 单独的字体下载给Terminal
Windows Terminal 终端个性化设置指南_windows terminal配色-CSDN博客

本文标签: 构建一个 类似 Windows terminal Linux