美文网首页
windows terminal 使用

windows terminal 使用

作者: wwmin_ | 来源:发表于2020-08-11 01:28 被阅读0次

Windows 终端是一个面向命令行工具和 shell(如命令提示符、PowerShell 和适用于 Linux 的 Windows 子系统 (WSL))用户的新式终端应用程序。 它的主要功能包括多个选项卡、窗格、Unicode 和 UTF-8 字符支持、GPU 加速文本呈现引擎,还可以用于创建你自己的主题并自定义文本、颜色、背景和快捷键绑定。

  1. 安装
    从 windows store中安装,搜索terminal 有两个,一个microsoft terminal一个windows terminal preview , 选择后者。
    如果你无法访问 Microsoft Store,GitHub 发布页上发布有内部版本。 如果从 GitHub 安装,终端将不会自动更新为新版本。
  2. 配置参考
    可参考我的配置:
// This file was initially generated by Windows Terminal Preview 1.2.2022.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
  "$schema": "https://aka.ms/terminal-profiles-schema",

  "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

  // You can add more global application settings here.
  // To learn more about global settings, visit https://aka.ms/terminal-global-settings

  // If enabled, selections are automatically copied to your clipboard.
  "copyOnSelect": false,

  // If enabled, formatted data is also copied to your clipboard
  "copyFormatting": false,
  // A profile specifies a command to execute paired with information about how it should look and feel.
  // Each one of them will appear in the 'New Tab' dropdown,
  //   and can be invoked from the commandline with `wt.exe -p xxx`
  // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
  "profiles": {
    "defaults": {
      // Put settings here that you want to apply to all profiles.
      "fontFace": "Cascadia Code",
      "fontSize": 10,
      "useAcrylic": true, //使用背景透明
      "acrylicOpacity": 0.8, //背景透明度
      "launchMode": "default", //启动大小 default , maximized , fullscreen
      "initialPosition": "800,#" //启动位置

    },
    "list": [
      {
        // Make changes here to the cmd.exe profile.
        "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
        "name": "命令提示符",
        "commandline": "cmd.exe",
        "hidden": false,
        "startingDirectory": null //默认值:"%USERPROFILE%",通过地址栏启动,输入 wt 即可打开Windows terminal (类似cmd)
      },
      {
        // Make changes here to the powershell.exe profile.
        "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
        "name": "Windows PowerShell",
        "commandline": "powershell.exe",
        "startingDirectory": null,
        "fontFace": "Cascadia Code PL",
        "hidden": false
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
        "hidden": true,
        "name": "Azure Cloud Shell",
        "source": "Windows.Terminal.Azure"
      },
      {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
        "hidden": false,
        "name": "git bash",
        "icon": "D:\\Program Files\\Git\\favicon.ico",
        "commandline": "D:\\Program Files\\Git\\bin\\bash.exe"
      }
    ]
  },

  // Add custom color schemes to this array.
  // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
  "schemes": [],

  // Add custom keybindings to this array.
  // To unbind a key combination from your defaults.json, set the command to "unbound".
  // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
  "keybindings": [
    // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
    // These two lines additionally bind them to Ctrl+C and Ctrl+V.
    // To learn more about selection, visit https://aka.ms/terminal-selection
    {
      "command": {
        "action": "copy",
        "singleLine": false
      },
      "keys": "ctrl+c"
    },
    {
      "command": "paste",
      "keys": "ctrl+v"
    },

    // Press Ctrl+Shift+F to open the search box
    {
      "command": "find",
      "keys": "ctrl+shift+f"
    },

    // Press Alt+Shift+D to open a new pane.
    // - "split": "auto" makes this pane open in the direction that provides the most surface area.
    // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
    // To learn more about panes, visit https://aka.ms/terminal-panes
    {
      "command": {
        "action": "splitPane",
        "split": "auto",
        "splitMode": "duplicate"
      },
      "keys": "alt+shift+d"
    }
  ]
}

效果图:


windows terminal
  1. 添加git-bash 命令
    在list中添加一个对象
 {
        "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b7}",
        "hidden": false,
        "name": "git bash",
        "icon": "D:\\Program Files\\Git\\favicon.ico",
        "commandline": "D:\\Program Files\\Git\\bin\\bash.exe"
}

其中guid可用工具生成, icon从官网下载(打开git-bash官网后,在控制台网络中可找到favicon.ico),放到适当位置,引用其位置即可。
commandline填写已安装的git下的bash.exe路径

  1. 在powershell中使用git并带有命令行自动补全和漂亮的文本
    参考:https://docs.microsoft.com/zh-cn/windows/terminal/tutorials/powerline-setup
  • 下载字体并安装:Cascadia Mono PL , 字体下载:CascadiaPL.ttf, 下载后直接双击字体即可安装。

  • 使用 PowerShell,安装 Posh-Git 和 Oh-My-Posh:

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

注意此处安装可能会失败,需要科学上网/FQ工具

  • 使用powershell 启动自动加载配置文件
echo $profile
Test-Path $profile
New-Item -Path $profile -Type File –Force

使用echo $profile检查.ps1文件是否存在,Test-Path $profile进一步测试是否存在,若不存在则用命令新建一个New-Item -Path $profile -Type File –Force

使用文本编辑器打开该 PowerShell 配置文件,该脚本在每次启动 PowerShell 时运行。 详细了解 PowerShell 配置文件
在 PowerShell 配置文件中,将以下内容添加到文件的末尾:

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

现在,每个新实例启动时都会导入 Posh-Git 和 Oh-My-Posh,然后从 Oh-My-Posh 设置 Paradox 主题。 Oh-My-Posh 附带了若干内置主题
效果如下:

带有命令补全和漂亮窗体的powershell git
  • 注意: 如果启动powershell时报错如下:
. : 无法加载文件 C:\Users\wweim\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1,因为在此系统上禁止运行脚
本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . 'C:\Users\wweim\Documents\WindowsPowerShell\Microsoft.PowerShell_pr ...
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

则可根据https:/go.microsoft.com/fwlink/?LinkID=135170中的提示进行设置, 简单的方式可这样操作:
使用管理员权限打开powershell

Get-ExecutionPolicy
# 如果是Restricted, 则代表不能使用本地加载ps1
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
# 设置本地用户执行策略为 RemoteSigned

完成, 重新打开terminal后进入powershell可发现已经可以加载ps1文件

参考:windows terminal官方文档

相关文章

网友评论

      本文标题:windows terminal 使用

      本文链接:https://www.haomeiwen.com/subject/qzzsdktx.html